-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconfig.go
More file actions
251 lines (217 loc) · 6.65 KB
/
config.go
File metadata and controls
251 lines (217 loc) · 6.65 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package cmd
import (
"fmt"
"io"
"strings"
"time"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"github.com/timescale/tiger-cli/internal/tiger/config"
"github.com/timescale/tiger-cli/internal/tiger/logging"
"github.com/timescale/tiger-cli/internal/tiger/util"
)
func buildConfigShowCmd() *cobra.Command {
var output string
var noDefaults bool
var withEnv bool
cmd := &cobra.Command{
Use: "show",
Short: "Show current configuration",
Long: `Display the current CLI configuration settings`,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
// Use flag value if provided, otherwise use config value
outputFormat := cfg.Output
if cmd.Flags().Changed("output") {
outputFormat = output
}
configFile, err := cfg.EnsureConfigDir()
if err != nil {
return err
}
// a new viper, free from env and cli flags
v := viper.New()
v.SetConfigFile(configFile)
if withEnv {
config.ApplyEnvOverrides(v)
}
if !noDefaults {
config.ApplyDefaults(v)
}
if err := config.ReadInConfig(v); err != nil {
return err
}
cfgOut, err := config.ForOutputFromViper(v)
if err != nil {
return err
}
if *cfgOut.ConfigDir == config.GetDefaultConfigDir() {
cfgOut.ConfigDir = nil
}
output := cmd.OutOrStdout()
switch outputFormat {
case "json":
return util.SerializeToJSON(output, cfgOut)
case "yaml":
return util.SerializeToYAML(output, cfgOut, false)
default:
return outputTable(output, cfgOut)
}
},
}
cmd.Flags().VarP((*outputFlag)(&output), "output", "o", "output format (json, yaml, table)")
cmd.Flags().BoolVar(&noDefaults, "no-defaults", false, "do not show default values for unset fields")
cmd.Flags().BoolVar(&withEnv, "with-env", false, "apply environment variable overrides")
return cmd
}
func buildConfigSetCmd() *cobra.Command {
return &cobra.Command{
Use: "set <key> <value>",
Short: "Set configuration value",
Long: `Set a configuration value and save it to ~/.config/tiger/config.yaml`,
Args: cobra.ExactArgs(2),
ValidArgsFunction: configOptionCompletion,
RunE: func(cmd *cobra.Command, args []string) error {
key, value := args[0], args[1]
cmd.SilenceUsage = true
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
if err := cfg.Set(key, value); err != nil {
return fmt.Errorf("failed to set config: %w", err)
}
logging.Info("Configuration updated", zap.String("key", key), zap.String("value", value))
fmt.Fprintf(cmd.OutOrStdout(), "Set %s = %s\n", key, value)
return nil
},
}
}
func buildConfigUnsetCmd() *cobra.Command {
return &cobra.Command{
Use: "unset <key>",
Short: "Remove configuration value",
Long: `Remove a configuration value and save changes to ~/.config/tiger/config.yaml`,
Args: cobra.ExactArgs(1),
ValidArgsFunction: configOptionCompletion,
RunE: func(cmd *cobra.Command, args []string) error {
key := args[0]
cmd.SilenceUsage = true
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
if err := cfg.Unset(key); err != nil {
return fmt.Errorf("failed to unset config: %w", err)
}
logging.Info("Configuration updated", zap.String("key", key))
fmt.Fprintf(cmd.OutOrStdout(), "Unset %s\n", key)
return nil
},
}
}
func buildConfigResetCmd() *cobra.Command {
return &cobra.Command{
Use: "reset",
Short: "Reset to defaults",
Long: `Reset all configuration settings to their default values`,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
if err := cfg.Reset(); err != nil {
return fmt.Errorf("failed to reset config: %w", err)
}
logging.Info("Configuration reset to defaults")
fmt.Fprintln(cmd.OutOrStdout(), "Configuration reset to defaults")
return nil
},
}
}
func buildConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Manage CLI configuration",
Long: `Manage CLI configuration settings stored in ~/.config/tiger/config.yaml`,
}
cmd.AddCommand(buildConfigShowCmd())
cmd.AddCommand(buildConfigSetCmd())
cmd.AddCommand(buildConfigUnsetCmd())
cmd.AddCommand(buildConfigResetCmd())
return cmd
}
func outputTable(w io.Writer, cfg *config.ConfigOutput) error {
table := tablewriter.NewWriter(w)
table.Header("PROPERTY", "VALUE")
if cfg.APIURL != nil {
table.Append("api_url", *cfg.APIURL)
}
if cfg.Analytics != nil {
table.Append("analytics", fmt.Sprintf("%t", *cfg.Analytics))
}
if cfg.ConfigDir != nil {
table.Append("config_dir", *cfg.ConfigDir)
}
if cfg.ConsoleURL != nil {
table.Append("console_url", *cfg.ConsoleURL)
}
if cfg.Debug != nil {
table.Append("debug", fmt.Sprintf("%t", *cfg.Debug))
}
if cfg.DocsMCP != nil {
table.Append("docs_mcp", fmt.Sprintf("%t", *cfg.DocsMCP))
}
if cfg.DocsMCPURL != nil {
table.Append("docs_mcp_url", *cfg.DocsMCPURL)
}
if cfg.GatewayURL != nil {
table.Append("gateway_url", *cfg.GatewayURL)
}
if cfg.Color != nil {
table.Append("color", fmt.Sprintf("%t", *cfg.Color))
}
if cfg.Output != nil {
table.Append("output", *cfg.Output)
}
if cfg.PasswordStorage != nil {
table.Append("password_storage", *cfg.PasswordStorage)
}
if cfg.ReleasesURL != nil {
table.Append("releases_url", *cfg.ReleasesURL)
}
if cfg.ServiceID != nil {
table.Append("service_id", *cfg.ServiceID)
}
if cfg.VersionCheckInterval != nil {
table.Append("version_check_interval", cfg.VersionCheckInterval.String())
}
if cfg.VersionCheckLastTime != nil {
table.Append("version_check_last_time", cfg.VersionCheckLastTime.Format(time.RFC1123))
}
return table.Render()
}
func configOptionCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// Config option is always first positional argument
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
var results []string
for opt := range config.ValidConfigOptions() {
if strings.HasPrefix(opt, toComplete) {
results = append(results, opt)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}