-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
config.go
51 lines (43 loc) · 1.92 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
package cmd
import (
"errors"
"strings"
"github.com/appleboy/com/array"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var availableKeys = []string{"openai.api_key", "openai.model", "openai.org_id", "openai.proxy", "output.lang"}
func init() {
configCmd.PersistentFlags().StringP("api_key", "k", "", "openai api key")
configCmd.PersistentFlags().StringP("model", "m", "gpt-3.5-turbo", "openai model")
configCmd.PersistentFlags().StringP("lang", "l", "en", "summarizing language uses English by default")
configCmd.PersistentFlags().StringP("org_id", "o", "", "openai requesting organization")
configCmd.PersistentFlags().StringP("proxy", "", "", "http proxy")
configCmd.PersistentFlags().IntP("diff_unified", "", 3, "generate diffs with <n> lines of context, default is 3")
_ = viper.BindPFlag("openai.org_id", configCmd.PersistentFlags().Lookup("org_id"))
_ = viper.BindPFlag("openai.api_key", configCmd.PersistentFlags().Lookup("api_key"))
_ = viper.BindPFlag("openai.model", configCmd.PersistentFlags().Lookup("model"))
_ = viper.BindPFlag("openai.proxy", configCmd.PersistentFlags().Lookup("proxy"))
_ = viper.BindPFlag("output.lang", configCmd.PersistentFlags().Lookup("lang"))
_ = viper.BindPFlag("git.diff_unified", configCmd.PersistentFlags().Lookup("diff_unified"))
}
var configCmd = &cobra.Command{
Use: "config",
Short: "Add openai config (openai.api_key, openai.model ...)",
Args: cobra.MinimumNArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
if args[0] != "set" {
return errors.New("config set key value. ex: config set openai.api_key sk-...")
}
if !array.InSlice(args[1], availableKeys) {
return errors.New("available key list: " + strings.Join(availableKeys, ", "))
}
viper.Set(args[1], args[2])
if err := viper.WriteConfig(); err != nil {
return err
}
color.Green("you can see the config file: %s", viper.ConfigFileUsed())
return nil
},
}