-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_cmd.go
204 lines (170 loc) · 4.59 KB
/
config_cmd.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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"strings"
)
type configCmd struct {
Set *configSetCmd `arg:"subcommand"`
Get *configGetCmd `arg:"subcommand"`
Path *configPathCmd `arg:"subcommand"`
}
func (c *configCmd) Execute(ctx context.Context, config *config) error {
switch {
case c.Set != nil:
return c.Set.Execute(ctx, config)
case c.Get != nil:
return c.Get.Execute(ctx, config)
case c.Path != nil:
return c.Path.Execute(ctx, config)
default:
buf := bytes.NewBuffer([]byte{})
enc := json.NewEncoder(buf)
enc.SetIndent("", " ")
err := enc.Encode(config)
if err != nil {
return err
}
fmt.Printf("%s", buf.String())
return nil
}
}
type configPathCmd struct{}
func (c *configPathCmd) Execute(ctx context.Context, config *config) error {
fmt.Printf("%s\n", getConfigPath())
return nil
}
type configGetCmd struct {
Model *struct {
} `arg:"subcommand:model"`
OpenAIAPIKey *struct {
} `arg:"subcommand:openai_api_key"`
OpenAIAPIEndpoint *struct {
} `arg:"subcommand:openai_api_endpoint"`
}
func (c *configGetCmd) Execute(ctx context.Context, config *config) error {
switch {
case c.Model != nil:
return executeGet(config, modelKeyValue{})
case c.OpenAIAPIKey != nil:
return executeGet(config, openaiKeyValue{})
case c.OpenAIAPIEndpoint != nil:
return executeGet(config, openaiEndpointValue{})
default:
return writeHelp(c, os.Stderr)
}
}
type configSetCmd struct {
Model *struct {
Model string `arg:"positional"`
} `arg:"subcommand:model"`
OpenAIAPIKey *struct {
OpenAIAPIKey string `arg:"positional"`
} `arg:"subcommand:openai_api_key"`
OpenAIAPIEndpoint *struct {
OpenAIAPIEndpoint string `arg:"positional"`
} `arg:"subcommand:openai_api_endpoint"`
}
func (c *configSetCmd) Execute(ctx context.Context, config *config) error {
switch {
case c.Model != nil:
return executeSet(config, modelKeyValue{}, c.Model.Model)
case c.OpenAIAPIKey != nil:
return executeSet(config, openaiKeyValue{}, c.OpenAIAPIKey.OpenAIAPIKey)
case c.OpenAIAPIEndpoint != nil:
return executeSet(config, openaiEndpointValue{}, c.OpenAIAPIEndpoint.OpenAIAPIEndpoint)
default:
return writeHelp(c, os.Stderr)
}
}
type configValue interface {
set(config *config, value string) error
get(config *config) string
name() string
}
type openaiEndpointValue struct{}
func (openaiEndpointValue) set(config *config, value string) error {
config.OpenaiAPIEndpoint = value
return nil
}
func (openaiEndpointValue) get(config *config) string {
return config.OpenaiAPIEndpoint
}
func (openaiEndpointValue) fromEnv() string {
val, _ := os.LookupEnv("OPENAI_API_ENDPOINT")
return val
}
func (openaiEndpointValue) name() string {
return "openai api endpoint"
}
type openaiKeyValue struct{}
func (openaiKeyValue) set(config *config, value string) error {
config.OpenAIAPIKey = value
return nil
}
func (openaiKeyValue) get(config *config) string {
return config.OpenAIAPIKey
}
func (openaiKeyValue) fromEnv() string {
val, _ := os.LookupEnv("OPENAI_API_KEY")
return val
}
func (openaiKeyValue) name() string {
return "openai api key"
}
type modelKeyValue struct{}
func (modelKeyValue) set(config *config, value string) error {
config.DefaultModel = value
return nil
}
func (modelKeyValue) get(config *config) string {
return config.DefaultModel
}
func (modelKeyValue) name() string {
return "openai model"
}
func executeSet(config *config, configVal configValue, value string) error {
if env, ok := configVal.(interface{ fromEnv() string }); value == "" && ok {
value = strings.TrimSpace(env.fromEnv())
if value != "" {
fmt.Printf("%s is set from env\n", configVal.name())
}
}
if value == "" {
_, hasEnv := configVal.(interface{ fromEnv() string })
if hasEnv {
fmt.Printf("The %s was not passed in via env or command line argument. Enter it in the following line:\n", configVal.name())
} else {
fmt.Printf("The %s was not passed in via command line argument. Enter it in the following line:\n", configVal.name())
}
var key string
_, err := fmt.Scanf("%s\n", &key)
if err != nil {
return err
}
key = strings.TrimSpace(key)
if key == "" {
return fmt.Errorf("invalid %s", configVal.name())
}
value = key
}
if err := configVal.set(config, value); err != nil {
return err
}
fmt.Printf("saving %s...\n", configVal.name())
if err := config.Write(); err != nil {
return err
}
fmt.Printf("%s stored in cofig\n", configVal.name())
return nil
}
func executeGet(config *config, configVal configValue) error {
if value := configVal.get(config); value != "" {
fmt.Printf("%s\n", value)
return nil
}
return fmt.Errorf("value is empty")
}