Move config listing to new command config list

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-07-23 20:02:58 -04:00
parent 5d723af9fd
commit 1ca444a79d
4 changed files with 82 additions and 67 deletions

View File

@ -7,7 +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. - `config list|ls` command to list configuration options.
- `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,7 +3,6 @@ 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,7 +8,6 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
"log" "log"
"os" "os"
"sort"
) )
// configCmd represents the config command // configCmd represents the config command
@ -16,18 +15,8 @@ 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.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if viper.GetBool("config.list") {
// List config keys
keys := viper.AllKeys()
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
for _, key := range keys {
fmt.Println(key)
}
} else {
if len(args) >= 1 {
// Check if it's a valid key // Check if it's a valid key
var keyExists bool var keyExists bool
for _, key := range viper.AllKeys() { for _, key := range viper.AllKeys() {
@ -85,14 +74,9 @@ var configCmd = &cobra.Command{
common.CheckError(configWritingErr) common.CheckError(configWritingErr)
} }
} }
}
}
}, },
} }
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"))
} }

32
cmd/configList.go Normal file
View File

@ -0,0 +1,32 @@
package cmd
import (
"fmt"
"github.com/spf13/viper"
"sort"
"github.com/spf13/cobra"
)
// configListCmd represents the list command
var configListCmd = &cobra.Command{
Use: "list",
Short: "List configs",
Aliases: []string{"ls"},
Run: func(cmd *cobra.Command, args []string) {
// Get alphabetically ordered list of config keys
keys := viper.AllKeys()
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
// List keys and values
for _, key := range keys {
fmt.Printf("%s: %v\n", key, viper.Get(key))
}
},
}
func init() {
configCmd.AddCommand(configListCmd)
}