diff --git a/cmd/commands/config/config.go b/cmd/commands/config/config.go index b37ab09..af4e0c9 100644 --- a/cmd/commands/config/config.go +++ b/cmd/commands/config/config.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "path/filepath" - "strings" "github.com/craftamap/bb/cmd/options" "github.com/craftamap/bb/config" @@ -31,81 +30,9 @@ func Add(rootCmd *cobra.Command, _ *options.GlobalOptions) { }, Run: func(_ *cobra.Command, args []string) { if Get { - // TODO: code here + GetValue(args) } else { - key := args[0] - inputValue := args[1] - - newValue, err := config.BbConfigurationValidation.ValidateEntry(key, inputValue) - - if err != nil { - logging.Error(fmt.Sprintf("failed to validate %s: %s", inputValue, err)) - return - } - - var configDirectory string - var filename string - if Local { - var err error - configDirectory, filename, err = config.GetLocalConfigurationPath() - if err != nil { - logging.Error(err) - return - } - } else { - configDirectory, filename = config.GetGlobalConfigurationPath() - } - - // If the directory does not exist, something is off: - // - The global configuration directory get's created in root - // - The local configuration directory is a repository, which always exists - if _, err := os.Stat(configDirectory); os.IsNotExist(err) { - logging.Error(fmt.Sprintf("Expected directory \"%s\", but the directory does not exist", configDirectory)) - return - } - path := filepath.Join(configDirectory, filename) - // If the config itself does not exist, it's fine (although weird for global) - we create it now - if _, err := os.Stat(path); os.IsNotExist(err) { - logging.Note(fmt.Sprintf("Creating config file %s", path)) - fh, err := os.Create(path) - if err != nil { - logging.Error(fmt.Sprintf("Unable to create file %s", path)) - } - fh.Close() - } - - logging.Debugf("Config file path: %s", path) - - tmpVp, err := config.GetViperForPath(path) - if err != nil { - logging.Error(err) - return - } - - isSetAlready := tmpVp.IsSet(key) - oldValue := tmpVp.Get(key) - - if isSetAlready { - // Don't print old password values - if strings.ToLower(key) == config.CONFIG_KEY_AUTH_PASSWORD { - oldValue = "(truncated)" - } - logging.Warning(fmt.Sprintf("\"%s\" is already set. This will overwrite the value of \"%s\" from \"%s\" to \"%s\".", key, key, oldValue, newValue)) - } - - logging.Note(fmt.Sprintf("Setting \"%s\" to \"%s\" in %s", key, newValue, path)) - logging.Debugf("%+v", tmpVp.AllSettings()) - - tmpVp.Set(key, newValue) - logging.Debugf("%+v", tmpVp.AllSettings()) - - err = config.WriteViper(tmpVp, path) - if err != nil { - logging.Error(err) - return - } - - logging.SuccessExclamation(fmt.Sprintf("Successfully updated configuration %s", path)) + SetValue(args) } }, } @@ -115,3 +42,124 @@ func Add(rootCmd *cobra.Command, _ *options.GlobalOptions) { rootCmd.AddCommand(&configCommand) } + +func SetValue(args []string) { + key := args[0] + inputValue := args[1] + + newValue, err := config.BbConfigurationValidation.ValidateEntry(key, inputValue) + + if err != nil { + logging.Error(fmt.Sprintf("failed to validate %s: %s", inputValue, err)) + return + } + + var configDirectory string + var filename string + if Local { + var err error + configDirectory, filename, err = config.GetLocalConfigurationPath() + if err != nil { + logging.Error(err) + return + } + } else { + configDirectory, filename = config.GetGlobalConfigurationPath() + } + + // If the directory does not exist, something is off: + // - The global configuration directory get's created in root + // - The local configuration directory is a repository, which always exists + if _, err := os.Stat(configDirectory); os.IsNotExist(err) { + logging.Error(fmt.Sprintf("Expected directory \"%s\", but the directory does not exist", configDirectory)) + return + } + path := filepath.Join(configDirectory, filename) + // If the config itself does not exist, it's fine (although weird for global) - we create it now + if _, err := os.Stat(path); os.IsNotExist(err) { + logging.Note(fmt.Sprintf("Creating config file %s", path)) + fh, err := os.Create(path) + if err != nil { + logging.Error(fmt.Sprintf("Unable to create file %s", path)) + } + fh.Close() + } + + logging.Debugf("Config file path: %s", path) + + tmpVp, err := config.GetViperForPath(path) + if err != nil { + logging.Error(err) + return + } + + isSetAlready := tmpVp.IsSet(key) + oldValue := tmpVp.Get(key) + + if isSetAlready { + // Don't print old password values + if config.BbConfigurationValidation[key].Hidden { + oldValue = "(hidden)" + } + logging.Warning(fmt.Sprintf("\"%s\" is already set. This will overwrite the value of \"%s\" from \"%s\" to \"%s\".", key, key, oldValue, newValue)) + } + + logging.Note(fmt.Sprintf("Setting \"%s\" to \"%s\" in %s", key, newValue, path)) + logging.Debugf("%+v", tmpVp.AllSettings()) + + tmpVp.Set(key, newValue) + logging.Debugf("%+v", tmpVp.AllSettings()) + + err = config.WriteViper(tmpVp, path) + if err != nil { + logging.Error(err) + return + } + + logging.SuccessExclamation(fmt.Sprintf("Successfully updated configuration %s", path)) +} + +func GetValue(args []string) { + key := args[0] + + entry, ok := config.BbConfigurationValidation[key] + if !ok { + logging.Warning("\"%s\" is not a valid key", key) + return + } + + var configDirectory string + var filename string + if Local { + var err error + configDirectory, filename, err = config.GetLocalConfigurationPath() + if err != nil { + logging.Error(err) + return + } + } else { + configDirectory, filename = config.GetGlobalConfigurationPath() + } + + path := filepath.Join(configDirectory, filename) + if _, err := os.Stat(path); os.IsNotExist(err) { + logging.Error(fmt.Sprintf("config file %s does not exist yet", path)) + return + } + + tmpVp, err := config.GetViperForPath(path) + if err != nil { + logging.Error(err) + return + } + value := tmpVp.Get(key) + if value == nil { + logging.Warning(fmt.Sprintf("%s is not set yet.", key)) + return + } + if entry.Hidden { + value = "(hidden)" + } + + logging.Success(fmt.Sprintf("%s = %s", key, value)) +} diff --git a/config/config.go b/config/config.go index 78b70b7..2fa3780 100644 --- a/config/config.go +++ b/config/config.go @@ -62,6 +62,7 @@ func SimpleStringValidator() Validator { // Entry contains all the data required for Validation and Convertion. type Entry struct { Validator Validator + Hidden bool } type Configuration map[string]Entry @@ -72,6 +73,7 @@ var BbConfigurationValidation Configuration = map[string]Entry{ }, CONFIG_KEY_AUTH_PASSWORD: { Validator: SimpleStringValidator(), + Hidden: true, }, CONFIG_KEY_GIT_REMOTE: { Validator: SimpleStringValidator(),