Add more fields, default table format and --format custom format flag to config list command

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-08-09 16:32:03 -04:00
parent feae59a058
commit 648cd6651a
3 changed files with 57 additions and 3 deletions

View File

@ -2,6 +2,7 @@ FROM alpine:3
ENV PSU_AUTH_TOKEN="" \
PSU_CONFIG="" \
PSU_CONFIG_LIST_FORMAT="" \
PSU_ENDPOINT_GROUP_LIST_FORMAT="" \
PSU_ENDPOINT_LIST_FORMAT="" \
PSU_INSECURE="" \

View File

@ -29,7 +29,7 @@ var configCmd = &cobra.Command{
if !keyExists {
logrus.WithFields(logrus.Fields{
"key": args[0],
"suggestions": "Try looking up the available configuration keys: psu config ls --keys",
"suggestions": "Try looking up the available configuration keys: psu config ls",
}).Fatal("Unknown configuration key")
}

View File

@ -2,7 +2,12 @@ package cmd
import (
"fmt"
"os"
"sort"
"strings"
"text/template"
"github.com/greenled/portainer-stack-utils/common"
"github.com/spf13/viper"
@ -21,13 +26,61 @@ var configListCmd = &cobra.Command{
return keys[i] < keys[j]
})
// Create config objects
var configs []config
for _, key := range keys {
// List config key and value
fmt.Printf("%s: %v\n", key, viper.Get(key))
envvar := strings.Replace(key, "-", "_", -1)
envvar = strings.Replace(envvar, ".", "_", -1)
envvar = strings.ToUpper(envvar)
envvar = "PSU_" + envvar
configs = append(configs, config{
Key: key,
EnvironmentVariable: envvar,
CurrentValue: viper.Get(key),
})
}
if viper.GetString("config.list.format") != "" {
// Print configs with a custom format
template, templateParsingErr := template.New("configTpl").Parse(viper.GetString("config.list.format"))
common.CheckError(templateParsingErr)
for _, c := range configs {
templateExecutionErr := template.Execute(os.Stdout, c)
common.CheckError(templateExecutionErr)
fmt.Println()
}
} else {
// Print configs with a table format
writer, err := common.NewTabWriter([]string{
"KEY",
"ENV VAR",
"CURRENT VALUE",
})
common.CheckError(err)
for _, c := range configs {
_, err := fmt.Fprintln(writer, fmt.Sprintf(
"%s\t%s\t%v",
c.Key,
c.EnvironmentVariable,
c.CurrentValue,
))
common.CheckError(err)
}
flushErr := writer.Flush()
common.CheckError(flushErr)
}
},
}
func init() {
configCmd.AddCommand(configListCmd)
configListCmd.Flags().String("format", "", "Format output using a Go template.")
viper.BindPFlag("config.list.format", configListCmd.Flags().Lookup("format"))
}
type config struct {
Key string
EnvironmentVariable string
CurrentValue interface{}
}