-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
227 lines (183 loc) · 4.19 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package oax
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"github.com/pelletier/go-toml"
)
type Settings struct {
Setting Setting `toml:"setting"`
Chat Chat `toml:"chat"`
}
type Setting struct {
Editor string `toml:"editor"`
ChatLogDir string `toml:"chatLogDir"`
}
type Chat struct {
Model string `toml:"model"`
Templates []ChatTemplate `toml:"templates"`
FileNameFormat string `toml:"fileNameFormat"`
}
type ChatTemplate struct {
Name string `toml:"name"`
Messages []Message `toml:"messages"`
}
type Message struct {
Role string `toml:"role"`
Content string `toml:"content"`
}
type Profile struct {
Name string `toml:"name"`
Description string `toml:"description"`
ApiKey string `toml:"apiKey"`
OrganizationID string `toml:"organizationId"`
Default bool `toml:"default"`
}
type ProfileToml struct {
Profiles []Profile `toml:"profiles"`
}
type Config struct {
Profiles []Profile
Settings Settings
}
var (
settingFilePath string
profileFilePath string
chatLogDirDefaultPath string
)
func init() {
configDir, err := getConfigDir()
if err != nil {
panic(err)
}
settingFilePath = filepath.Join(configDir, "settings.toml")
profileFilePath = filepath.Join(configDir, "profiles.toml")
chatLogDirDefaultPath = filepath.Join(configDir, "chat-log")
}
func GetConfig() (*Config, error) {
setting, err := loadSetting()
if err != nil {
return nil, fmt.Errorf("error load setting: %w", err)
}
profiles, err := loadProfiles()
if err != nil {
return nil, fmt.Errorf("error load profile: %w", err)
}
chatLogDir, err := createIfNotExistChatLogDir(setting.Setting.ChatLogDir)
if err != nil {
return nil, fmt.Errorf("error create chat log dir: %w", err)
}
setting.Setting.ChatLogDir = chatLogDir
return &Config{
Settings: *setting,
Profiles: profiles.Profiles,
}, nil
}
func getConfigDir() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
var configDir string
if runtime.GOOS == "windows" {
configDir = filepath.Join(usr.HomeDir, "AppData", "Roaming", "oax")
} else {
configDir = filepath.Join(usr.HomeDir, ".config", "oax")
}
err = os.MkdirAll(configDir, 0755)
if err != nil {
return "", err
}
return configDir, nil
}
func loadSetting() (*Settings, error) {
_, err := os.Stat(settingFilePath)
var configTree *toml.Tree
if os.IsNotExist(err) {
settingStr := `[setting]
editor = "vim"`
err = ioutil.WriteFile(settingFilePath, []byte(settingStr), 0644)
if err != nil {
return nil, err
}
configTree, err = toml.Load(settingStr)
if err != nil {
return nil, err
}
} else {
configTree, err = toml.LoadFile(settingFilePath)
if err != nil {
return nil, err
}
}
setting := &Settings{}
err = configTree.Unmarshal(setting)
if err != nil {
return nil, err
}
return setting, nil
}
func loadProfiles() (*ProfileToml, error) {
_, err := os.Stat(profileFilePath)
var configTree *toml.Tree
if os.IsNotExist(err) {
settingStr := `[[profiles]]
name = "personal"
apiKey = "sk-xxxx"
default = true
`
err = ioutil.WriteFile(profileFilePath, []byte(settingStr), 0644)
if err != nil {
return nil, err
}
configTree, err = toml.Load(settingStr)
if err != nil {
return nil, err
}
} else {
configTree, err = toml.LoadFile(profileFilePath)
if err != nil {
return nil, err
}
}
profile := &ProfileToml{}
err = configTree.Unmarshal(profile)
if err != nil {
return nil, err
}
return profile, nil
}
func createIfNotExistChatLogDir(chatLogDir string) (string, error) {
if chatLogDir == "" {
chatLogDir = chatLogDirDefaultPath
} else {
chatLogDir, err := replaceTildeWithHomedir(chatLogDir)
if err != nil {
return chatLogDir, err
}
}
if err := createDirIfNotExist(chatLogDir); err != nil {
return "", err
}
return chatLogDir, nil
}
func CmdConfig(editor string, setting bool, profile bool) error {
var cmd *exec.Cmd
if setting {
cmd = exec.Command(editor, settingFilePath)
} else {
cmd = exec.Command(editor, profileFilePath)
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return err
}
return nil
}