-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconfig.go
More file actions
567 lines (488 loc) · 15.6 KB
/
config.go
File metadata and controls
567 lines (488 loc) · 15.6 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
package config
import (
"errors"
"fmt"
"io/fs"
"iter"
"maps"
"os"
"path/filepath"
"strconv"
"time"
"github.com/go-viper/mapstructure/v2"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/timescale/tiger-cli/internal/tiger/util"
)
type Config struct {
APIURL string `mapstructure:"api_url"`
Analytics bool `mapstructure:"analytics"`
Color bool `mapstructure:"color"`
ConfigDir string `mapstructure:"config_dir"`
ConsoleURL string `mapstructure:"console_url"`
Debug bool `mapstructure:"debug"`
DocsMCP bool `mapstructure:"docs_mcp"`
DocsMCPURL string `mapstructure:"docs_mcp_url"`
GatewayURL string `mapstructure:"gateway_url"`
Output string `mapstructure:"output"`
PasswordStorage string `mapstructure:"password_storage"`
ReleasesURL string `mapstructure:"releases_url"`
ServiceID string `mapstructure:"service_id"`
VersionCheckInterval time.Duration `mapstructure:"version_check_interval"`
VersionCheckLastTime time.Time `mapstructure:"version_check_last_time"`
viper *viper.Viper `mapstructure:"-"`
}
type ConfigOutput struct {
APIURL *string `mapstructure:"api_url" json:"api_url,omitempty"`
Analytics *bool `mapstructure:"analytics" json:"analytics,omitempty"`
Color *bool `mapstructure:"color" json:"color,omitempty"`
ConfigDir *string `mapstructure:"config_dir" json:"config_dir,omitempty"`
ConsoleURL *string `mapstructure:"console_url" json:"console_url,omitempty"`
Debug *bool `mapstructure:"debug" json:"debug,omitempty"`
DocsMCP *bool `mapstructure:"docs_mcp" json:"docs_mcp,omitempty"`
DocsMCPURL *string `mapstructure:"docs_mcp_url" json:"docs_mcp_url,omitempty"`
GatewayURL *string `mapstructure:"gateway_url" json:"gateway_url,omitempty"`
Output *string `mapstructure:"output" json:"output,omitempty"`
PasswordStorage *string `mapstructure:"password_storage" json:"password_storage,omitempty"`
ReleasesURL *string `mapstructure:"releases_url" json:"releases_url,omitempty"`
ServiceID *string `mapstructure:"service_id" json:"service_id,omitempty"`
VersionCheckInterval *util.Duration `mapstructure:"version_check_interval" json:"version_check_interval,omitempty"` // [util.Duration] ensures value is marshaled in [time.Duration.String] format when output
VersionCheckLastTime *time.Time `mapstructure:"version_check_last_time" json:"version_check_last_time,omitempty"`
}
const (
ConfigFileName = "config.yaml"
DefaultAPIURL = "https://console.cloud.timescale.com/public/api/v1"
DefaultAnalytics = true
DefaultColor = true
DefaultConsoleURL = "https://console.cloud.timescale.com"
DefaultDebug = false
DefaultDocsMCP = true
DefaultDocsMCPURL = "https://mcp.tigerdata.com/docs"
DefaultGatewayURL = "https://console.cloud.timescale.com/api"
DefaultOutput = "table"
DefaultPasswordStorage = "keyring"
DefaultReleasesURL = "https://cli.tigerdata.com"
DefaultVersionCheckInterval = 24 * time.Hour
)
var defaultValues = map[string]any{
"analytics": DefaultAnalytics,
"api_url": DefaultAPIURL,
"color": DefaultColor,
"console_url": DefaultConsoleURL,
"debug": DefaultDebug,
"docs_mcp": DefaultDocsMCP,
"docs_mcp_url": DefaultDocsMCPURL,
"gateway_url": DefaultGatewayURL,
"output": DefaultOutput,
"password_storage": DefaultPasswordStorage,
"releases_url": DefaultReleasesURL,
"service_id": "",
"version_check_interval": DefaultVersionCheckInterval.String(), // String can be interpreted as either [time.Duration] (for [Config]) or [util.Duration] (for [ConfigOutput])
"version_check_last_time": time.Time{},
}
func ValidConfigOptions() iter.Seq[string] {
return maps.Keys(defaultValues)
}
func ApplyDefaults(v *viper.Viper) {
for key, value := range defaultValues {
v.SetDefault(key, value)
}
}
func ApplyEnvOverrides(v *viper.Viper) {
v.SetEnvPrefix("TIGER")
v.AutomaticEnv()
}
func ReadInConfig(v *viper.Viper) error {
// Try to read config file if it exists
// If file doesn't exist, that's okay - we'll use defaults and env vars
if err := v.ReadInConfig(); err != nil &&
!errors.As(err, &viper.ConfigFileNotFoundError{}) &&
!errors.Is(err, fs.ErrNotExist) {
return err
}
return nil
}
// SetupViper configures the global Viper instance with defaults, env vars, and config file
func SetupViper(configDir string) error {
v := viper.GetViper()
// Configure viper to read from config file
configFile := GetConfigFile(configDir)
v.SetConfigFile(configFile)
// Configure viper to read from env vars
ApplyEnvOverrides(v)
// Set defaults for all config values
ApplyDefaults(v)
return ReadInConfig(v)
}
func FromViper(v *viper.Viper) (*Config, error) {
cfg := &Config{
ConfigDir: filepath.Dir(v.ConfigFileUsed()),
viper: v,
}
if err := v.Unmarshal(cfg); err != nil {
return nil, fmt.Errorf("error unmarshaling config: %w", err)
}
return cfg, nil
}
func ForOutputFromViper(v *viper.Viper) (*ConfigOutput, error) {
configDir := filepath.Dir(v.ConfigFileUsed())
cfg := &ConfigOutput{
ConfigDir: &configDir,
}
if err := v.Unmarshal(cfg,
// Decode hook allows us to unmarshal a string into a [util.Duration] for the sake of VersionCheckInterval
viper.DecodeHook(mapstructure.TextUnmarshallerHookFunc()),
); err != nil {
return nil, fmt.Errorf("error unmarshaling config for output: %w", err)
}
return cfg, nil
}
// Load creates a new Config instance from the current viper state
// This function should be called after SetupViper has been called to initialize viper
func Load() (*Config, error) {
v := viper.GetViper()
// Try to read config file into viper to ensure we're unmarshaling the most
// up-to-date values into the config struct.
if err := ReadInConfig(v); err != nil {
return nil, err
}
return FromViper(v)
}
func ensureConfigDir(configDir string) (string, error) {
if err := os.MkdirAll(configDir, 0755); err != nil {
return "", fmt.Errorf("error creating config directory: %w", err)
}
return GetConfigFile(configDir), nil
}
func (c *Config) EnsureConfigDir() (string, error) {
return ensureConfigDir(c.ConfigDir)
}
// UseTestConfig writes only the specified key-value pairs to the config file and
// returns a Config instance with those values set.
// This function is intended for testing purposes only, where you need to set up
// specific config file state without writing default values for unspecified keys.
func UseTestConfig(configDir string, values map[string]any) (*Config, error) {
configFile, err := ensureConfigDir(configDir)
if err != nil {
return nil, err
}
v := viper.New()
v.SetConfigFile(configFile)
// Write only the specified key-value pairs
for key, value := range values {
v.Set(key, value)
}
if err := v.WriteConfigAs(configFile); err != nil {
return nil, fmt.Errorf("error writing config file: %w", err)
}
viper.Reset()
if err := SetupViper(configDir); err != nil {
return nil, err
}
// Construct and return a Config instance with the values
cfg := &Config{
ConfigDir: configDir,
}
if err := viper.Unmarshal(cfg); err != nil {
return nil, fmt.Errorf("error unmarshaling config: %w", err)
}
return cfg, nil
}
func (c *Config) Set(key, value string) error {
// Validate and update the field
validated, err := c.UpdateField(key, value)
if err != nil {
return err
}
// Write to config file
configFile, err := c.EnsureConfigDir()
if err != nil {
return err
}
v := viper.New()
v.SetConfigFile(configFile)
v.ReadInConfig()
v.Set(key, validated)
if err := v.WriteConfigAs(configFile); err != nil {
return fmt.Errorf("error writing config file: %w", err)
}
return nil
}
func setBool(key, val string) (bool, error) {
b, err := strconv.ParseBool(val)
if err != nil {
return false, fmt.Errorf("invalid %s value: %s (must be true or false)", key, val)
}
return b, nil
}
// UpdateField updates the field in the Config struct corresponding to the given key.
// It accepts either a string (from user input) or a typed value (string/bool from defaults).
// The function validates the value and updates both the struct field and viper state.
func (c *Config) UpdateField(key string, value any) (any, error) {
var validated any
switch key {
case "api_url":
s, ok := value.(string)
if !ok {
return nil, fmt.Errorf("api_url must be string, got %T", value)
}
c.APIURL = s
validated = s
case "console_url":
s, ok := value.(string)
if !ok {
return nil, fmt.Errorf("console_url must be string, got %T", value)
}
c.ConsoleURL = s
validated = s
case "gateway_url":
s, ok := value.(string)
if !ok {
return nil, fmt.Errorf("gateway_url must be string, got %T", value)
}
c.GatewayURL = s
validated = s
case "docs_mcp":
switch v := value.(type) {
case bool:
c.DocsMCP = v
validated = v
case string:
b, err := setBool("docs_mcp", v)
if err != nil {
return nil, err
}
c.DocsMCP = b
validated = b
default:
return nil, fmt.Errorf("docs_mcp must be string or bool, got %T", value)
}
case "docs_mcp_url":
s, ok := value.(string)
if !ok {
return nil, fmt.Errorf("docs_mcp_url must be string, got %T", value)
}
c.DocsMCPURL = s
validated = s
case "service_id":
s, ok := value.(string)
if !ok {
return nil, fmt.Errorf("service_id must be string, got %T", value)
}
c.ServiceID = s
validated = s
case "color":
switch v := value.(type) {
case bool:
c.Color = v
validated = v
case string:
b, err := setBool("color", v)
if err != nil {
return nil, err
}
c.Color = b
validated = b
default:
return nil, fmt.Errorf("color must be string or bool, got %T", value)
}
case "output":
s, ok := value.(string)
if !ok {
return nil, fmt.Errorf("output must be string, got %T", value)
}
if err := ValidateOutputFormat(s, false); err != nil {
return nil, err
}
c.Output = s
validated = s
case "analytics":
switch v := value.(type) {
case bool:
c.Analytics = v
validated = v
case string:
b, err := setBool("analytics", v)
if err != nil {
return nil, err
}
c.Analytics = b
validated = b
default:
return nil, fmt.Errorf("analytics must be string or bool, got %T", value)
}
case "password_storage":
s, ok := value.(string)
if !ok {
return nil, fmt.Errorf("password_storage must be string, got %T", value)
}
if s != "keyring" && s != "pgpass" && s != "none" {
return nil, fmt.Errorf("invalid password_storage value: %s (must be keyring, pgpass, or none)", s)
}
c.PasswordStorage = s
validated = s
case "debug":
switch v := value.(type) {
case bool:
c.Debug = v
validated = v
case string:
b, err := setBool("debug", v)
if err != nil {
return nil, err
}
c.Debug = b
validated = b
default:
return nil, fmt.Errorf("debug must be string or bool, got %T", value)
}
case "releases_url":
s, ok := value.(string)
if !ok {
return nil, fmt.Errorf("releases_url must be string, got %T", value)
}
c.ReleasesURL = s
validated = s
case "version_check_interval":
switch v := value.(type) {
case time.Duration:
if v < 0 {
return nil, fmt.Errorf("version_check_interval must be non-negative (0 to disable)")
}
c.VersionCheckInterval = v
validated = v
case string:
// Parse duration string like "1h", "30m", "24h"
d, err := time.ParseDuration(v)
if err != nil {
return nil, fmt.Errorf("invalid version_check_interval value: %s (must be a duration like '1h', '30m', etc.)", v)
}
if d < 0 {
return nil, fmt.Errorf("version_check_interval must be non-negative (0 to disable)")
}
c.VersionCheckInterval = d
validated = d
default:
return nil, fmt.Errorf("version_check_interval must be string or duration, got %T", value)
}
case "version_check_last_time":
nowish := time.Now().Add(time.Hour)
switch v := value.(type) {
case time.Time:
if v.After(nowish) {
return nil, fmt.Errorf("version_check_last_time cannot be in the future")
}
c.VersionCheckLastTime = v
validated = v
case string:
// Try parsing as RFC3339 first, then as unix timestamp
t, err := time.Parse(time.RFC3339, v)
if err != nil {
// Try parsing as unix timestamp
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid version_check_last_time value: %s (must be RFC3339 timestamp or unix timestamp)", v)
}
t = time.Unix(i, 0)
}
if t.After(nowish) {
return nil, fmt.Errorf("version_check_last_time cannot be in the future")
}
c.VersionCheckLastTime = t
validated = t
default:
return nil, fmt.Errorf("version_check_last_time must be string or time, got %T", value)
}
default:
return nil, fmt.Errorf("unknown configuration key: %s", key)
}
if c.viper == nil {
viper.Set(key, validated)
} else {
c.viper.Set(key, validated)
}
return validated, nil
}
func (c *Config) Unset(key string) error {
configFile, err := c.EnsureConfigDir()
if err != nil {
return err
}
vCurrent := viper.New()
vCurrent.SetConfigFile(configFile)
vCurrent.ReadInConfig()
vNew := viper.New()
vNew.SetConfigFile(configFile)
_, validKey := defaultValues[key]
for k, v := range vCurrent.AllSettings() {
if k != key {
vNew.Set(k, v)
} else {
validKey = true
}
}
if !validKey {
return fmt.Errorf("unknown configuration key: %s", key)
}
// Apply the default to the current global viper state
if def, ok := defaultValues[key]; ok {
if _, err := c.UpdateField(key, def); err != nil {
return err
}
}
if err := vNew.WriteConfigAs(configFile); err != nil {
return fmt.Errorf("error writing config file: %w", err)
}
return nil
}
func (c *Config) Reset() error {
configFile, err := c.EnsureConfigDir()
if err != nil {
return err
}
v := viper.New()
v.SetConfigFile(configFile)
if err := v.WriteConfigAs(configFile); err != nil {
return fmt.Errorf("error writing config file: %w", err)
}
// Apply all defaults to the current global viper state
for key, value := range defaultValues {
if _, err := c.UpdateField(key, value); err != nil {
return err
}
}
return nil
}
func GetConfigFile(dir string) string {
return filepath.Join(dir, ConfigFileName)
}
func (c *Config) GetConfigFile() string {
return GetConfigFile(c.ConfigDir)
}
// TODO: This function is currently used to get the directory that the API
// key fallback file should be stored in (see credentials.go). But ideally, those
// functions would take a Config struct and use the ConfigDir field instead.
func GetConfigDir() string {
return filepath.Dir(viper.ConfigFileUsed())
}
func GetDefaultConfigDir() string {
homeDir, err := os.UserHomeDir()
if err != nil {
return "./.config/tiger"
}
return filepath.Join(homeDir, ".config", "tiger")
}
func GetEffectiveConfigDir(configDirFlag *pflag.Flag) string {
if configDirFlag.Changed {
return util.ExpandPath(configDirFlag.Value.String())
}
if dir := os.Getenv("TIGER_CONFIG_DIR"); dir != "" {
return util.ExpandPath(dir)
}
return GetDefaultConfigDir()
}
// ResetGlobalConfig clears the global viper state for testing
// This is mainly used to reset viper configuration between test runs
func ResetGlobalConfig() {
viper.Reset()
}