Skip to content

Commit 6563513

Browse files
YutongGudevgianlu
authored andcommitted
feat: add command line config overrides support
1 parent 63920cf commit 6563513

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

cmd/daemon/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,10 @@ func loadConfig(cfg *Config) error {
451451
}
452452
defaultConfigDir := filepath.Join(userConfigDir, "go-librespot")
453453
f.StringVar(&cfg.ConfigDir, "config_dir", defaultConfigDir, "the configuration directory")
454+
455+
var configOverrides []string
456+
f.StringArrayVarP(&configOverrides, "conf", "c", nil, "override config values (format: field=value, use field1.field2=value for nested fields)")
457+
454458
err = f.Parse(os.Args[1:])
455459
if err != nil {
456460
return err
@@ -515,6 +519,26 @@ func loadConfig(cfg *Config) error {
515519
return fmt.Errorf("failed loading command line configuration: %w", err)
516520
}
517521

522+
// apply command line config overrides (-c/--conf flags)
523+
if len(configOverrides) > 0 {
524+
overrideMap := make(map[string]interface{})
525+
for _, override := range configOverrides {
526+
parts := strings.SplitN(override, "=", 2)
527+
if len(parts) != 2 {
528+
return fmt.Errorf("invalid config override format: %s (expected field=value)", override)
529+
}
530+
key := strings.TrimSpace(parts[0])
531+
value := strings.TrimSpace(parts[1])
532+
if key == "" {
533+
return fmt.Errorf("invalid config override: empty field name in %s", override)
534+
}
535+
overrideMap[key] = value
536+
}
537+
if err := k.Load(confmap.Provider(overrideMap, "."), nil); err != nil {
538+
return fmt.Errorf("failed loading config overrides: %w", err)
539+
}
540+
}
541+
518542
// unmarshal configuration
519543
if err := k.Unmarshal("", &cfg); err != nil {
520544
return fmt.Errorf("failed to unmarshal configuration: %w", err)

0 commit comments

Comments
 (0)