|
15 | 15 | // You should have received a copy of the GNU Affero General Public License |
16 | 16 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
17 | 17 |
|
18 | | -package config |
| 18 | +package credentials |
19 | 19 |
|
20 | 20 | import ( |
21 | 21 | "errors" |
@@ -43,81 +43,81 @@ var initFlags struct { |
43 | 43 | func initInitCommand() *cobra.Command { |
44 | 44 | initCommand := &cobra.Command{ |
45 | 45 | 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", |
48 | 48 | Run: runInitCommand, |
49 | 49 | } |
50 | 50 |
|
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}") |
54 | 54 |
|
55 | 55 | return initCommand |
56 | 56 | } |
57 | 57 |
|
58 | 58 | func runInitCommand(cmd *cobra.Command, args []string) { |
59 | | - logrus.Info("Initializing config file") |
| 59 | + logrus.Info("Initializing credentials file") |
60 | 60 |
|
61 | 61 | // Get default destination directory if it's not passed |
62 | 62 | if initFlags.destDir == "" { |
63 | | - configPath, err := arduino.DataDir() |
| 63 | + credPath, err := arduino.DataDir() |
64 | 64 | 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) |
66 | 66 | os.Exit(errorcodes.ErrGeneric) |
67 | 67 | } |
68 | 68 | // 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) |
72 | 72 | os.Exit(errorcodes.ErrGeneric) |
73 | 73 | } |
74 | 74 | } |
75 | | - initFlags.destDir = configPath.String() |
| 75 | + initFlags.destDir = credPath.String() |
76 | 76 | } |
77 | 77 |
|
78 | 78 | // Validate format flag |
79 | 79 | initFlags.format = strings.ToLower(initFlags.format) |
80 | 80 | 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'") |
82 | 82 | os.Exit(errorcodes.ErrGeneric) |
83 | 83 | } |
84 | 84 |
|
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() |
87 | 87 | 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) |
89 | 89 | os.Exit(errorcodes.ErrGeneric) |
90 | 90 | } |
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) |
93 | 93 | os.Exit(errorcodes.ErrGeneric) |
94 | 94 | } |
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) |
99 | 99 | os.Exit(errorcodes.ErrGeneric) |
100 | 100 | } |
101 | 101 |
|
102 | | - // Take needed configuration parameters starting an interactive mode |
| 102 | + // Take needed credentials starting an interactive mode |
103 | 103 | feedback.Print("To obtain your API credentials visit https://create.arduino.cc/iot/integrations") |
104 | 104 | id, key, err := paramsPrompt() |
105 | 105 | 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) |
107 | 107 | os.Exit(errorcodes.ErrGeneric) |
108 | 108 | } |
109 | 109 |
|
110 | | - // Write the configuration file |
| 110 | + // Write the credentials file |
111 | 111 | newSettings := viper.New() |
112 | 112 | newSettings.SetConfigPermissions(os.FileMode(0600)) |
113 | 113 | newSettings.Set("client", id) |
114 | 114 | 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) |
117 | 117 | os.Exit(errorcodes.ErrGeneric) |
118 | 118 | } |
119 | 119 |
|
120 | | - feedback.Printf("Config file successfully initialized at: %s", configFile) |
| 120 | + feedback.Printf("Credentials file successfully initialized at: %s", credFile) |
121 | 121 | } |
122 | 122 |
|
123 | 123 | func paramsPrompt() (id, key string, err error) { |
|
0 commit comments