|
1 | 1 | package config |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "os" |
5 | | - "strings" |
6 | | - |
7 | | - "github.com/arduino/arduino-cli/cli/errorcodes" |
8 | | - "github.com/arduino/arduino-cli/cli/feedback" |
9 | | - paths "github.com/arduino/go-paths-helper" |
10 | | - "github.com/arduino/iot-cloud-cli/command/config" |
11 | | - "github.com/sirupsen/logrus" |
12 | 4 | "github.com/spf13/cobra" |
13 | | - "github.com/spf13/viper" |
14 | 5 | ) |
15 | 6 |
|
16 | | -var configFlags struct { |
17 | | - file string |
18 | | - client string |
19 | | - secret string |
20 | | -} |
21 | | - |
22 | 7 | func NewCommand() *cobra.Command { |
23 | 8 | configCommand := &cobra.Command{ |
24 | 9 | Use: "config", |
25 | | - Short: "Set the configuration file", |
26 | | - Long: "Set the configuration file to access Arduino IoT Cloud", |
27 | | - Run: runConfigCommand, |
28 | | - } |
29 | | - configCommand.Flags().StringVarP(&configFlags.file, "file", "f", "", "Existing configuration yaml file") |
30 | | - configCommand.Flags().StringVarP(&configFlags.client, "client", "c", "", "Client ID") |
31 | | - configCommand.Flags().StringVarP(&configFlags.secret, "secret", "s", "", "Secret ID") |
32 | | - return configCommand |
33 | | -} |
34 | | - |
35 | | -func runConfigCommand(cmd *cobra.Command, args []string) { |
36 | | - if configFlags.file == "" && (configFlags.client == "" || configFlags.secret == "") { |
37 | | - feedback.Error("Error during config: provide either a yaml file or credentials") |
38 | | - os.Exit(errorcodes.ErrGeneric) |
| 10 | + Short: "Configuration commands.", |
| 11 | + Long: "Configuration commands.", |
39 | 12 | } |
40 | 13 |
|
41 | | - conf := viper.New() |
| 14 | + configCommand.AddCommand(initInitCommand()) |
42 | 15 |
|
43 | | - if configFlags.file != "" { |
44 | | - file := paths.New(configFlags.file) |
45 | | - filename := strings.TrimSuffix(file.String(), file.Ext()) |
46 | | - conf.SetConfigName(filename) |
47 | | - conf.SetConfigType(strings.Trim(file.Ext(), ".")) |
48 | | - conf.AddConfigPath(".") |
49 | | - err := conf.ReadInConfig() |
50 | | - if err != nil { |
51 | | - feedback.Errorf("Error during config: fatal error config file: %v", err) |
52 | | - os.Exit(errorcodes.ErrGeneric) |
53 | | - } |
54 | | - |
55 | | - } else { |
56 | | - conf.BindPFlag("client", cmd.Flag("client")) |
57 | | - conf.BindPFlag("secret", cmd.Flag("secret")) |
58 | | - } |
59 | | - |
60 | | - err := config.Config(conf) |
61 | | - if err != nil { |
62 | | - feedback.Errorf("Error during config: storing config file: %v", err) |
63 | | - os.Exit(errorcodes.ErrGeneric) |
64 | | - } |
65 | | - |
66 | | - logrus.Info("Configuration file updated") |
| 16 | + return configCommand |
67 | 17 | } |
0 commit comments