File tree 1 file changed +23
-6
lines changed 1 file changed +23
-6
lines changed Original file line number Diff line number Diff line change @@ -234,9 +234,9 @@ type runOptions struct {
234
234
235
235
func main () {
236
236
var (
237
- opts runOptions
238
- openAIKey string
239
- doSaveKey bool
237
+ opts runOptions
238
+ cliOpenAIKey string
239
+ doSaveKey bool
240
240
)
241
241
cmd := & serpent.Command {
242
242
Use : "aicommit [ref]" ,
@@ -246,15 +246,31 @@ func main() {
246
246
if err != nil && ! os .IsNotExist (err ) {
247
247
return err
248
248
}
249
- if savedKey != "" && openAIKey == "" {
249
+ var openAIKey string
250
+ if savedKey != "" && cliOpenAIKey == "" {
250
251
openAIKey = savedKey
252
+ } else if cliOpenAIKey != "" {
253
+ openAIKey = cliOpenAIKey
251
254
}
255
+
256
+ if savedKey != "" && cliOpenAIKey != "" {
257
+ openAIKeyOpt := inv .Command .Options .ByName ("openai-key" )
258
+ if openAIKeyOpt == nil {
259
+ panic ("openai-key option not found" )
260
+ }
261
+ // savedKey overrides cliOpenAIKey only when set via environment.
262
+ // See https://github.com/coder/aicommit/issues/6.
263
+ if openAIKeyOpt .ValueSource == serpent .ValueSourceEnv {
264
+ openAIKey = savedKey
265
+ }
266
+ }
267
+
252
268
if openAIKey == "" {
253
269
return errors .New ("$OPENAI_API_KEY is not set" )
254
270
}
255
271
256
272
if doSaveKey {
257
- err := saveKey (openAIKey )
273
+ err := saveKey (cliOpenAIKey )
258
274
if err != nil {
259
275
return err
260
276
}
@@ -280,7 +296,8 @@ func main() {
280
296
Name : "openai-key" ,
281
297
Description : "The OpenAI API key to use." ,
282
298
Env : "OPENAI_API_KEY" ,
283
- Value : serpent .StringOf (& openAIKey ),
299
+ Flag : "openai-key" ,
300
+ Value : serpent .StringOf (& cliOpenAIKey ),
284
301
},
285
302
{
286
303
Name : "model" ,
You can’t perform that action at this time.
0 commit comments