Improve config list: dynamic yaml key discovery and comprehensive env var detection (#3088)
Improve config list: dynamic key discovery and comprehensive env var detection. Replaces the hardcoded yaml-only key list with config.AllKeys() + IsYamlOnlyKey() so new keys are automatically included. Fixes env override detection to use config.EnvVarName() for both BD_* and legacy BEADS_* prefixes with LookupEnv. Thanks to @harry-miller-trimble for the contribution! Closes bd-uss
This commit is contained in:
parent
7ada135baf
commit
e42f498acf
2 changed files with 122 additions and 23 deletions
|
|
@ -284,37 +284,56 @@ var configListCmd = &cobra.Command{
|
|||
// This addresses the confusion when `bd config list` shows one value but the effective
|
||||
// value used by commands is different due to higher-priority config sources.
|
||||
func showConfigYAMLOverrides(dbConfig map[string]string) {
|
||||
var warnings []string
|
||||
var envWarnings []string
|
||||
|
||||
// Check each DB config key for env var overrides
|
||||
// Check each DB config key for env var overrides using config.EnvVarName
|
||||
// which handles both BD_* and legacy BEADS_* prefixes with LookupEnv.
|
||||
for key, dbValue := range dbConfig {
|
||||
envKey := "BD_" + strings.ToUpper(strings.ReplaceAll(strings.ReplaceAll(key, "-", "_"), ".", "_"))
|
||||
if envValue := os.Getenv(envKey); envValue != "" && envValue != dbValue {
|
||||
warnings = append(warnings, fmt.Sprintf(" %s: DB has %q, but env %s=%q takes precedence", key, dbValue, envKey, envValue))
|
||||
if envName := config.EnvVarName(key); envName != "" {
|
||||
envValue := os.Getenv(envName)
|
||||
if envValue != dbValue {
|
||||
envWarnings = append(envWarnings, fmt.Sprintf(" %s: DB has %q, but env %s=%q takes precedence", key, dbValue, envName, envValue))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for yaml-only keys set in config.yaml that aren't visible in DB output
|
||||
yamlKeys := []string{
|
||||
"no-db", "json", "actor", "identity",
|
||||
"routing.mode", "routing.default", "routing.maintainer", "routing.contributor",
|
||||
"sync.git-remote", "no-push", "no-git-ops",
|
||||
"git.author", "git.no-gpg-sign",
|
||||
"create.require-description",
|
||||
"validation.on-create", "validation.on-close", "validation.on-sync",
|
||||
"hierarchy.max-depth",
|
||||
"backup.enabled", "backup.interval", "backup.git-push", "backup.git-repo",
|
||||
"dolt.idle-timeout", "dolt.shared-server",
|
||||
}
|
||||
// Discover yaml-only keys dynamically via AllKeys() instead of a hardcoded list.
|
||||
// This stays in sync as new yaml-only keys are added to the config system.
|
||||
allKeys := config.AllKeys()
|
||||
sort.Strings(allKeys)
|
||||
|
||||
var yamlOverrides []string
|
||||
for _, key := range yamlKeys {
|
||||
val := config.GetYamlConfig(key)
|
||||
if val != "" && config.GetValueSource(key) == config.SourceConfigFile {
|
||||
for _, key := range allKeys {
|
||||
// Skip keys already shown in the DB config section
|
||||
if _, inDB := dbConfig[key]; inDB {
|
||||
continue
|
||||
}
|
||||
// Only show yaml-only keys that are explicitly set in config.yaml
|
||||
if !config.IsYamlOnlyKey(key) {
|
||||
continue
|
||||
}
|
||||
if config.GetValueSource(key) != config.SourceConfigFile {
|
||||
continue
|
||||
}
|
||||
val := config.GetString(key)
|
||||
if val != "" {
|
||||
yamlOverrides = append(yamlOverrides, fmt.Sprintf(" %s = %s", key, val))
|
||||
}
|
||||
}
|
||||
|
||||
// Also check yaml-only keys for env var overrides
|
||||
for _, key := range allKeys {
|
||||
if _, inDB := dbConfig[key]; inDB {
|
||||
continue // already checked above
|
||||
}
|
||||
if envName := config.EnvVarName(key); envName != "" {
|
||||
src := config.GetValueSource(key)
|
||||
if src == config.SourceEnvVar {
|
||||
envWarnings = append(envWarnings, fmt.Sprintf(" %s: env %s=%q overrides config", key, envName, os.Getenv(envName)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(yamlOverrides) > 0 {
|
||||
fmt.Println("\nAlso set in config.yaml (not shown above):")
|
||||
for _, line := range yamlOverrides {
|
||||
|
|
@ -322,13 +341,15 @@ func showConfigYAMLOverrides(dbConfig map[string]string) {
|
|||
}
|
||||
}
|
||||
|
||||
if len(warnings) > 0 {
|
||||
sort.Strings(warnings)
|
||||
if len(envWarnings) > 0 {
|
||||
sort.Strings(envWarnings)
|
||||
fmt.Println("\n⚠ Environment variable overrides detected:")
|
||||
for _, w := range warnings {
|
||||
for _, w := range envWarnings {
|
||||
fmt.Println(w)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("\nTip: Run 'bd config show' for all effective config with provenance.")
|
||||
}
|
||||
|
||||
var configUnsetCmd = &cobra.Command{
|
||||
|
|
|
|||
78
cmd/bd/config_list_test.go
Normal file
78
cmd/bd/config_list_test.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
)
|
||||
|
||||
func TestShowConfigYAMLOverrides_EnvVarDetection(t *testing.T) {
|
||||
t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1")
|
||||
config.ResetForTesting()
|
||||
_ = config.Initialize()
|
||||
|
||||
// Set an env var override using BD_ prefix
|
||||
t.Setenv("BD_TEST_KEY", "env-value")
|
||||
|
||||
dbConfig := map[string]string{
|
||||
"test-key": "db-value",
|
||||
}
|
||||
|
||||
// Redirect stdout to avoid test noise
|
||||
old := os.Stdout
|
||||
os.Stdout, _ = os.Open(os.DevNull)
|
||||
defer func() { os.Stdout = old }()
|
||||
|
||||
// Should not panic
|
||||
showConfigYAMLOverrides(dbConfig)
|
||||
}
|
||||
|
||||
func TestShowConfigYAMLOverrides_LegacyEnvVar(t *testing.T) {
|
||||
t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1")
|
||||
config.ResetForTesting()
|
||||
_ = config.Initialize()
|
||||
|
||||
// Set a legacy BEADS_ prefix env var
|
||||
t.Setenv("BEADS_TEST_KEY", "legacy-value")
|
||||
|
||||
dbConfig := map[string]string{
|
||||
"test-key": "db-value",
|
||||
}
|
||||
|
||||
old := os.Stdout
|
||||
os.Stdout, _ = os.Open(os.DevNull)
|
||||
defer func() { os.Stdout = old }()
|
||||
|
||||
showConfigYAMLOverrides(dbConfig)
|
||||
}
|
||||
|
||||
func TestShowConfigYAMLOverrides_EmptyDB(t *testing.T) {
|
||||
t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1")
|
||||
config.ResetForTesting()
|
||||
_ = config.Initialize()
|
||||
|
||||
old := os.Stdout
|
||||
os.Stdout, _ = os.Open(os.DevNull)
|
||||
defer func() { os.Stdout = old }()
|
||||
|
||||
// Empty DB config — should still check yaml keys and not panic
|
||||
showConfigYAMLOverrides(map[string]string{})
|
||||
}
|
||||
|
||||
func TestShowConfigYAMLOverrides_NoOverrides(t *testing.T) {
|
||||
t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1")
|
||||
config.ResetForTesting()
|
||||
_ = config.Initialize()
|
||||
|
||||
dbConfig := map[string]string{
|
||||
"some-key": "some-value",
|
||||
}
|
||||
|
||||
old := os.Stdout
|
||||
os.Stdout, _ = os.Open(os.DevNull)
|
||||
defer func() { os.Stdout = old }()
|
||||
|
||||
// No env vars set, should complete without issues
|
||||
showConfigYAMLOverrides(dbConfig)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue