Skip to content

Commit

Permalink
config: Add Get command (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
craftamap committed Sep 10, 2021
1 parent b038254 commit 8db0f74
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 75 deletions.
198 changes: 123 additions & 75 deletions cmd/commands/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/craftamap/bb/cmd/options"
"github.com/craftamap/bb/config"
Expand All @@ -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)
}
},
}
Expand All @@ -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))
}
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -72,6 +73,7 @@ var BbConfigurationValidation Configuration = map[string]Entry{
},
CONFIG_KEY_AUTH_PASSWORD: {
Validator: SimpleStringValidator(),
Hidden: true,
},
CONFIG_KEY_GIT_REMOTE: {
Validator: SimpleStringValidator(),
Expand Down

0 comments on commit 8db0f74

Please sign in to comment.