X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/d1c01b959fb703e604e7cce4e49af411420c5300..f5c601f1801aaa276f8c335fcb51c373edc0b50c:/config.go diff --git a/config.go b/config.go index 2f0bfca..fb24c20 100644 --- a/config.go +++ b/config.go @@ -11,16 +11,18 @@ import ( ) const ( - // endpointChatGPT names the ChatGPT backend with the subscription login. - endpointChatGPT = "chatgpt" + // configEndpointChatGPT names the ChatGPT backend with the subscription + // login. + configEndpointChatGPT = "chatgpt" - // endpointAPI names the OpenAI API with an API key. - endpointAPI = "api" + // configEndpointAPI names the OpenAI API with an API key. + configEndpointAPI = "api" ) // Config holds the settings of config.json in the data directory. type Config struct { Endpoint string `json:"endpoint"` + Voice VoiceConfig `json:"voice"` Compactor ModelConfig `json:"compactor"` } @@ -30,18 +32,6 @@ type ModelConfig struct { Effort string `json:"effort"` } -// defaultConfig returns the settings that apply when config.json does not -// set them. -func defaultConfig() Config { - return Config{ - Endpoint: endpointChatGPT, - Compactor: ModelConfig{ - Model: "gpt-6.1-sol", - Effort: "high", - }, - } -} - // loadConfig reads config.json in dir over the defaults. A missing file gives // the defaults. An unknown field is an error, so a typo does not become a // silent default. @@ -58,6 +48,7 @@ func loadConfig(dir string) (Config, error) { decoder := json.NewDecoder(bytes.NewReader(data)) decoder.DisallowUnknownFields() + err = decoder.Decode(&config) if err == nil && !errors.Is(decoder.Decode(&struct{}{}), io.EOF) { err = errors.New("data after the JSON object") @@ -76,8 +67,8 @@ func loadConfig(dir string) (Config, error) { // validate makes sure that each setting has a valid value. func (c Config) validate() error { - if c.Endpoint != endpointChatGPT && c.Endpoint != endpointAPI { - return fmt.Errorf("endpoint %q is not %q or %q", c.Endpoint, endpointChatGPT, endpointAPI) + if c.Endpoint != configEndpointChatGPT && c.Endpoint != configEndpointAPI { + return fmt.Errorf("endpoint %q is not %q or %q", c.Endpoint, configEndpointChatGPT, configEndpointAPI) } if c.Compactor.Model == "" || c.Compactor.Effort == "" { @@ -87,40 +78,14 @@ func (c Config) validate() error { return nil } -// dataDir returns the data directory and creates it with mode 0700 when it -// does not exist. It is ONECHAT_DIR when that is set. Otherwise it is -// $XDG_DATA_HOME/onechat when XDG_DATA_HOME is an absolute path, or else -// ~/.local/share/onechat. -func dataDir() (string, error) { - dir := os.Getenv("ONECHAT_DIR") - if dir == "" { - var err error - dir, err = defaultDataDir() - if err != nil { - return "", err - } - } - - err := os.MkdirAll(dir, 0o700) - if err != nil { - return "", fmt.Errorf("create data directory: %w", err) - } - - return dir, nil -} - -// defaultDataDir returns the data directory for an unset ONECHAT_DIR. The XDG -// specification says to ignore a relative XDG_DATA_HOME. -func defaultDataDir() (string, error) { - xdg := os.Getenv("XDG_DATA_HOME") - if filepath.IsAbs(xdg) { - return filepath.Join(xdg, "onechat"), nil - } - - home, err := os.UserHomeDir() - if err != nil { - return "", fmt.Errorf("find data directory: set ONECHAT_DIR: %w", err) +// defaultConfig returns the settings that apply when config.json does not +// set them. +func defaultConfig() Config { + return Config{ + Endpoint: configEndpointChatGPT, + Compactor: ModelConfig{ + Model: "gpt-6.1-sol", + Effort: "high", + }, } - - return filepath.Join(home, ".local", "share", "onechat"), nil }