Files
beads/internal/config/config.go
Steve Yegge 4da8caef24 Migrate to Viper for unified configuration management (bd-78)
- Add Viper dependency and create internal/config package
- Initialize Viper singleton with config file search paths
- Bind all global flags to Viper with proper precedence (flags > env > config > defaults)
- Replace manual os.Getenv() calls with config.GetString/GetBool/GetDuration
- Update CONFIG.md with comprehensive Viper documentation
- Add comprehensive tests for config precedence and env binding
- Walk up parent directories to discover .beads/config.yaml from subdirectories
- Add env key replacer for hyphenated keys (BD_NO_DAEMON -> no-daemon)
- Remove deprecated prefer-global-daemon setting
- Move Viper config apply before early-return to support version/init/help commands

Hybrid architecture maintains separation:
- Viper: User-specific tool preferences (--json, --no-daemon, etc.)
- bd config: Team-shared project data (Jira URLs, Linear tokens, etc.)

All tests passing. Closes bd-78, bd-79, bd-80, bd-81, bd-82, bd-83.

Amp-Thread-ID: https://ampcode.com/threads/T-0d0f8c1d-b877-4fa9-8477-b6fea63fb664
Co-authored-by: Amp <amp@ampcode.com>
2025-10-23 17:30:05 -07:00

163 lines
4.1 KiB
Go

package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/spf13/viper"
)
var v *viper.Viper
// Initialize sets up the viper configuration singleton
// Should be called once at application startup
func Initialize() error {
v = viper.New()
// Set config file name and type
v.SetConfigName("config")
v.SetConfigType("yaml")
// Add config search paths (in order of precedence)
// 1. Walk up from CWD to find project .beads/ directory
// This allows commands to work from subdirectories
cwd, err := os.Getwd()
if err == nil {
// Walk up parent directories to find .beads/config.yaml
for dir := cwd; dir != filepath.Dir(dir); dir = filepath.Dir(dir) {
beadsDir := filepath.Join(dir, ".beads")
configPath := filepath.Join(beadsDir, "config.yaml")
if _, err := os.Stat(configPath); err == nil {
// Found .beads/config.yaml - add this path
v.AddConfigPath(beadsDir)
break
}
// Also check if .beads directory exists (even without config.yaml)
if info, err := os.Stat(beadsDir); err == nil && info.IsDir() {
v.AddConfigPath(beadsDir)
break
}
}
// Also add CWD/.beads for backward compatibility
v.AddConfigPath(filepath.Join(cwd, ".beads"))
}
// 2. User config directory (~/.config/bd/)
if configDir, err := os.UserConfigDir(); err == nil {
v.AddConfigPath(filepath.Join(configDir, "bd"))
}
// 3. Home directory (~/.beads/)
if homeDir, err := os.UserHomeDir(); err == nil {
v.AddConfigPath(filepath.Join(homeDir, ".beads"))
}
// Automatic environment variable binding
// Environment variables take precedence over config file
// E.g., BD_JSON, BD_NO_DAEMON, BD_ACTOR, BD_DB
v.SetEnvPrefix("BD")
// Replace hyphens and dots with underscores for env var mapping
// This allows BD_NO_DAEMON to map to "no-daemon" config key
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
v.AutomaticEnv()
// Set defaults for all flags
v.SetDefault("json", false)
v.SetDefault("no-daemon", false)
v.SetDefault("no-auto-flush", false)
v.SetDefault("no-auto-import", false)
v.SetDefault("db", "")
v.SetDefault("actor", "")
// Additional environment variables (not prefixed with BD_)
// These are bound explicitly for backward compatibility
v.BindEnv("flush-debounce", "BEADS_FLUSH_DEBOUNCE")
v.BindEnv("auto-start-daemon", "BEADS_AUTO_START_DAEMON")
// Set defaults for additional settings
v.SetDefault("flush-debounce", "5s")
v.SetDefault("auto-start-daemon", true)
// Read config file if it exists (don't error if not found)
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
// Config file found but another error occurred
return fmt.Errorf("error reading config file: %w", err)
}
// Config file not found - this is ok, we'll use defaults
}
return nil
}
// GetString retrieves a string configuration value
func GetString(key string) string {
if v == nil {
return ""
}
return v.GetString(key)
}
// GetBool retrieves a boolean configuration value
func GetBool(key string) bool {
if v == nil {
return false
}
return v.GetBool(key)
}
// GetInt retrieves an integer configuration value
func GetInt(key string) int {
if v == nil {
return 0
}
return v.GetInt(key)
}
// GetDuration retrieves a duration configuration value
func GetDuration(key string) time.Duration {
if v == nil {
return 0
}
return v.GetDuration(key)
}
// Set sets a configuration value
func Set(key string, value interface{}) {
if v != nil {
v.Set(key, value)
}
}
// BindPFlag is reserved for future use if we want to bind Cobra flags directly to Viper
// For now, we handle flag precedence manually in PersistentPreRun
// Uncomment and implement if needed:
//
// func BindPFlag(key string, flag *pflag.Flag) error {
// if v == nil {
// return fmt.Errorf("viper not initialized")
// }
// return v.BindPFlag(key, flag)
// }
// ConfigFileUsed returns the path to the config file being used
func ConfigFileUsed() string {
if v == nil {
return ""
}
return v.ConfigFileUsed()
}
// AllSettings returns all configuration settings as a map
func AllSettings() map[string]interface{} {
if v == nil {
return map[string]interface{}{}
}
return v.AllSettings()
}