package cli import ( "fmt" "os" "path/filepath" "github.com/go-yaml/yaml" ) type Config struct { OpenAI struct { APIKey string `yaml:"apiKey"` } `yaml:"openai"` } func getConfigDir() string { var configDir string xdgConfigHome := os.Getenv("XDG_CONFIG_HOME") if xdgConfigHome != "" { configDir = filepath.Join(xdgConfigHome, "lmcli") } else { userHomeDir, _ := os.UserHomeDir() configDir = filepath.Join(userHomeDir, ".config/lmcli") } os.MkdirAll(configDir, 0755) return configDir } func NewConfig() (*Config, error) { configFile := filepath.Join(getConfigDir(), "config.yaml") configBytes, err := os.ReadFile(configFile) if os.IsNotExist(err) { defaultConfig := &Config{} defaultConfig.OpenAI.APIKey = "your_key_here" file, err := os.Create(configFile) if err != nil { return nil, fmt.Errorf("Could not open config file for writing: %v", err) } fmt.Printf("Writing default configuration to: %s\n", configFile) bytes, _ := yaml.Marshal(defaultConfig) _, err = file.Write(bytes) if err != nil { return nil, fmt.Errorf("Could not save default configuration: %v", err) } } else if err != nil { return nil, fmt.Errorf("Could not read config file: %v", err) } config := &Config{} yaml.Unmarshal(configBytes, config) return config, nil }