-
-
Notifications
You must be signed in to change notification settings - Fork 4
Refactor config commands #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,17 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"github.com/arduino/arduino-cli/cli/errorcodes" | ||
"github.com/arduino/arduino-cli/cli/feedback" | ||
paths "github.com/arduino/go-paths-helper" | ||
"github.com/arduino/iot-cloud-cli/command/config" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var configFlags struct { | ||
file string | ||
client string | ||
secret string | ||
} | ||
|
||
func NewCommand() *cobra.Command { | ||
configCommand := &cobra.Command{ | ||
Use: "config", | ||
Short: "Set the configuration file", | ||
Long: "Set the configuration file to access Arduino IoT Cloud", | ||
Run: runConfigCommand, | ||
} | ||
configCommand.Flags().StringVarP(&configFlags.file, "file", "f", "", "Existing configuration yaml file") | ||
configCommand.Flags().StringVarP(&configFlags.client, "client", "c", "", "Client ID") | ||
configCommand.Flags().StringVarP(&configFlags.secret, "secret", "s", "", "Secret ID") | ||
return configCommand | ||
} | ||
|
||
func runConfigCommand(cmd *cobra.Command, args []string) { | ||
if configFlags.file == "" && (configFlags.client == "" || configFlags.secret == "") { | ||
feedback.Error("Error during config: provide either a yaml file or credentials") | ||
os.Exit(errorcodes.ErrGeneric) | ||
Short: "Configuration commands.", | ||
Long: "Configuration commands.", | ||
} | ||
|
||
conf := viper.New() | ||
configCommand.AddCommand(initInitCommand()) | ||
|
||
if configFlags.file != "" { | ||
file := paths.New(configFlags.file) | ||
filename := strings.TrimSuffix(file.String(), file.Ext()) | ||
conf.SetConfigName(filename) | ||
conf.SetConfigType(strings.Trim(file.Ext(), ".")) | ||
conf.AddConfigPath(".") | ||
err := conf.ReadInConfig() | ||
if err != nil { | ||
feedback.Errorf("Error during config: fatal error config file: %v", err) | ||
os.Exit(errorcodes.ErrGeneric) | ||
} | ||
|
||
} else { | ||
conf.BindPFlag("client", cmd.Flag("client")) | ||
conf.BindPFlag("secret", cmd.Flag("secret")) | ||
} | ||
|
||
err := config.Config(conf) | ||
if err != nil { | ||
feedback.Errorf("Error during config: storing config file: %v", err) | ||
os.Exit(errorcodes.ErrGeneric) | ||
} | ||
|
||
logrus.Info("Configuration file updated") | ||
return configCommand | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/arduino/arduino-cli/cli/errorcodes" | ||
"github.com/arduino/arduino-cli/cli/feedback" | ||
"github.com/arduino/iot-cloud-cli/command/config" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var initFlags struct { | ||
destDir string | ||
overwrite bool | ||
format string | ||
} | ||
|
||
func initInitCommand() *cobra.Command { | ||
initCommand := &cobra.Command{ | ||
Use: "init", | ||
Short: "Initialize a configuration file with default values", | ||
Long: "Initialize an Arduino IoT Cloud CLI configuration file with default values", | ||
Run: runInitCommand, | ||
} | ||
|
||
initCommand.Flags().StringVar(&initFlags.destDir, "dest-dir", ".", "Sets where to save the configuration file.") | ||
initCommand.Flags().BoolVar(&initFlags.overwrite, "overwrite", false, "Overwrite existing config file.") | ||
initCommand.Flags().StringVar(&initFlags.format, "config-format", "yaml", "Format of the configuration file, can be {yaml|json}") | ||
|
||
return initCommand | ||
} | ||
|
||
func runInitCommand(cmd *cobra.Command, args []string) { | ||
logrus.Infof("Initializing a config file in folder: %s", initFlags.destDir) | ||
|
||
params := &config.InitParams{ | ||
DestDir: initFlags.destDir, | ||
Overwrite: initFlags.overwrite, | ||
Format: initFlags.format, | ||
} | ||
|
||
err := config.Init(params) | ||
if err != nil { | ||
feedback.Errorf("Error during config init: %v", err) | ||
os.Exit(errorcodes.ErrGeneric) | ||
} | ||
|
||
logrus.Info("Config file successfully initialized") | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/arduino/go-paths-helper" | ||
"github.com/arduino/iot-cloud-cli/internal/config" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// InitParams contains the parameters needed to initialize a configuration file. | ||
// DestDir - destination directory in which the configuration file will be saved. | ||
// Overwrite - specify if existing config file should be overwritten. | ||
// Format - the config file format, can be 'json' or 'yaml'. | ||
type InitParams struct { | ||
DestDir string | ||
Overwrite bool | ||
Format string | ||
} | ||
|
||
func validateFormatString(arg string) error { | ||
if arg != "json" && arg != "yaml" { | ||
return errors.New("passed format is not valid, select between 'json' and 'yaml'") | ||
} | ||
return nil | ||
} | ||
polldo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Init initializes a configuration file with default values. | ||
// If the file doesn't exist, it is created. | ||
// If it exists, it is written to only if overwrite param is true. | ||
func Init(params *InitParams) error { | ||
configPath, err := paths.New(params.DestDir).Abs() | ||
if err != nil { | ||
return fmt.Errorf("%s: %w", "cannot retrieve absolute path of passed dest-dir", err) | ||
} | ||
if !configPath.IsDir() { | ||
return fmt.Errorf("%s: %w", "passed dest-dir is not a valid directory", err) | ||
} | ||
|
||
params.Format = strings.ToLower(params.Format) | ||
if err := validateFormatString(params.Format); err != nil { | ||
return err | ||
} | ||
|
||
configFile := configPath.Join(config.Filename + "." + params.Format) | ||
|
||
if !params.Overwrite && configFile.Exist() { | ||
return errors.New("config file already exists, use --overwrite to discard the existing one") | ||
} | ||
|
||
newSettings := viper.New() | ||
config.SetDefaults(newSettings) | ||
if err := newSettings.WriteConfigAs(configFile.String()); err != nil { | ||
return fmt.Errorf("cannot create config file: %v", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package config | ||
|
||
import "github.com/spf13/viper" | ||
|
||
var ( | ||
Filename = "arduino-cloud" | ||
) | ||
|
||
// SetDefaults sets the default values for configuration keys | ||
func SetDefaults(settings *viper.Viper) { | ||
// Client ID | ||
settings.SetDefault("client", "xxxxxxxxxxxxxx") | ||
// Secret | ||
settings.SetDefault("secret", "xxxxxxxxxxxxxx") | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.