Skip to content

Refactor config in credentials #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,32 @@ Another example: let's say that the execution of the previous command results in
`$ arduino-cloud-cli device create --name mydevice -v`


## Set a configuration
## Set credentials

arduino-cloud-cli needs a configuration file containing an Arduino IoT Cloud client ID and its corresponding secret.
arduino-cloud-cli needs a credentials file containing an Arduino IoT Cloud client ID and its corresponding secret.
You can retrieve these credentials from the [cloud](https://create.arduino.cc/iot/integrations) by creating a new API key.

Once you have the credentials, execute the following command and provide them when asked to create a new configuration file:
Once you have the credentials, execute the following command and provide them:

`$ arduino-cloud-cli config init`
`$ arduino-cloud-cli credentials init`

By default it will be created in the arduino data directory (arduino15).
You can specify a different destination folder with the `--dest-dir` option.
arduino-cloud-cli looks for its configuration file in different directories in the following order: current working directory, parents of the current working directory, arduino15 default directory.
arduino-cloud-cli looks for its credentials file in different directories in the following order: current working directory, parents of the current working directory, arduino15 default directory.

This gives you the possibility to use different configuration files depending on the project you are working on.
This gives you the possibility to use different credentials files depending on the project you are working on.

`$ arduino-cloud-cli config init --dest-dir <destinationFolder>`
`$ arduino-cloud-cli credentials init --dest-dir <destinationFolder>`

To reset an old configuration file, just overwrite it using this command:
To reset an old credentials file, just overwrite it using this command:

`$ arduino-cloud-cli config init --overwrite`
`$ arduino-cloud-cli credentials init --overwrite`

Configuration file is supported in two different format: json and yaml. Use the `--config-format` to choose it. Default is yaml.
Credentials file is supported in two different format: json and yaml. Use the `--file-format` to choose it. Default is yaml.

`$ arduino-cloud-cli config init --config-format json`
`$ arduino-cloud-cli credentials init --file-format json`

It is also possible to specify credentials directly in `ARDUINO_CLOUD_CLIENT` and `ARDUINO_CLOUD_SECRET` environment variables. Credentials specified in environment variables have higher priority than the ones specified in config files.
It is also possible to specify credentials directly in `ARDUINO_CLOUD_CLIENT` and `ARDUINO_CLOUD_SECRET` environment variables. Credentials specified in environment variables have higher priority than the ones specified in credentials files.

## Device provisioning

Expand Down
4 changes: 2 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cloud-cli/cli/config"
"github.com/arduino/arduino-cloud-cli/cli/credentials"
"github.com/arduino/arduino-cloud-cli/cli/dashboard"
"github.com/arduino/arduino-cloud-cli/cli/device"
"github.com/arduino/arduino-cloud-cli/cli/ota"
Expand All @@ -49,7 +49,7 @@ func Execute() {
}

cli.AddCommand(version.NewCommand())
cli.AddCommand(config.NewCommand())
cli.AddCommand(credentials.NewCommand())
cli.AddCommand(device.NewCommand())
cli.AddCommand(thing.NewCommand())
cli.AddCommand(dashboard.NewCommand())
Expand Down
14 changes: 7 additions & 7 deletions cli/config/config.go → cli/credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package config
package credentials

import (
"github.com/spf13/cobra"
)

func NewCommand() *cobra.Command {
configCommand := &cobra.Command{
Use: "config",
Short: "Configuration commands.",
Long: "Configuration commands.",
credentialsCommand := &cobra.Command{
Use: "credentials",
Short: "Credentials commands.",
Long: "Credentials commands.",
}

configCommand.AddCommand(initInitCommand())
credentialsCommand.AddCommand(initInitCommand())

return configCommand
return credentialsCommand
}
58 changes: 29 additions & 29 deletions cli/config/init.go → cli/credentials/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package config
package credentials

import (
"errors"
Expand Down Expand Up @@ -43,81 +43,81 @@ var initFlags struct {
func initInitCommand() *cobra.Command {
initCommand := &cobra.Command{
Use: "init",
Short: "Initialize a configuration file",
Long: "Initialize an Arduino IoT Cloud CLI configuration",
Short: "Initialize a credentials file",
Long: "Initialize an Arduino IoT Cloud CLI credentials file",
Run: runInitCommand,
}

initCommand.Flags().StringVar(&initFlags.destDir, "dest-dir", "", "Sets where to save the configuration file")
initCommand.Flags().BoolVar(&initFlags.overwrite, "overwrite", false, "Overwrite existing config file")
initCommand.Flags().StringVar(&initFlags.format, "config-format", "yaml", "Format of the configuration file, can be {yaml|json}")
initCommand.Flags().StringVar(&initFlags.destDir, "dest-dir", "", "Sets where to save the credentials file")
initCommand.Flags().BoolVar(&initFlags.overwrite, "overwrite", false, "Overwrite existing credentials file")
initCommand.Flags().StringVar(&initFlags.format, "file-format", "yaml", "Format of the credentials file, can be {yaml|json}")

return initCommand
}

func runInitCommand(cmd *cobra.Command, args []string) {
logrus.Info("Initializing config file")
logrus.Info("Initializing credentials file")

// Get default destination directory if it's not passed
if initFlags.destDir == "" {
configPath, err := arduino.DataDir()
credPath, err := arduino.DataDir()
if err != nil {
feedback.Errorf("Error during config init: cannot retrieve arduino default directory: %v", err)
feedback.Errorf("Error during credentials init: cannot retrieve arduino default directory: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
// Create arduino default directory if it does not exist
if configPath.NotExist() {
if err = configPath.MkdirAll(); err != nil {
feedback.Errorf("Error during config init: cannot create arduino default directory %s: %v", configPath, err)
if credPath.NotExist() {
if err = credPath.MkdirAll(); err != nil {
feedback.Errorf("Error during credentials init: cannot create arduino default directory %s: %v", credPath, err)
os.Exit(errorcodes.ErrGeneric)
}
}
initFlags.destDir = configPath.String()
initFlags.destDir = credPath.String()
}

// Validate format flag
initFlags.format = strings.ToLower(initFlags.format)
if initFlags.format != "json" && initFlags.format != "yaml" {
feedback.Error("Error during config init: format is not valid, provide 'json' or 'yaml'")
feedback.Error("Error during credentials init: format is not valid, provide 'json' or 'yaml'")
os.Exit(errorcodes.ErrGeneric)
}

// Check that the destination directory is valid and build the configuration file path
configPath, err := paths.New(initFlags.destDir).Abs()
// Check that the destination directory is valid and build the credentials file path
credPath, err := paths.New(initFlags.destDir).Abs()
if err != nil {
feedback.Errorf("Error during config init: cannot retrieve absolute path of %s: %v", initFlags.destDir, err)
feedback.Errorf("Error during credentials init: cannot retrieve absolute path of %s: %v", initFlags.destDir, err)
os.Exit(errorcodes.ErrGeneric)
}
if !configPath.IsDir() {
feedback.Errorf("Error during config init: %s is not a valid directory", configPath)
if !credPath.IsDir() {
feedback.Errorf("Error during credentials init: %s is not a valid directory", credPath)
os.Exit(errorcodes.ErrGeneric)
}
configFile := configPath.Join(config.Filename + "." + initFlags.format)
if !initFlags.overwrite && configFile.Exist() {
feedback.Errorf("Error during config init: %s already exists, use '--overwrite' to overwrite it",
configFile)
credFile := credPath.Join(config.CredentialsFilename + "." + initFlags.format)
if !initFlags.overwrite && credFile.Exist() {
feedback.Errorf("Error during credentials init: %s already exists, use '--overwrite' to overwrite it",
credFile)
os.Exit(errorcodes.ErrGeneric)
}

// Take needed configuration parameters starting an interactive mode
// Take needed credentials starting an interactive mode
feedback.Print("To obtain your API credentials visit https://create.arduino.cc/iot/integrations")
id, key, err := paramsPrompt()
if err != nil {
feedback.Errorf("Error during config init: cannot take config params: %v", err)
feedback.Errorf("Error during credentials init: cannot take credentials params: %v", err)
os.Exit(errorcodes.ErrGeneric)
}

// Write the configuration file
// Write the credentials file
newSettings := viper.New()
newSettings.SetConfigPermissions(os.FileMode(0600))
newSettings.Set("client", id)
newSettings.Set("secret", key)
if err := newSettings.WriteConfigAs(configFile.String()); err != nil {
feedback.Errorf("Error during config init: cannot create config file: %v", err)
if err := newSettings.WriteConfigAs(credFile.String()); err != nil {
feedback.Errorf("Error during credentials init: cannot write credentials file: %v", err)
os.Exit(errorcodes.ErrGeneric)
}

feedback.Printf("Config file successfully initialized at: %s", configFile)
feedback.Printf("Credentials file successfully initialized at: %s", credFile)
}

func paramsPrompt() (id, key string, err error) {
Expand Down
2 changes: 1 addition & 1 deletion command/dashboard/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type CreateParams struct {

// Create allows to create a new dashboard.
func Create(params *CreateParams) (*DashboardInfo, error) {
conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion command/dashboard/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type DeleteParams struct {
// Delete command is used to delete a dashboard
// from Arduino IoT Cloud.
func Delete(params *DeleteParams) error {
conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion command/dashboard/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type ExtractParams struct {
// Extract command is used to extract a dashboard template
// from a dashboard on Arduino IoT Cloud.
func Extract(params *ExtractParams) (map[string]interface{}, error) {
conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion command/dashboard/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
// List command is used to list
// the dashboards of Arduino IoT Cloud.
func List() ([]DashboardInfo, error) {
conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion command/device/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Create(params *CreateParams) (*DeviceInfo, error) {
)
}

conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion command/device/creategeneric.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type DeviceGenericInfo struct {

// CreateGeneric command is used to add a new generic device to Arduino IoT Cloud.
func CreateGeneric(params *CreateGenericParams) (*DeviceGenericInfo, error) {
conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion command/device/createlora.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func CreateLora(params *CreateLoraParams) (*DeviceLoraInfo, error) {
return nil, err
}

conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion command/device/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Delete(params *DeleteParams) error {
return errors.New("cannot use both ID and Tags. only one of them should be not nil")
}

conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion command/device/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type ListParams struct {
// List command is used to list
// the devices of Arduino IoT Cloud.
func List(params *ListParams) ([]DeviceInfo, error) {
conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions command/device/listfrequency.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type FrequencyPlanInfo struct {
// ListFrequencyPlans command is used to list
// the supported LoRa frequency plans.
func ListFrequencyPlans() ([]FrequencyPlanInfo, error) {
conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return nil, err
}
Expand All @@ -53,7 +53,7 @@ func ListFrequencyPlans() ([]FrequencyPlanInfo, error) {
freq := FrequencyPlanInfo{
Name: f.Name,
ID: f.Id,
Advanced: fmt.Sprintf("%v",f.Advanced),
Advanced: fmt.Sprintf("%v", f.Advanced),
}
freqs = append(freqs, freq)
}
Expand Down
2 changes: 1 addition & 1 deletion command/ota/massupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func MassUpload(params *MassUploadParams) ([]Result, error) {
return nil, fmt.Errorf("%s: %w", "cannot generate .ota file", err)
}

conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion command/ota/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type UploadParams struct {
// Upload command is used to upload a firmware OTA,
// on a device of Arduino IoT Cloud.
func Upload(params *UploadParams) error {
conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion command/tag/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type CreateTagsParams struct {
// CreateTags allows to create or overwrite tags
// on a resource of Arduino IoT Cloud.
func CreateTags(params *CreateTagsParams) error {
conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion command/tag/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type DeleteTagsParams struct {
// DeleteTags command is used to delete tags of a device
// from Arduino IoT Cloud.
func DeleteTags(params *DeleteTagsParams) error {
conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion command/thing/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type BindParams struct {
// Bind command is used to bind a thing to a device
// on Arduino IoT Cloud.
func Bind(params *BindParams) error {
conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion command/thing/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type CloneParams struct {

// Clone allows to create a new thing from an already existing one.
func Clone(params *CloneParams) (*ThingInfo, error) {
conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion command/thing/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type CreateParams struct {

// Create allows to create a new thing.
func Create(params *CreateParams) (*ThingInfo, error) {
conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion command/thing/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Delete(params *DeleteParams) error {
return errors.New("cannot use both ID and Tags. only one of them should be not nil")
}

conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion command/thing/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type ExtractParams struct {
// Extract command is used to extract a thing template
// from a thing on Arduino IoT Cloud.
func Extract(params *ExtractParams) (map[string]interface{}, error) {
conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion command/thing/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type ListParams struct {
// List command is used to list
// the things of Arduino IoT Cloud.
func List(params *ListParams) ([]ThingInfo, error) {
conf, err := config.Retrieve()
conf, err := config.RetrieveCredentials()
if err != nil {
return nil, err
}
Expand Down
Loading