package config import ( "claude-squad/log" "encoding/json" "fmt" "os" "os/exec" "os/user" "path/filepath" "regexp" "strings" ) const ( ConfigFileName = "config.json" defaultProgram = "claude" ) // GetConfigDir returns the path to the application's configuration directory func GetConfigDir() (string, error) { homeDir, err := os.UserHomeDir() if err != nil { return "", fmt.Errorf("failed to get config home directory: %w", err) } return filepath.Join(homeDir, ".claude-squad"), nil } // Profile represents a named program configuration type Profile struct { Name string `json:"name"` Program string `json:"program"` } // Config represents the application configuration type Config struct { // DefaultProgram is the default program to run in new instances DefaultProgram string `json:"default_program"` // AutoYes is a flag to automatically accept all prompts. AutoYes bool `json:"auto_yes"` // DaemonPollInterval is the interval (ms) at which the daemon polls sessions for autoyes mode. DaemonPollInterval int `json:"daemon_poll_interval"` // BranchPrefix is the prefix used for git branches created by the application. BranchPrefix string `json:"branch_prefix"` // Profiles is a list of named program profiles. Profiles []Profile `json:"profiles,omitempty"` } // GetProgram returns the program to run. If Profiles is non-empty and // DefaultProgram matches a profile name, that profile's Program is returned. // Otherwise DefaultProgram is returned as-is. func (c *Config) GetProgram() string { for _, p := range c.Profiles { if p.Name == c.DefaultProgram { return p.Program } } return c.DefaultProgram } // GetProfiles returns a unified list of profiles. If Profiles is defined, // those are returned with the default profile first. Otherwise, a single // profile is synthesized from DefaultProgram. func (c *Config) GetProfiles() []Profile { if len(c.Profiles) == 0 { return []Profile{{Name: c.DefaultProgram, Program: c.DefaultProgram}} } // Reorder so the default profile comes first. profiles := make([]Profile, 0, len(c.Profiles)) for _, p := range c.Profiles { if p.Name == c.DefaultProgram { profiles = append(profiles, p) break } } for _, p := range c.Profiles { if p.Name != c.DefaultProgram { profiles = append(profiles, p) } } return profiles } // DefaultConfig returns the default configuration func DefaultConfig() *Config { program, err := GetClaudeCommand() if err != nil { log.ErrorLog.Printf("failed to get claude command: %v", err) program = defaultProgram } return &Config{ DefaultProgram: program, AutoYes: false, DaemonPollInterval: 1000, BranchPrefix: func() string { user, err := user.Current() if err != nil || user == nil || user.Username == "" { log.ErrorLog.Printf("failed to get current user: %v", err) return "session/" } return fmt.Sprintf("%s/", strings.ToLower(user.Username)) }(), } } // GetClaudeCommand attempts to find the "claude" command in the user's shell // It checks in the following order: // 1. Shell alias resolution: using "which" command // 2. PATH lookup // // If both fail, it returns an error. func GetClaudeCommand() (string, error) { shell := os.Getenv("SHELL") if shell == "" { shell = "/bin/bash" // Default to bash if SHELL is not set } // Force the shell to load the user's profile and then run the command // For zsh, source .zshrc; for bash, source .bashrc var shellCmd string if strings.Contains(shell, "zsh") { shellCmd = "source ~/.zshrc &>/dev/null || true; which claude" } else if strings.Contains(shell, "bash") { shellCmd = "source ~/.bashrc &>/dev/null || true; which claude" } else { shellCmd = "which claude" } cmd := exec.Command(shell, "-c", shellCmd) output, err := cmd.Output() if err == nil && len(output) > 0 { path := strings.TrimSpace(string(output)) if path != "" { // Check if the output is an alias definition and extract the actual path // Handle formats like "claude: aliased to /path/to/claude" or other shell-specific formats aliasRegex := regexp.MustCompile(`(?:aliased to|->|=)\s*([^\s]+)`) matches := aliasRegex.FindStringSubmatch(path) if len(matches) > 1 { path = matches[1] } return path, nil } } // Otherwise, try to find in PATH directly claudePath, err := exec.LookPath("claude") if err == nil { return claudePath, nil } return "", fmt.Errorf("claude command not found in aliases or PATH") } func LoadConfig() *Config { configDir, err := GetConfigDir() if err != nil { log.ErrorLog.Printf("failed to get config directory: %v", err) return DefaultConfig() } configPath := filepath.Join(configDir, ConfigFileName) data, err := os.ReadFile(configPath) if err != nil { if os.IsNotExist(err) { // Create and save default config if file doesn't exist defaultCfg := DefaultConfig() if saveErr := saveConfig(defaultCfg); saveErr != nil { log.WarningLog.Printf("failed to save default config: %v", saveErr) } return defaultCfg } log.WarningLog.Printf("failed to get config file: %v", err) return DefaultConfig() } var config Config if err := json.Unmarshal(data, &config); err != nil { log.ErrorLog.Printf("failed to parse config file: %v", err) return DefaultConfig() } return &config } // saveConfig saves the configuration to disk func saveConfig(config *Config) error { configDir, err := GetConfigDir() if err != nil { return fmt.Errorf("failed to get config directory: %w", err) } if err := os.MkdirAll(configDir, 0755); err != nil { return fmt.Errorf("failed to create config directory: %w", err) } configPath := filepath.Join(configDir, ConfigFileName) data, err := json.MarshalIndent(config, "", " ") if err != nil { return fmt.Errorf("failed to marshal config: %w", err) } return os.WriteFile(configPath, data, 0644) } // SaveConfig exports the saveConfig function for use by other packages func SaveConfig(config *Config) error { return saveConfig(config) }