@@ -34,77 +34,75 @@ import (
34
34
"github.com/spf13/viper"
35
35
)
36
36
37
- var initFlags struct {
37
+ type initFlags struct {
38
38
destDir string
39
39
overwrite bool
40
40
format string
41
41
}
42
42
43
43
func initInitCommand () * cobra.Command {
44
+ flags := & initFlags {}
44
45
initCommand := & cobra.Command {
45
46
Use : "init" ,
46
47
Short : "Initialize a credentials file" ,
47
48
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
+ },
49
55
}
50
56
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}" )
54
60
55
61
return initCommand
56
62
}
57
63
58
- func runInitCommand (cmd * cobra. Command , args [] string ) {
64
+ func runInitCommand (flags * initFlags ) error {
59
65
logrus .Info ("Initializing credentials file" )
60
66
61
67
// Get default destination directory if it's not passed
62
- if initFlags .destDir == "" {
68
+ if flags .destDir == "" {
63
69
credPath , err := arduino .DataDir ()
64
70
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 )
67
72
}
68
73
// Create arduino default directory if it does not exist
69
74
if credPath .NotExist () {
70
75
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 )
73
77
}
74
78
}
75
- initFlags .destDir = credPath .String ()
79
+ flags .destDir = credPath .String ()
76
80
}
77
81
78
82
// 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'" )
83
86
}
84
87
85
88
// 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 ()
87
90
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 )
90
92
}
91
93
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 )
94
95
}
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 )
100
99
}
101
100
102
101
// Take needed credentials starting an interactive mode
103
102
feedback .Print ("To obtain your API credentials visit https://create.arduino.cc/iot/integrations" )
104
103
id , key , err := paramsPrompt ()
105
104
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 )
108
106
}
109
107
110
108
// Write the credentials file
@@ -113,11 +111,11 @@ func runInitCommand(cmd *cobra.Command, args []string) {
113
111
newSettings .Set ("client" , id )
114
112
newSettings .Set ("secret" , key )
115
113
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 )
118
115
}
119
116
120
117
feedback .Printf ("Credentials file successfully initialized at: %s" , credFile )
118
+ return nil
121
119
}
122
120
123
121
func paramsPrompt () (id , key string , err error ) {
0 commit comments