Add --list flag to config command to list all available configuration keys

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-07-23 14:43:32 -04:00
parent 0723ae0eb8
commit 5664faf2dd
3 changed files with 67 additions and 49 deletions

View File

@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Added ### Added
- `config` command to get and set configuration options. - `config` command to get and set configuration options.
- `-l, --list` flag to list all available configuration keys.
- `endpoint list|ls` command to print the endpoints list as a table. - `endpoint list|ls` command to print the endpoints list as a table.
- `--format` flag to format output using a Go template. - `--format` flag to format output using a Go template.
- `stack list|ls` command to print the stacks list as a table. - `stack list|ls` command to print the stacks list as a table.

View File

@ -3,6 +3,7 @@ FROM alpine
ENV PSU_AUTHENTICATION_PASSWORD="" \ ENV PSU_AUTHENTICATION_PASSWORD="" \
PSU_AUTHENTICATION_USER="" \ PSU_AUTHENTICATION_USER="" \
PSU_CONFIG="" \ PSU_CONFIG="" \
PSU_CONFIG_LIST="" \
PSU_CONNECTION_INSECURE="" \ PSU_CONNECTION_INSECURE="" \
PSU_CONNECTION_TIMEOUT="" \ PSU_CONNECTION_TIMEOUT="" \
PSU_CONNECTION_URL="" \ PSU_CONNECTION_URL="" \

View File

@ -8,6 +8,7 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
"log" "log"
"os" "os"
"sort"
) )
// configCmd represents the config command // configCmd represents the config command
@ -15,63 +16,75 @@ var configCmd = &cobra.Command{
Use: "config KEY [VALUE]", Use: "config KEY [VALUE]",
Short: "Get and set configuration options", Short: "Get and set configuration options",
Example: "psu config user admin", Example: "psu config user admin",
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// Check if it's a valid key if viper.GetBool("config.list") {
var keyExists bool // List config keys
for _, key := range viper.AllKeys() { keys := viper.AllKeys()
if key == args[0] { sort.Slice(keys, func(i, j int) bool {
keyExists = true return keys[i] < keys[j]
break })
for _, key := range keys {
fmt.Println(key)
} }
}
if !keyExists {
log.Fatalf("Unkonwn configuration key \"%s\"", args[0])
}
// Create new viper
commandViper := viper.New()
// Set config file name
var configFile string
if viper.ConfigFileUsed() != "" {
// Use config file from viper
configFile = viper.ConfigFileUsed()
} else { } else {
// Find home directory if len(args) >= 1 {
home, err := homedir.Dir() // Check if it's a valid key
if err != nil { var keyExists bool
fmt.Println(err) for _, key := range viper.AllKeys() {
os.Exit(1) if key == args[0] {
} keyExists = true
break
}
}
if !keyExists {
log.Fatalf("Unkonwn configuration key \"%s\"", args[0])
}
// Use $HOME/.psu.yaml // Create new viper
configFile = fmt.Sprintf("%s%s.psu.yaml", home, string(os.PathSeparator)) commandViper := viper.New()
}
commandViper.SetConfigFile(configFile)
// Read config from file // Set config file name
if configReadingErr := commandViper.ReadInConfig(); configReadingErr != nil { var configFile string
common.PrintVerbose(fmt.Sprintf("Could not read configuration from \"%s\". Expect all configuration values to be unset.", configFile)) if viper.ConfigFileUsed() != "" {
} // Use config file from viper
configFile = viper.ConfigFileUsed()
} else {
// Find home directory
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if len(args) == 1 { // Use $HOME/.psu.yaml
// Get config configFile = fmt.Sprintf("%s%s.psu.yaml", home, string(os.PathSeparator))
fmt.Println(commandViper.Get(args[0])) }
} else { commandViper.SetConfigFile(configFile)
// Set config
commandViper.Set(args[0], args[1])
// Make sure the config file exists // Read config from file
_, fileCreationErr := os.Create(configFile) if configReadingErr := commandViper.ReadInConfig(); configReadingErr != nil {
if fileCreationErr != nil { common.PrintVerbose(fmt.Sprintf("Could not read configuration from \"%s\". Expect all configuration values to be unset.", configFile))
common.CheckError(fileCreationErr) }
}
// Write te config file if len(args) == 1 {
configWritingErr := commandViper.WriteConfig() // Get config
if configWritingErr != nil { fmt.Println(commandViper.Get(args[0]))
common.CheckError(configWritingErr) } else {
// Set config
commandViper.Set(args[0], args[1])
// Make sure the config file exists
_, fileCreationErr := os.Create(configFile)
if fileCreationErr != nil {
common.CheckError(fileCreationErr)
}
// Write te config file
configWritingErr := commandViper.WriteConfig()
if configWritingErr != nil {
common.CheckError(configWritingErr)
}
}
} }
} }
}, },
@ -79,4 +92,7 @@ var configCmd = &cobra.Command{
func init() { func init() {
rootCmd.AddCommand(configCmd) rootCmd.AddCommand(configCmd)
configCmd.Flags().BoolP("list", "l", false, "list all config keys")
viper.BindPFlag("config.list", configCmd.Flags().Lookup("list"))
} }