-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
314 lines (291 loc) · 11.9 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package main
import (
"fmt"
"os"
"runtime"
"strings"
"gopkg.in/yaml.v3"
)
// ProgConfig represents program configuration
type ProgConfig struct {
GeminiAPIKey string `yaml:"GeminiAPIKey"`
GeminiAiModel string `yaml:"GeminiAiModel"`
GeminiCandidateCount int32 `yaml:"GeminiCandidateCount"`
GeminiMaxOutputTokens int32 `yaml:"GeminiMaxOutputTokens"`
GeminiTemperature float32 `yaml:"GeminiTemperature"`
GeminiTopP float32 `yaml:"GeminiTopP"`
GeminiTopK int32 `yaml:"GeminiTopK"`
GeminiSystemInstruction string `yaml:"GeminiSystemInstruction"`
GeminiMaxWaitTimeFileProcessing int `yaml:"GeminiMaxWaitTimeFileProcessing"`
//
MarkdownPromptResponseFile string `yaml:"MarkdownPromptResponseFile"`
MarkdownOutput bool `yaml:"MarkdownOutput"`
MarkdownOutputApplication string
MarkdownOutputApplicationMacOS string `yaml:"MarkdownOutputApplicationMacOS"`
MarkdownOutputApplicationLinux string `yaml:"MarkdownOutputApplicationLinux"`
MarkdownOutputApplicationWindows string `yaml:"MarkdownOutputApplicationWindows"`
MarkdownOutputApplicationOther string `yaml:"MarkdownOutputApplicationOther"`
MarkdownHistory bool `yaml:"MarkdownHistory"`
MarkdownHistoryDirectory string `yaml:"MarkdownHistoryDirectory"`
//
AnsiRendering bool `yaml:"AnsiRendering"`
AnsiPromptResponseFile string `yaml:"AnsiPromptResponseFile"`
AnsiOutput bool `yaml:"AnsiOutput"`
AnsiHistory bool `yaml:"AnsiHistory"`
AnsiHistoryDirectory string `yaml:"AnsiHistoryDirectory"`
AnsiReplaceColors []map[string]string `yaml:"AnsiReplaceColors"`
//
HTMLRendering bool `yaml:"HTMLRendering"`
HTMLPromptResponseFile string `yaml:"HTMLPromptResponseFile"`
HTMLOutput bool `yaml:"HTMLOutput"`
HTMLOutputApplication string
HTMLOutputApplicationMacOS string `yaml:"HTMLOutputApplicationMacOS"`
HTMLOutputApplicationLinux string `yaml:"HTMLOutputApplicationLinux"`
HTMLOutputApplicationWindows string `yaml:"HTMLOutputApplicationWindows"`
HTMLOutputApplicationOther string `yaml:"HTMLOutputApplicationOther"`
HTMLHistory bool `yaml:"HTMLHistory"`
HTMLHistoryDirectory string `yaml:"HTMLHistoryDirectory"`
HTMLMaxLengthTitle int `yaml:"HTMLMaxLengthTitle"`
HTMLReplaceElements []map[string]string `yaml:"HTMLReplaceElements"`
HTMLHeader string `yaml:"HTMLHeader"`
HTMLFooter string `yaml:"HTMLFooter"`
//
InputFromTerminal bool `yaml:"InputFromTerminal"`
InputFromFile bool `yaml:"InputFromFile"`
InputFile string `yaml:"InputFile"`
InputFromLocalhost bool `yaml:"InputFromLocalhost"`
InputLocalhostPort int `yaml:"InputLocalhostPort"`
//
NotifyPrompt bool `yaml:"NotifyPrompt"`
NotifyPromptApplication string
NotifyPromptApplicationMacOS string `yaml:"NotifyPromptApplicationMacOS"`
NotifyPromptApplicationLinux string `yaml:"NotifyPromptApplicationLinux"`
NotifyPromptApplicationWindows string `yaml:"NotifyPromptApplicationWindows"`
NotifyPromptApplicationOther string `yaml:"NotifyPromptApplicationOther"`
NotifyResponse bool `yaml:"NotifyResponse"`
NotifyResponseApplication string
NotifyResponseApplicationMacOS string `yaml:"NotifyResponseApplicationMacOS"`
NotifyResponseApplicationLinux string `yaml:"NotifyResponseApplicationLinux"`
NotifyResponseApplicationWindows string `yaml:"NotifyResponseApplicationWindows"`
NotifyResponseApplicationOther string `yaml:"NotifyResponseApplicationOther"`
//
HistoryFilenameSchema string `yaml:"HistoryFilenameSchema"`
HistoryFilenameAddPrefix bool `yaml:"HistoryFilenameAddPrefix"`
HistoryFilenameAddPostfix bool `yaml:"HistoryFilenameAddPostfix"`
HistoryFilenameExtensionMarkdown string `yaml:"HistoryFilenameExtensionMarkdown"`
HistoryFilenameExtensionAnsi string `yaml:"HistoryFilenameExtensionAnsi"`
HistoryFilenameExtensionHTML string `yaml:"HistoryFilenameExtensionHTML"`
HistoryMaxFilenameLength int `yaml:"HistoryMaxFilenameLength"`
//
GeneralInternetProxy string `yaml:"GeneralInternetProxy"`
}
// progConfig contains program configuration
var progConfig = ProgConfig{GeminiCandidateCount: -1, GeminiTemperature: -1.0, GeminiTopP: -1.0, GeminiTopK: -1, GeminiMaxOutputTokens: -1}
/*
loadConfiguration loads program configuration from yaml file.
*/
func loadConfiguration(configFile string) error {
operatingSystem := runtime.GOOS
source, err := os.ReadFile(configFile)
if err != nil {
return fmt.Errorf("error [%w] reading configuration file", err)
}
err = yaml.Unmarshal(source, &progConfig)
if err != nil {
return fmt.Errorf("error [%w] unmarshalling configuration file", err)
}
// gemini
if progConfig.GeminiAPIKey == "" {
return fmt.Errorf("empty GeminiAPIKey not allowed")
}
if progConfig.GeminiAiModel == "" {
return fmt.Errorf("empty GeminiAiModel not allowed")
}
if progConfig.GeminiCandidateCount <= 0 {
return fmt.Errorf("empty GeminiCandidateCount not allowed")
}
// markdown
if progConfig.MarkdownPromptResponseFile == "" {
return fmt.Errorf("empty MarkdownPromptResponseFile not allowed")
}
switch operatingSystem {
case "darwin":
progConfig.MarkdownOutputApplication = progConfig.MarkdownOutputApplicationMacOS
case "linux":
progConfig.MarkdownOutputApplication = progConfig.MarkdownOutputApplicationLinux
case "windows":
progConfig.MarkdownOutputApplication = progConfig.MarkdownOutputApplicationWindows
default:
progConfig.MarkdownOutputApplication = progConfig.MarkdownOutputApplicationOther
}
if progConfig.MarkdownOutput && progConfig.MarkdownOutputApplication == "" {
return fmt.Errorf("empty operating system specific MarkdownOutputApplication not allowed")
}
if progConfig.MarkdownHistory && progConfig.MarkdownHistoryDirectory == "" {
return fmt.Errorf("empty MarkdownHistoryDirectory not allowed")
}
// ansi
if progConfig.AnsiRendering && progConfig.AnsiPromptResponseFile == "" {
return fmt.Errorf("empty AnsiPromptResponseFile not allowed")
}
if progConfig.AnsiHistory && progConfig.AnsiHistoryDirectory == "" {
return fmt.Errorf("empty AnsiHistoryDirectory not allowed")
}
// html
if progConfig.HTMLRendering && progConfig.HTMLPromptResponseFile == "" {
return fmt.Errorf("empty HTMLPromptResponseFile not allowed")
}
switch operatingSystem {
case "darwin":
progConfig.HTMLOutputApplication = progConfig.HTMLOutputApplicationMacOS
case "linux":
progConfig.HTMLOutputApplication = progConfig.HTMLOutputApplicationLinux
case "windows":
progConfig.HTMLOutputApplication = progConfig.HTMLOutputApplicationWindows
default:
progConfig.HTMLOutputApplication = progConfig.HTMLOutputApplicationOther
}
if progConfig.HTMLOutput && progConfig.HTMLOutputApplication == "" {
return fmt.Errorf("empty operating system specific HTMLOutputApplication not allowed")
}
if progConfig.HTMLHistory && progConfig.HTMLHistoryDirectory == "" {
return fmt.Errorf("empty HTMLHistoryDirectory not allowed")
}
// input
if progConfig.InputFromFile && progConfig.InputFile == "" {
return fmt.Errorf("empty InputFile not allowed")
}
// notification
switch operatingSystem {
case "darwin":
progConfig.NotifyPromptApplication = progConfig.NotifyPromptApplicationMacOS
case "linux":
progConfig.NotifyPromptApplication = progConfig.NotifyPromptApplicationLinux
case "windows":
progConfig.NotifyPromptApplication = progConfig.NotifyPromptApplicationWindows
default:
progConfig.NotifyPromptApplication = progConfig.NotifyPromptApplicationOther
}
if progConfig.NotifyPrompt && progConfig.NotifyPromptApplication == "" {
return fmt.Errorf("empty operating system specific NotifyPromptApplication not allowed")
}
switch operatingSystem {
case "darwin":
progConfig.NotifyResponseApplication = progConfig.NotifyResponseApplicationMacOS
case "linux":
progConfig.NotifyResponseApplication = progConfig.NotifyResponseApplicationLinux
case "windows":
progConfig.NotifyResponseApplication = progConfig.NotifyResponseApplicationWindows
default:
progConfig.NotifyResponseApplication = progConfig.NotifyResponseApplicationOther
}
if progConfig.NotifyResponse && progConfig.NotifyResponseApplication == "" {
return fmt.Errorf("empty operating system specific NotifyResponseApplication not allowed")
}
// history
progConfig.HistoryFilenameSchema = strings.ToLower(progConfig.HistoryFilenameSchema)
switch progConfig.HistoryFilenameSchema {
case "timestamp":
case "prompt":
default:
return fmt.Errorf("unsupported history filename schema")
}
if progConfig.HistoryMaxFilenameLength > 255 {
return fmt.Errorf("max length of history filename show not be greater than 255")
}
// get api-key (password)
progConfig.GeminiAPIKey, err = getPassword(progConfig.GeminiAPIKey)
if err != nil {
return fmt.Errorf("error [%w] getting api-key", err)
}
// get internet proxy (password)
if progConfig.GeneralInternetProxy != "" {
progConfig.GeneralInternetProxy, err = getPassword(progConfig.GeneralInternetProxy)
if err != nil {
return fmt.Errorf("error [%w] getting internet proxy", err)
}
}
return nil
}
/*
showConfiguration shows loaded configuration.
*/
func showConfiguration() {
// general notes
fmt.Printf("\nNotes concerning the freely available version of 'Google Gemini AI':\n")
fmt.Printf(" See the help page for the 'Google Gemini AI' terms of service.\n")
fmt.Printf(" All input data will be used by Google to improve 'Gemini AI'.\n")
fmt.Printf(" Therefore, do not process any private or confidential data.\n")
fmt.Printf("\nInput from:\n")
if progConfig.InputFromTerminal {
fmt.Printf(" Terminal : yes\n")
}
if progConfig.InputFromFile {
fmt.Printf(" File : %v\n", progConfig.InputFile)
}
if progConfig.InputFromLocalhost {
fmt.Printf(" localhost : %v (port)\n", progConfig.InputLocalhostPort)
}
fmt.Printf("\nRendering:\n")
fmt.Printf(" Markdown : %v\n", progConfig.MarkdownPromptResponseFile)
if progConfig.AnsiRendering {
fmt.Printf(" Ansi : %v\n", progConfig.AnsiPromptResponseFile)
}
if progConfig.HTMLRendering {
fmt.Printf(" HTML : %v\n", progConfig.HTMLPromptResponseFile)
}
fmt.Printf("\nHistory:\n")
if progConfig.MarkdownHistory {
fmt.Printf(" Markdown : %v\n", progConfig.MarkdownHistoryDirectory)
}
if progConfig.AnsiHistory {
fmt.Printf(" Ansi : %v\n", progConfig.AnsiHistoryDirectory)
}
if progConfig.HTMLHistory {
fmt.Printf(" HTML : %v\n", progConfig.HTMLHistoryDirectory)
}
fmt.Printf("\nOutput:\n")
if progConfig.AnsiOutput {
fmt.Printf(" Terminal : yes\n")
}
if progConfig.MarkdownOutput {
fmt.Printf(" Markdown : execute application\n")
}
if progConfig.HTMLOutput {
fmt.Printf(" HTML : execute application\n")
}
}
/*
initializeProgram initializes this program.
*/
func initializeProgram() {
var err error
// create history directories
if progConfig.MarkdownHistory {
err = os.Mkdir(progConfig.MarkdownHistoryDirectory, 0750)
if err != nil && !os.IsExist(err) {
fmt.Printf("error [%v] at os.Mkdir()\n", err)
os.Exit(1)
}
}
if progConfig.AnsiHistory {
err = os.Mkdir(progConfig.AnsiHistoryDirectory, 0750)
if err != nil && !os.IsExist(err) {
fmt.Printf("error [%v] at os.Mkdir()\n", err)
os.Exit(1)
}
}
if progConfig.HTMLHistory {
err = os.Mkdir(progConfig.HTMLHistoryDirectory, 0750)
if err != nil && !os.IsExist(err) {
fmt.Printf("error [%v] at os.Mkdir()\n", err)
os.Exit(1)
}
err = os.Mkdir(progConfig.HTMLHistoryDirectory+"/assets", 0750)
if err != nil && !os.IsExist(err) {
fmt.Printf("error [%v] at os.Mkdir()\n", err)
os.Exit(1)
}
writeAssets(progConfig.HTMLHistoryDirectory)
}
}