-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
87 lines (69 loc) · 2.08 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"encoding/json"
"os"
"path/filepath"
"reflect"
log "github.com/sirupsen/logrus"
)
type ProgramConfig struct {
APIKeys map[string]string `json:"apikeys"`
Engine string `json:"engine"`
SummarizePrompt string `json:"summarizeprompt"`
ProviderModel map[string]string `json:"providermodel"`
PrintAIEngineTemplate string `json:"printaiengine"`
LogLevel string `json:"loglevel"`
LogDir string `json:"logdir"`
LogFormatter string `json:"logformat"`
configFilePath string // don't serialize this
}
func initProgramConfig() (*ProgramConfig, error) {
userProgramDir, err := getProgramUserDir()
if err != nil {
return nil, err
}
configDir := filepath.Join(
userProgramDir,
defaultConfigDir)
var config ProgramConfig
config.configFilePath = filepath.Join(
configDir,
programName+"."+defaultConfigFileExtension)
config.Engine = defaultEngine
config.SummarizePrompt = defaultSummarizePrompt
config.ProviderModel = defaultProviderModel
config.PrintAIEngineTemplate = defaultPrintAIEngineTemplate
data, err := os.ReadFile(config.configFilePath)
if err == nil {
err = json.Unmarshal(data, &config)
if err != nil {
log.Warningf("failed to deserialize config file: %v", err)
}
} else {
log.Warningf("failed to read config file: %v", err)
}
if config.LogDir == "" {
config.LogDir = filepath.Join(
userProgramDir,
defaultLogDir)
}
initLoggingToFile(config)
return &config, nil
}
func initAPIKeysConfig(progOptions ProgramOptions, config *ProgramConfig) error {
newAPIKeys, err := processMissedAPIKeys(config.APIKeys, progOptions.engines)
if err != nil {
return err
}
if !reflect.DeepEqual(config.APIKeys, newAPIKeys) {
config.APIKeys = newAPIKeys
data, err := json.MarshalIndent(config, "", " ")
if err == nil {
err = os.WriteFile(config.configFilePath, data, 0600)
}
if err != nil {
log.Warningf("failed to write to config file: %v", err)
}
}
return nil
}