@@ -34,77 +34,75 @@ import (
3434 "github.com/spf13/viper"
3535)
3636
37- var initFlags struct {
37+ type initFlags struct {
3838 destDir string
3939 overwrite bool
4040 format string
4141}
4242
4343func initInitCommand () * cobra.Command {
44+ flags := & initFlags {}
4445 initCommand := & cobra.Command {
4546 Use : "init" ,
4647 Short : "Initialize a credentials file" ,
4748 Long : "Initialize an Arduino IoT Cloud CLI credentials file" ,
48- Run : runInitCommand ,
49+ Run : func (cmd * cobra.Command , args []string ) {
50+ if err := runInitCommand (flags ); err != nil {
51+ feedback .Errorf ("Error during credentials init: %v" , err )
52+ os .Exit (errorcodes .ErrGeneric )
53+ }
54+ },
4955 }
5056
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}" )
57+ initCommand .Flags ().StringVar (& flags .destDir , "dest-dir" , "" , "Sets where to save the credentials file" )
58+ initCommand .Flags ().BoolVar (& flags .overwrite , "overwrite" , false , "Overwrite existing credentials file" )
59+ initCommand .Flags ().StringVar (& flags .format , "file-format" , "yaml" , "Format of the credentials file, can be {yaml|json}" )
5460
5561 return initCommand
5662}
5763
58- func runInitCommand (cmd * cobra. Command , args [] string ) {
64+ func runInitCommand (flags * initFlags ) error {
5965 logrus .Info ("Initializing credentials file" )
6066
6167 // Get default destination directory if it's not passed
62- if initFlags .destDir == "" {
68+ if flags .destDir == "" {
6369 credPath , err := arduino .DataDir ()
6470 if err != nil {
65- feedback .Errorf ("Error during credentials init: cannot retrieve arduino default directory: %v" , err )
66- os .Exit (errorcodes .ErrGeneric )
71+ return fmt .Errorf ("cannot retrieve arduino default directory: %w" , err )
6772 }
6873 // Create arduino default directory if it does not exist
6974 if credPath .NotExist () {
7075 if err = credPath .MkdirAll (); err != nil {
71- feedback .Errorf ("Error during credentials init: cannot create arduino default directory %s: %v" , credPath , err )
72- os .Exit (errorcodes .ErrGeneric )
76+ return fmt .Errorf ("cannot create arduino default directory %s: %w" , credPath , err )
7377 }
7478 }
75- initFlags .destDir = credPath .String ()
79+ flags .destDir = credPath .String ()
7680 }
7781
7882 // Validate format flag
79- initFlags .format = strings .ToLower (initFlags .format )
80- if initFlags .format != "json" && initFlags .format != "yaml" {
81- feedback .Error ("Error during credentials init: format is not valid, provide 'json' or 'yaml'" )
82- os .Exit (errorcodes .ErrGeneric )
83+ flags .format = strings .ToLower (flags .format )
84+ if flags .format != "json" && flags .format != "yaml" {
85+ return fmt .Errorf ("format is not valid, provide 'json' or 'yaml'" )
8386 }
8487
8588 // Check that the destination directory is valid and build the credentials file path
86- credPath , err := paths .New (initFlags .destDir ).Abs ()
89+ credPath , err := paths .New (flags .destDir ).Abs ()
8790 if err != nil {
88- feedback .Errorf ("Error during credentials init: cannot retrieve absolute path of %s: %v" , initFlags .destDir , err )
89- os .Exit (errorcodes .ErrGeneric )
91+ return fmt .Errorf ("cannot retrieve absolute path of %s: %w" , flags .destDir , err )
9092 }
9193 if ! credPath .IsDir () {
92- feedback .Errorf ("Error during credentials init: %s is not a valid directory" , credPath )
93- os .Exit (errorcodes .ErrGeneric )
94+ return fmt .Errorf ("%s is not a valid directory" , credPath )
9495 }
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 )
99- os .Exit (errorcodes .ErrGeneric )
96+ credFile := credPath .Join (config .CredentialsFilename + "." + flags .format )
97+ if ! flags .overwrite && credFile .Exist () {
98+ return fmt .Errorf ("%s already exists, use '--overwrite' to overwrite it" , credFile )
10099 }
101100
102101 // Take needed credentials starting an interactive mode
103102 feedback .Print ("To obtain your API credentials visit https://create.arduino.cc/iot/integrations" )
104103 id , key , err := paramsPrompt ()
105104 if err != nil {
106- feedback .Errorf ("Error during credentials init: cannot take credentials params: %v" , err )
107- os .Exit (errorcodes .ErrGeneric )
105+ return fmt .Errorf ("cannot take credentials params: %w" , err )
108106 }
109107
110108 // Write the credentials file
@@ -113,11 +111,11 @@ func runInitCommand(cmd *cobra.Command, args []string) {
113111 newSettings .Set ("client" , id )
114112 newSettings .Set ("secret" , key )
115113 if err := newSettings .WriteConfigAs (credFile .String ()); err != nil {
116- feedback .Errorf ("Error during credentials init: cannot write credentials file: %v" , err )
117- os .Exit (errorcodes .ErrGeneric )
114+ return fmt .Errorf ("cannot write credentials file: %w" , err )
118115 }
119116
120117 feedback .Printf ("Credentials file successfully initialized at: %s" , credFile )
118+ return nil
121119}
122120
123121func paramsPrompt () (id , key string , err error ) {
0 commit comments