Skip to content

Commit b4ef674

Browse files
Paolo Calaopolldo
Paolo Calao
authored andcommitted
Refactor config in credentials (#91)
We want the configuration split in two files: 'arduino-cloud.yaml' and 'arduino-cloud-credentials.yaml' They will have two separate commands: 'arduino-cloud-cli config' and 'arduino-cloud-cli credentials' At the moment only credentials have been added, but in the future we could add a config with these params: url endpoint; output format; verbose; breaking change: 'config' command is now changed into 'credentials'
1 parent b092535 commit b4ef674

28 files changed

+301
-300
lines changed

README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,32 @@ Another example: let's say that the execution of the previous command results in
2828
`$ arduino-cloud-cli device create --name mydevice -v`
2929

3030

31-
## Set a configuration
31+
## Set credentials
3232

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

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

38-
`$ arduino-cloud-cli config init`
38+
`$ arduino-cloud-cli credentials init`
3939

4040
By default it will be created in the arduino data directory (arduino15).
4141
You can specify a different destination folder with the `--dest-dir` option.
42-
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.
42+
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.
4343

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

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

48-
To reset an old configuration file, just overwrite it using this command:
48+
To reset an old credentials file, just overwrite it using this command:
4949

50-
`$ arduino-cloud-cli config init --overwrite`
50+
`$ arduino-cloud-cli credentials init --overwrite`
5151

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

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

56-
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.
56+
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.
5757

5858
## Device provisioning
5959

cli/cli.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
"github.com/arduino/arduino-cli/cli/errorcodes"
2727
"github.com/arduino/arduino-cli/cli/feedback"
28-
"github.com/arduino/arduino-cloud-cli/cli/config"
28+
"github.com/arduino/arduino-cloud-cli/cli/credentials"
2929
"github.com/arduino/arduino-cloud-cli/cli/dashboard"
3030
"github.com/arduino/arduino-cloud-cli/cli/device"
3131
"github.com/arduino/arduino-cloud-cli/cli/ota"
@@ -49,7 +49,7 @@ func Execute() {
4949
}
5050

5151
cli.AddCommand(version.NewCommand())
52-
cli.AddCommand(config.NewCommand())
52+
cli.AddCommand(credentials.NewCommand())
5353
cli.AddCommand(device.NewCommand())
5454
cli.AddCommand(thing.NewCommand())
5555
cli.AddCommand(dashboard.NewCommand())

cli/config/config.go renamed to cli/credentials/credentials.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
// You should have received a copy of the GNU Affero General Public License
1616
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1717

18-
package config
18+
package credentials
1919

2020
import (
2121
"github.com/spf13/cobra"
2222
)
2323

2424
func NewCommand() *cobra.Command {
25-
configCommand := &cobra.Command{
26-
Use: "config",
27-
Short: "Configuration commands.",
28-
Long: "Configuration commands.",
25+
credentialsCommand := &cobra.Command{
26+
Use: "credentials",
27+
Short: "Credentials commands.",
28+
Long: "Credentials commands.",
2929
}
3030

31-
configCommand.AddCommand(initInitCommand())
31+
credentialsCommand.AddCommand(initInitCommand())
3232

33-
return configCommand
33+
return credentialsCommand
3434
}

cli/config/init.go renamed to cli/credentials/init.go

+29-29
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// You should have received a copy of the GNU Affero General Public License
1616
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1717

18-
package config
18+
package credentials
1919

2020
import (
2121
"errors"
@@ -43,81 +43,81 @@ var initFlags struct {
4343
func initInitCommand() *cobra.Command {
4444
initCommand := &cobra.Command{
4545
Use: "init",
46-
Short: "Initialize a configuration file",
47-
Long: "Initialize an Arduino IoT Cloud CLI configuration",
46+
Short: "Initialize a credentials file",
47+
Long: "Initialize an Arduino IoT Cloud CLI credentials file",
4848
Run: runInitCommand,
4949
}
5050

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

5555
return initCommand
5656
}
5757

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

6161
// Get default destination directory if it's not passed
6262
if initFlags.destDir == "" {
63-
configPath, err := arduino.DataDir()
63+
credPath, err := arduino.DataDir()
6464
if err != nil {
65-
feedback.Errorf("Error during config init: cannot retrieve arduino default directory: %v", err)
65+
feedback.Errorf("Error during credentials init: cannot retrieve arduino default directory: %v", err)
6666
os.Exit(errorcodes.ErrGeneric)
6767
}
6868
// Create arduino default directory if it does not exist
69-
if configPath.NotExist() {
70-
if err = configPath.MkdirAll(); err != nil {
71-
feedback.Errorf("Error during config init: cannot create arduino default directory %s: %v", configPath, err)
69+
if credPath.NotExist() {
70+
if err = credPath.MkdirAll(); err != nil {
71+
feedback.Errorf("Error during credentials init: cannot create arduino default directory %s: %v", credPath, err)
7272
os.Exit(errorcodes.ErrGeneric)
7373
}
7474
}
75-
initFlags.destDir = configPath.String()
75+
initFlags.destDir = credPath.String()
7676
}
7777

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

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

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

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

120-
feedback.Printf("Config file successfully initialized at: %s", configFile)
120+
feedback.Printf("Credentials file successfully initialized at: %s", credFile)
121121
}
122122

123123
func paramsPrompt() (id, key string, err error) {

command/dashboard/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type CreateParams struct {
3434

3535
// Create allows to create a new dashboard.
3636
func Create(params *CreateParams) (*DashboardInfo, error) {
37-
conf, err := config.Retrieve()
37+
conf, err := config.RetrieveCredentials()
3838
if err != nil {
3939
return nil, err
4040
}

command/dashboard/delete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type DeleteParams struct {
3131
// Delete command is used to delete a dashboard
3232
// from Arduino IoT Cloud.
3333
func Delete(params *DeleteParams) error {
34-
conf, err := config.Retrieve()
34+
conf, err := config.RetrieveCredentials()
3535
if err != nil {
3636
return err
3737
}

command/dashboard/extract.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type ExtractParams struct {
3434
// Extract command is used to extract a dashboard template
3535
// from a dashboard on Arduino IoT Cloud.
3636
func Extract(params *ExtractParams) (map[string]interface{}, error) {
37-
conf, err := config.Retrieve()
37+
conf, err := config.RetrieveCredentials()
3838
if err != nil {
3939
return nil, err
4040
}

command/dashboard/list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
// List command is used to list
2626
// the dashboards of Arduino IoT Cloud.
2727
func List() ([]DashboardInfo, error) {
28-
conf, err := config.Retrieve()
28+
conf, err := config.RetrieveCredentials()
2929
if err != nil {
3030
return nil, err
3131
}

command/device/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func Create(params *CreateParams) (*DeviceInfo, error) {
6363
)
6464
}
6565

66-
conf, err := config.Retrieve()
66+
conf, err := config.RetrieveCredentials()
6767
if err != nil {
6868
return nil, err
6969
}

command/device/creategeneric.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type DeviceGenericInfo struct {
4444

4545
// CreateGeneric command is used to add a new generic device to Arduino IoT Cloud.
4646
func CreateGeneric(params *CreateGenericParams) (*DeviceGenericInfo, error) {
47-
conf, err := config.Retrieve()
47+
conf, err := config.RetrieveCredentials()
4848
if err != nil {
4949
return nil, err
5050
}

command/device/createlora.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func CreateLora(params *CreateLoraParams) (*DeviceLoraInfo, error) {
107107
return nil, err
108108
}
109109

110-
conf, err := config.Retrieve()
110+
conf, err := config.RetrieveCredentials()
111111
if err != nil {
112112
return nil, err
113113
}

command/device/delete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func Delete(params *DeleteParams) error {
4343
return errors.New("cannot use both ID and Tags. only one of them should be not nil")
4444
}
4545

46-
conf, err := config.Retrieve()
46+
conf, err := config.RetrieveCredentials()
4747
if err != nil {
4848
return err
4949
}

command/device/list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type ListParams struct {
3333
// List command is used to list
3434
// the devices of Arduino IoT Cloud.
3535
func List(params *ListParams) ([]DeviceInfo, error) {
36-
conf, err := config.Retrieve()
36+
conf, err := config.RetrieveCredentials()
3737
if err != nil {
3838
return nil, err
3939
}

command/device/listfrequency.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type FrequencyPlanInfo struct {
3434
// ListFrequencyPlans command is used to list
3535
// the supported LoRa frequency plans.
3636
func ListFrequencyPlans() ([]FrequencyPlanInfo, error) {
37-
conf, err := config.Retrieve()
37+
conf, err := config.RetrieveCredentials()
3838
if err != nil {
3939
return nil, err
4040
}
@@ -53,7 +53,7 @@ func ListFrequencyPlans() ([]FrequencyPlanInfo, error) {
5353
freq := FrequencyPlanInfo{
5454
Name: f.Name,
5555
ID: f.Id,
56-
Advanced: fmt.Sprintf("%v",f.Advanced),
56+
Advanced: fmt.Sprintf("%v", f.Advanced),
5757
}
5858
freqs = append(freqs, freq)
5959
}

command/ota/massupload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func MassUpload(params *MassUploadParams) ([]Result, error) {
7171
return nil, fmt.Errorf("%s: %w", "cannot generate .ota file", err)
7272
}
7373

74-
conf, err := config.Retrieve()
74+
conf, err := config.RetrieveCredentials()
7575
if err != nil {
7676
return nil, err
7777
}

command/ota/upload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type UploadParams struct {
4545
// Upload command is used to upload a firmware OTA,
4646
// on a device of Arduino IoT Cloud.
4747
func Upload(params *UploadParams) error {
48-
conf, err := config.Retrieve()
48+
conf, err := config.RetrieveCredentials()
4949
if err != nil {
5050
return err
5151
}

command/tag/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type CreateTagsParams struct {
3535
// CreateTags allows to create or overwrite tags
3636
// on a resource of Arduino IoT Cloud.
3737
func CreateTags(params *CreateTagsParams) error {
38-
conf, err := config.Retrieve()
38+
conf, err := config.RetrieveCredentials()
3939
if err != nil {
4040
return err
4141
}

command/tag/delete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type DeleteTagsParams struct {
3535
// DeleteTags command is used to delete tags of a device
3636
// from Arduino IoT Cloud.
3737
func DeleteTags(params *DeleteTagsParams) error {
38-
conf, err := config.Retrieve()
38+
conf, err := config.RetrieveCredentials()
3939
if err != nil {
4040
return err
4141
}

command/thing/bind.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type BindParams struct {
3333
// Bind command is used to bind a thing to a device
3434
// on Arduino IoT Cloud.
3535
func Bind(params *BindParams) error {
36-
conf, err := config.Retrieve()
36+
conf, err := config.RetrieveCredentials()
3737
if err != nil {
3838
return err
3939
}

command/thing/clone.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type CloneParams struct {
3333

3434
// Clone allows to create a new thing from an already existing one.
3535
func Clone(params *CloneParams) (*ThingInfo, error) {
36-
conf, err := config.Retrieve()
36+
conf, err := config.RetrieveCredentials()
3737
if err != nil {
3838
return nil, err
3939
}

command/thing/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type CreateParams struct {
3434

3535
// Create allows to create a new thing.
3636
func Create(params *CreateParams) (*ThingInfo, error) {
37-
conf, err := config.Retrieve()
37+
conf, err := config.RetrieveCredentials()
3838
if err != nil {
3939
return nil, err
4040
}

command/thing/delete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func Delete(params *DeleteParams) error {
4343
return errors.New("cannot use both ID and Tags. only one of them should be not nil")
4444
}
4545

46-
conf, err := config.Retrieve()
46+
conf, err := config.RetrieveCredentials()
4747
if err != nil {
4848
return err
4949
}

command/thing/extract.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type ExtractParams struct {
3434
// Extract command is used to extract a thing template
3535
// from a thing on Arduino IoT Cloud.
3636
func Extract(params *ExtractParams) (map[string]interface{}, error) {
37-
conf, err := config.Retrieve()
37+
conf, err := config.RetrieveCredentials()
3838
if err != nil {
3939
return nil, err
4040
}

command/thing/list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type ListParams struct {
3636
// List command is used to list
3737
// the things of Arduino IoT Cloud.
3838
func List(params *ListParams) ([]ThingInfo, error) {
39-
conf, err := config.Retrieve()
39+
conf, err := config.RetrieveCredentials()
4040
if err != nil {
4141
return nil, err
4242
}

0 commit comments

Comments
 (0)