package config import ( "fmt" "os" "path/filepath" "strings" "github.com/spf13/viper" ) // Config holds all configuration type Config struct { Server string Token string Output string Timeout string Quiet bool Verbose bool } var cfg *Config // Load initializes the configuration func Load() (*Config, error) { home, err := os.UserHomeDir() if err != nil { return nil, fmt.Errorf("failed to get home directory: %w", err) } viper.SetConfigName(".claudia-docs") viper.SetConfigType("yaml") viper.AddConfigPath(home) // Set defaults viper.SetDefault("server", "http://localhost:8000") viper.SetDefault("output", "json") viper.SetDefault("timeout", "30s") // Environment variables viper.SetEnvPrefix("CLAUDIA") viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() // Read config file if exists _ = viper.ReadInConfig() cfg = &Config{ Server: viper.GetString("server"), Token: viper.GetString("token"), Output: viper.GetString("output"), Timeout: viper.GetString("timeout"), Quiet: viper.GetBool("quiet"), Verbose: viper.GetBool("verbose"), } return cfg, nil } // GetToken returns the current token func GetToken() string { if cfg == nil { return "" } return cfg.Token } // GetServer returns the current server URL func GetServer() string { if cfg == nil { return "http://localhost:8000" } return cfg.Server } // SaveToken saves a token to the config file func SaveToken(token string) error { home, err := os.UserHomeDir() if err != nil { return fmt.Errorf("failed to get home directory: %w", err) } configPath := filepath.Join(home, ".claudia-docs.yaml") viper.SetConfigFile(configPath) viper.Set("token", token) return viper.WriteConfig() } // ClearToken removes the token from config func ClearToken() error { home, err := os.UserHomeDir() if err != nil { return fmt.Errorf("failed to get home directory: %w", err) } configPath := filepath.Join(home, ".claudia-docs.yaml") viper.SetConfigFile(configPath) viper.Set("token", "") return viper.WriteConfig() } // GetConfig returns the current config func GetConfig() *Config { return cfg } // UpdateFromFlags updates config from CLI flags func UpdateFromFlags(server, token, output string, quiet, verbose bool) { if server != "" { cfg.Server = server } if token != "" { cfg.Token = token } if output != "" { cfg.Output = output } cfg.Quiet = quiet cfg.Verbose = verbose }