-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_get.go
70 lines (60 loc) · 1.85 KB
/
config_get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package cmd
import (
"fmt"
"sort"
"github.com/buonotti/apisense/log"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/buonotti/apisense/errors"
"github.com/buonotti/apisense/util"
)
var configGetCmd = &cobra.Command{
Use: "get",
Short: "Get a configuration value",
Long: `Get a configuration value`, // TODO: Add more info
Run: func(cmd *cobra.Command, _ []string) {
key := cmd.Flag("key").Value.String()
if key == "" {
allKeys := viper.AllKeys()
sort.Strings(allKeys)
keyLengths := util.Map(allKeys, func(key string) int { return len(key) })
maxKeyLength := util.Max(keyLengths)
for _, key := range allKeys {
printConfigValue(key, maxKeyLength)
}
} else {
printConfigValue(key, len(key))
}
},
}
func printConfigValue(key string, maxKeyLength int) {
val := viper.Get(key)
key = util.Pad(key, maxKeyLength)
styledKey := lipgloss.NewStyle().Bold(true).Render(fmt.Sprintf("%s = ", key))
styledVal := yellowStyle().Render(fmt.Sprintf("%v", val))
switch val := val.(type) {
case bool:
if val {
styledVal = greenStyle().Render(fmt.Sprintf("%v", val))
} else {
styledVal = redStyle().Render(fmt.Sprintf("%v", val))
}
case int64:
styledVal = blueStyle().Render(fmt.Sprintf("%v", val))
case float64:
styledVal = blueStyle().Render(fmt.Sprintf("%v", val))
case string:
if val == "" {
val = "<empty>"
styledVal = greyedOutStyle().Italic(true).Render(fmt.Sprintf("%v", val))
}
}
log.CliLogger.Infof("%s%s", styledKey, styledVal)
}
func init() {
configGetCmd.Flags().StringP("key", "k", "", "The key to get")
err := configGetCmd.RegisterFlagCompletionFunc("key", validConfigKeysFunc())
errors.CheckErr(errors.SafeWrap(errors.CannotRegisterCompletionFunction, err, "cannot register completion function for config get"))
configCmd.AddCommand(configGetCmd)
}