-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconfig.go
More file actions
202 lines (173 loc) · 5.56 KB
/
config.go
File metadata and controls
202 lines (173 loc) · 5.56 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
package cmd
import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
"github.com/timescale/tiger-cli/internal/tiger/config"
"github.com/timescale/tiger-cli/internal/tiger/logging"
)
func buildConfigShowCmd() *cobra.Command {
return &cobra.Command{
Use: "show",
Short: "Show current configuration",
Long: `Display the current CLI configuration settings`,
Args: cobra.NoArgs,
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)
}
switch cfg.Output {
case "json":
return outputJSON(cfg, cmd)
case "yaml":
return outputYAML(cfg, cmd)
case "env":
return fmt.Errorf("environment variable output is not supported for config")
default:
return outputTable(cfg, 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),
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),
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,
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(cfg *config.Config, cmd *cobra.Command) error {
out := cmd.OutOrStdout()
fmt.Fprintln(out, "Current Configuration:")
fmt.Fprintf(out, " api_url: %s\n", cfg.APIURL)
fmt.Fprintf(out, " console_url: %s\n", cfg.ConsoleURL)
fmt.Fprintf(out, " gateway_url: %s\n", cfg.GatewayURL)
fmt.Fprintf(out, " docs_mcp: %t\n", cfg.DocsMCP)
fmt.Fprintf(out, " docs_mcp_url: %s\n", cfg.DocsMCPURL)
fmt.Fprintf(out, " project_id: %s\n", valueOrEmpty(cfg.ProjectID))
fmt.Fprintf(out, " service_id: %s\n", valueOrEmpty(cfg.ServiceID))
fmt.Fprintf(out, " output: %s\n", cfg.Output)
fmt.Fprintf(out, " analytics: %t\n", cfg.Analytics)
fmt.Fprintf(out, " password_storage: %s\n", cfg.PasswordStorage)
fmt.Fprintf(out, " debug: %t\n", cfg.Debug)
fmt.Fprintf(out, " config_dir: %s\n", cfg.ConfigDir)
return nil
}
func outputJSON(cfg *config.Config, cmd *cobra.Command) error {
data := map[string]interface{}{
"api_url": cfg.APIURL,
"console_url": cfg.ConsoleURL,
"gateway_url": cfg.GatewayURL,
"docs_mcp": cfg.DocsMCP,
"docs_mcp_url": cfg.DocsMCPURL,
"project_id": cfg.ProjectID,
"service_id": cfg.ServiceID,
"output": cfg.Output,
"analytics": cfg.Analytics,
"password_storage": cfg.PasswordStorage,
"debug": cfg.Debug,
"config_dir": cfg.ConfigDir,
}
encoder := json.NewEncoder(cmd.OutOrStdout())
encoder.SetIndent("", " ")
return encoder.Encode(data)
}
func outputYAML(cfg *config.Config, cmd *cobra.Command) error {
data := map[string]interface{}{
"api_url": cfg.APIURL,
"console_url": cfg.ConsoleURL,
"gateway_url": cfg.GatewayURL,
"docs_mcp": cfg.DocsMCP,
"docs_mcp_url": cfg.DocsMCPURL,
"project_id": cfg.ProjectID,
"service_id": cfg.ServiceID,
"output": cfg.Output,
"analytics": cfg.Analytics,
"password_storage": cfg.PasswordStorage,
"debug": cfg.Debug,
"config_dir": cfg.ConfigDir,
}
encoder := yaml.NewEncoder(cmd.OutOrStdout())
defer encoder.Close()
return encoder.Encode(data)
}
func valueOrEmpty(s string) string {
if s == "" {
return "(not set)"
}
return s
}