mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Add more fields, default table format and --format custom format flag to config list command
This commit is contained in:
parent
feae59a058
commit
648cd6651a
@ -2,6 +2,7 @@ FROM alpine:3
|
|||||||
|
|
||||||
ENV PSU_AUTH_TOKEN="" \
|
ENV PSU_AUTH_TOKEN="" \
|
||||||
PSU_CONFIG="" \
|
PSU_CONFIG="" \
|
||||||
|
PSU_CONFIG_LIST_FORMAT="" \
|
||||||
PSU_ENDPOINT_GROUP_LIST_FORMAT="" \
|
PSU_ENDPOINT_GROUP_LIST_FORMAT="" \
|
||||||
PSU_ENDPOINT_LIST_FORMAT="" \
|
PSU_ENDPOINT_LIST_FORMAT="" \
|
||||||
PSU_INSECURE="" \
|
PSU_INSECURE="" \
|
||||||
|
@ -29,7 +29,7 @@ var configCmd = &cobra.Command{
|
|||||||
if !keyExists {
|
if !keyExists {
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"key": args[0],
|
"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")
|
}).Fatal("Unknown configuration key")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,12 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/greenled/portainer-stack-utils/common"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
|
||||||
@ -21,13 +26,61 @@ var configListCmd = &cobra.Command{
|
|||||||
return keys[i] < keys[j]
|
return keys[i] < keys[j]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Create config objects
|
||||||
|
var configs []config
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
// List config key and value
|
envvar := strings.Replace(key, "-", "_", -1)
|
||||||
fmt.Printf("%s: %v\n", key, viper.Get(key))
|
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() {
|
func init() {
|
||||||
configCmd.AddCommand(configListCmd)
|
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{}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user