psu/cmd/configList.go
Juan Carlos Mejías Rodríguez c4e2f0145f Enhance flag descriptions
2019-08-08 11:56:07 -04:00

42 lines
861 B
Go

package cmd
import (
"fmt"
"sort"
"github.com/spf13/viper"
"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]
})
for _, key := range keys {
if viper.GetBool("config.list.keys") {
// List config key
fmt.Println(key)
} else {
// List config key and value
fmt.Printf("%s: %v\n", key, viper.Get(key))
}
}
},
}
func init() {
configCmd.AddCommand(configListCmd)
configListCmd.Flags().Bool("keys", false, "Only list keys.")
viper.BindPFlag("config.list.keys", configListCmd.Flags().Lookup("keys"))
}