2023-11-04 12:20:13 -06:00
|
|
|
package cli
|
2023-11-04 11:30:03 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/go-yaml/yaml"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2023-11-21 21:45:06 -07:00
|
|
|
ModelDefaults *struct {
|
|
|
|
SystemPrompt *string `yaml:"systemPrompt" default:"You are a helpful assistant."`
|
|
|
|
} `yaml:"modelDefaults"`
|
2023-11-18 18:14:00 -07:00
|
|
|
OpenAI *struct {
|
2023-11-28 22:27:58 -07:00
|
|
|
APIKey *string `yaml:"apiKey" default:"your_key_here"`
|
|
|
|
DefaultModel *string `yaml:"defaultModel" default:"gpt-4"`
|
|
|
|
DefaultMaxLength *int `yaml:"defaultMaxLength" default:"256"`
|
|
|
|
EnabledTools []string `yaml:"enabledTools"`
|
2023-11-04 11:30:03 -06:00
|
|
|
} `yaml:"openai"`
|
2023-11-18 22:00:59 -07:00
|
|
|
Chroma *struct {
|
|
|
|
Style *string `yaml:"style" default:"onedark"`
|
|
|
|
Formatter *string `yaml:"formatter" default:"terminal16m"`
|
|
|
|
} `yaml:"chroma"`
|
2023-11-04 11:30:03 -06:00
|
|
|
}
|
|
|
|
|
2024-01-01 21:31:21 -07:00
|
|
|
func configDir() string {
|
2023-11-04 11:30:03 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-05 10:44:16 -07:00
|
|
|
func NewConfig() (*Config, error) {
|
2024-01-01 21:31:21 -07:00
|
|
|
configFile := filepath.Join(configDir(), "config.yaml")
|
2023-11-18 18:14:00 -07:00
|
|
|
shouldWriteDefaults := false
|
|
|
|
c := &Config{}
|
2023-11-04 11:30:03 -06:00
|
|
|
|
2023-11-28 22:54:05 -07:00
|
|
|
configExists := true
|
2023-11-04 11:30:03 -06:00
|
|
|
configBytes, err := os.ReadFile(configFile)
|
|
|
|
if os.IsNotExist(err) {
|
2023-11-28 22:54:05 -07:00
|
|
|
configExists = false
|
2023-11-18 18:14:00 -07:00
|
|
|
} else if err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not read config file: %v", err)
|
|
|
|
} else {
|
|
|
|
yaml.Unmarshal(configBytes, c)
|
|
|
|
}
|
2023-11-04 11:30:03 -06:00
|
|
|
|
2023-11-18 18:14:00 -07:00
|
|
|
shouldWriteDefaults = SetStructDefaults(c)
|
2023-11-28 22:54:05 -07:00
|
|
|
if !configExists || shouldWriteDefaults {
|
|
|
|
if configExists {
|
|
|
|
fmt.Printf("Saving new defaults to configuration, backing up existing configuration to %s\n", configFile + ".bak")
|
|
|
|
os.Rename(configFile, configFile + ".bak")
|
|
|
|
}
|
|
|
|
fmt.Printf("Writing configuration file to %s\n", configFile)
|
2023-11-04 11:30:03 -06:00
|
|
|
file, err := os.Create(configFile)
|
|
|
|
if err != nil {
|
2023-11-05 10:44:16 -07:00
|
|
|
return nil, fmt.Errorf("Could not open config file for writing: %v", err)
|
2023-11-04 11:30:03 -06:00
|
|
|
}
|
2023-11-18 18:14:00 -07:00
|
|
|
bytes, _ := yaml.Marshal(c)
|
2023-11-04 11:30:03 -06:00
|
|
|
_, err = file.Write(bytes)
|
|
|
|
if err != nil {
|
2023-11-05 10:44:16 -07:00
|
|
|
return nil, fmt.Errorf("Could not save default configuration: %v", err)
|
2023-11-04 11:30:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 18:14:00 -07:00
|
|
|
return c, nil
|
2023-11-04 11:30:03 -06:00
|
|
|
}
|