87 lines
1.7 KiB
Go
87 lines
1.7 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type Config struct {
|
|
Server ServerConfig `yaml:"server"`
|
|
Daemon DaemonConfig `yaml:"daemon"`
|
|
Claude ClaudeConfig `yaml:"claude"`
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
URL string `yaml:"url"`
|
|
APIKey string `yaml:"api_key"`
|
|
}
|
|
|
|
type DaemonConfig struct {
|
|
SocketPath string `yaml:"socket_path"`
|
|
TmuxPrefix string `yaml:"tmux_prefix"`
|
|
LogLevel string `yaml:"log_level"`
|
|
LogFile string `yaml:"log_file"`
|
|
DataDir string `yaml:"data_dir"`
|
|
}
|
|
|
|
type ClaudeConfig struct {
|
|
Binary string `yaml:"binary"`
|
|
DefaultArgs []string `yaml:"default_args"`
|
|
}
|
|
|
|
func DefaultConfig() *Config {
|
|
return &Config{
|
|
Server: ServerConfig{
|
|
URL: "ws://localhost:8080/ws/daemon",
|
|
APIKey: "change-me-daemon-key",
|
|
},
|
|
Daemon: DaemonConfig{
|
|
SocketPath: "/tmp/ccrm.sock",
|
|
TmuxPrefix: "ccrm-",
|
|
LogLevel: "info",
|
|
LogFile: "~/.local/share/ccrm/daemon.log",
|
|
DataDir: "~/.local/share/ccrm",
|
|
},
|
|
Claude: ClaudeConfig{
|
|
Binary: "claude",
|
|
},
|
|
}
|
|
}
|
|
|
|
func Load() (*Config, error) {
|
|
cfg := DefaultConfig()
|
|
|
|
configPath := filepath.Join(configDir(), "config.yaml")
|
|
data, err := os.ReadFile(configPath)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return cfg, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
if err := yaml.Unmarshal(data, cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
// ExpandPath expands ~ to the user's home directory.
|
|
func ExpandPath(path string) string {
|
|
if len(path) >= 2 && path[:2] == "~/" {
|
|
home, _ := os.UserHomeDir()
|
|
return filepath.Join(home, path[2:])
|
|
}
|
|
return path
|
|
}
|
|
|
|
func configDir() string {
|
|
if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
|
|
return filepath.Join(xdg, "ccrm")
|
|
}
|
|
home, _ := os.UserHomeDir()
|
|
return filepath.Join(home, ".config", "ccrm")
|
|
}
|