package config import ( "encoding/json" "os" "path/filepath" "runtime" "strings" ) // WidgetConfig holds the widget configuration. type WidgetConfig struct { RefreshInterval int `json:"refresh_interval"` OrgID string `json:"org_id"` } var defaults = WidgetConfig{ RefreshInterval: 300, OrgID: "", } // ConfigDir returns the platform-specific config directory. func ConfigDir() string { if v := os.Getenv("CLAUDE_STATUSLINE_CONFIG"); v != "" { return v } if runtime.GOOS == "windows" { return filepath.Join(os.Getenv("LOCALAPPDATA"), "claude-statusline") } if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" { return filepath.Join(xdg, "claude-statusline") } home, _ := os.UserHomeDir() return filepath.Join(home, ".config", "claude-statusline") } // ConfigPath returns the path to widget-config.json. func ConfigPath() string { return filepath.Join(ConfigDir(), "widget-config.json") } // SessionKeyPath returns the path to the session-key file. func SessionKeyPath() string { return filepath.Join(ConfigDir(), "session-key") } // Load reads widget-config.json and merges with defaults. func Load() WidgetConfig { cfg := defaults data, err := os.ReadFile(ConfigPath()) if err != nil { return cfg } _ = json.Unmarshal(data, &cfg) if cfg.RefreshInterval < 60 { cfg.RefreshInterval = 60 } return cfg } // Save writes widget-config.json. func Save(cfg WidgetConfig) error { dir := ConfigDir() if err := os.MkdirAll(dir, 0o755); err != nil { return err } data, err := json.MarshalIndent(cfg, "", " ") if err != nil { return err } return os.WriteFile(ConfigPath(), data, 0o644) } // GetSessionKey returns the session key from env or file. func GetSessionKey() string { if v := os.Getenv("CLAUDE_SESSION_KEY"); v != "" { return strings.TrimSpace(v) } data, err := os.ReadFile(SessionKeyPath()) if err != nil { return "" } return strings.TrimSpace(string(data)) } // SetSessionKey writes the session key to file. func SetSessionKey(key string) error { dir := ConfigDir() if err := os.MkdirAll(dir, 0o755); err != nil { return err } return os.WriteFile(SessionKeyPath(), []byte(strings.TrimSpace(key)+"\n"), 0o600) }