-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlist.go
94 lines (82 loc) · 2.54 KB
/
list.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package list
import (
"fmt"
"slices"
"sort"
"strconv"
"strings"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "Lists the current CLI configuration values",
Long: fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s\n%s",
"Lists the current CLI configuration values, based on the following sources (in order of precedence):",
"- Environment variable",
` The environment variable is the name of the setting, with underscores ("_") instead of dashes ("-") and the "STACKIT" prefix.`,
" Example: you can set the project ID by setting the environment variable STACKIT_PROJECT_ID.",
"- Configuration set in CLI",
` These are set using the "stackit config set" command`,
` Example: you can set the project ID by running "stackit config set --project-id xxx"`,
),
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`List your active configuration`,
"$ stackit config list"),
),
RunE: func(cmd *cobra.Command, args []string) error {
err := viper.ReadInConfig()
if err != nil {
return fmt.Errorf("read config file: %w", err)
}
configData := viper.AllSettings()
// Sort the config options by key
configKeys := make([]string, 0, len(configData))
for k := range configData {
configKeys = append(configKeys, k)
}
sort.Strings(configKeys)
table := tables.NewTable()
table.SetHeader("NAME", "VALUE")
for _, key := range configKeys {
value := configData[key]
// Convert value to string
// (Assuming value is either string or bool)
valueString, ok := value.(string)
if !ok {
valueBool, ok := value.(bool)
if !ok {
continue
}
valueString = strconv.FormatBool(valueBool)
}
// Don't show unset values
if valueString == "" {
continue
}
// Don't show unsupported (deprecated or user-inputted) configuration options
// that might be present in the config file
if !slices.Contains(config.ConfigKeys, key) {
continue
}
// Replace "_" with "-" to match the flags
key = strings.ReplaceAll(key, "_", "-")
table.AddRow(key, valueString)
table.AddSeparator()
}
err = table.Display(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}
return nil
},
}
return cmd
}