Skip to content

Commit 8b6858d

Browse files
authored
chore(cmd): support config list and set for sub command. (#124)
* chore(cmd): support config list and set for sub command Signed-off-by: appleboy <appleboy.tw@gmail.com> * docs: update Signed-off-by: appleboy <appleboy.tw@gmail.com> --------- Signed-off-by: appleboy <appleboy.tw@gmail.com>
1 parent edb9f5e commit 8b6858d

5 files changed

Lines changed: 119 additions & 111 deletions

File tree

cmd/config.go

Lines changed: 1 addition & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,10 @@
11
package cmd
22

33
import (
4-
"errors"
5-
"strings"
6-
"time"
7-
8-
"github.com/appleboy/com/array"
9-
"github.com/fatih/color"
104
"github.com/spf13/cobra"
11-
"github.com/spf13/viper"
125
)
136

14-
var availableKeys = []string{
15-
"git.diff_unified",
16-
"git.exclude_list",
17-
"git.template_file",
18-
"git.template_string",
19-
"openai.socks",
20-
"openai.api_key",
21-
"openai.model",
22-
"openai.org_id",
23-
"openai.proxy",
24-
"output.lang",
25-
"openai.base_url",
26-
"openai.timeout",
27-
"openai.max_tokens",
28-
"openai.temperature",
29-
"openai.provider",
30-
"openai.model_name",
31-
"openai.skip_verify",
32-
"openai.headers",
33-
"openai.api_version",
34-
"openai.top_p",
35-
"openai.frequency_penalty",
36-
"openai.presence_penalty",
37-
}
38-
39-
func init() {
40-
configCmd.PersistentFlags().StringP("base_url", "b", "", "what API base url to use.")
41-
configCmd.PersistentFlags().StringP("api_key", "k", "", "openai api key")
42-
configCmd.PersistentFlags().StringP("model", "m", "gpt-3.5-turbo", "openai model")
43-
configCmd.PersistentFlags().StringP("lang", "l", "en", "summarizing language uses English by default")
44-
configCmd.PersistentFlags().StringP("org_id", "o", "", "openai requesting organization")
45-
configCmd.PersistentFlags().StringP("proxy", "", "", "http proxy")
46-
configCmd.PersistentFlags().StringP("socks", "", "", "socks proxy")
47-
configCmd.PersistentFlags().DurationP("timeout", "t", 10*time.Second, "http timeout")
48-
configCmd.PersistentFlags().StringP("template_file", "", "", "git commit message file")
49-
configCmd.PersistentFlags().StringP("template_string", "", "", "git commit message string")
50-
configCmd.PersistentFlags().IntP("diff_unified", "", 3, "generate diffs with <n> lines of context, default is 3")
51-
configCmd.PersistentFlags().IntP("max_tokens", "", 300, "the maximum number of tokens to generate in the chat completion.")
52-
configCmd.PersistentFlags().Float32P("temperature", "", 1.0, "What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.")
53-
configCmd.PersistentFlags().Float32P("top_p", "", 1.0, "An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.")
54-
configCmd.PersistentFlags().Float32P("frequency_penalty", "", 0.0, "Number between 0.0 and 1.0 that penalizes new tokens based on their existing frequency in the text so far. Decreases the model's likelihood to repeat the same line verbatim.")
55-
configCmd.PersistentFlags().Float32P("presence_penalty", "", 0.0, "Number between 0.0 and 1.0 that penalizes new tokens based on whether they appear in the text so far. Increases the model's likelihood to talk about new topics.")
56-
configCmd.PersistentFlags().StringP("exclude_list", "", "", "exclude file from `git diff` command")
57-
58-
configCmd.PersistentFlags().StringP("provider", "", "openai", "service provider, only support 'openai' or 'azure'")
59-
configCmd.PersistentFlags().StringP("model_name", "", "", "model deployment name for Azure cognitive service")
60-
configCmd.PersistentFlags().BoolP("skip_verify", "", false, "skip verify TLS certificate")
61-
configCmd.PersistentFlags().StringP("headers", "", "", "custom headers for openai request")
62-
configCmd.PersistentFlags().StringP("api_version", "", "", "openai api version")
63-
64-
_ = viper.BindPFlag("openai.base_url", configCmd.PersistentFlags().Lookup("base_url"))
65-
_ = viper.BindPFlag("openai.org_id", configCmd.PersistentFlags().Lookup("org_id"))
66-
_ = viper.BindPFlag("openai.api_key", configCmd.PersistentFlags().Lookup("api_key"))
67-
_ = viper.BindPFlag("openai.model", configCmd.PersistentFlags().Lookup("model"))
68-
_ = viper.BindPFlag("openai.proxy", configCmd.PersistentFlags().Lookup("proxy"))
69-
_ = viper.BindPFlag("openai.socks", configCmd.PersistentFlags().Lookup("socks"))
70-
_ = viper.BindPFlag("openai.timeout", configCmd.PersistentFlags().Lookup("timeout"))
71-
_ = viper.BindPFlag("openai.max_tokens", configCmd.PersistentFlags().Lookup("max_tokens"))
72-
_ = viper.BindPFlag("openai.temperature", configCmd.PersistentFlags().Lookup("temperature"))
73-
_ = viper.BindPFlag("openai.top_p", configCmd.PersistentFlags().Lookup("top_p"))
74-
_ = viper.BindPFlag("openai.frequency_penalty", configCmd.PersistentFlags().Lookup("frequency_penalty"))
75-
_ = viper.BindPFlag("openai.presence_penalty", configCmd.PersistentFlags().Lookup("presence_penalty"))
76-
_ = viper.BindPFlag("output.lang", configCmd.PersistentFlags().Lookup("lang"))
77-
_ = viper.BindPFlag("git.diff_unified", configCmd.PersistentFlags().Lookup("diff_unified"))
78-
_ = viper.BindPFlag("git.exclude_list", configCmd.PersistentFlags().Lookup("exclude_list"))
79-
_ = viper.BindPFlag("git.template_file", configCmd.PersistentFlags().Lookup("template_file"))
80-
_ = viper.BindPFlag("git.template_string", configCmd.PersistentFlags().Lookup("template_string"))
81-
82-
_ = viper.BindPFlag("openai.provider", configCmd.PersistentFlags().Lookup("provider"))
83-
_ = viper.BindPFlag("openai.model_name", configCmd.PersistentFlags().Lookup("model_name"))
84-
_ = viper.BindPFlag("openai.skip_verify", configCmd.PersistentFlags().Lookup("skip_verify"))
85-
_ = viper.BindPFlag("openai.headers", configCmd.PersistentFlags().Lookup("headers"))
86-
_ = viper.BindPFlag("openai.api_version", configCmd.PersistentFlags().Lookup("api_version"))
87-
}
88-
897
var configCmd = &cobra.Command{
908
Use: "config",
91-
Short: "Add openai config (openai.api_key, openai.model ...)",
92-
Args: cobra.MinimumNArgs(3),
93-
RunE: func(cmd *cobra.Command, args []string) error {
94-
// Check if command is 'set'
95-
if args[0] != "set" {
96-
return errors.New("config set key value. ex: config set openai.api_key sk-...")
97-
}
98-
99-
// Check if key is available
100-
if !array.InSlice(args[1], availableKeys) {
101-
return errors.New("available key list: " + strings.Join(availableKeys, ", "))
102-
}
103-
104-
// Set config value in viper
105-
if args[1] == "git.exclude_list" {
106-
viper.Set(args[1], strings.Split(args[2], ","))
107-
} else {
108-
viper.Set(args[1], args[2])
109-
}
110-
111-
// Write config to file
112-
if err := viper.WriteConfig(); err != nil {
113-
return err
114-
}
115-
116-
// Print success message with config file location
117-
color.Green("you can see the config file: %s", viper.ConfigFileUsed())
118-
return nil
119-
},
9+
Short: "custom config (openai.api_key, openai.model ...)",
12010
}

cmd/config_list.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cmd
2+
3+
import (
4+
"sort"
5+
6+
"github.com/fatih/color"
7+
"github.com/rodaine/table"
8+
"github.com/spf13/cobra"
9+
"github.com/spf13/viper"
10+
)
11+
12+
func init() {
13+
configCmd.AddCommand(configListCmd)
14+
}
15+
16+
// availableKeys is a list of available config keys
17+
var availableKeys = map[string]string{
18+
"git.diff_unified": "generate diffs with <n> lines of context, default is 3",
19+
"git.exclude_list": "exclude file from git diff command",
20+
"git.template_file": "template file for commit message",
21+
"git.template_string": "template string for commit message",
22+
"openai.socks": "socks proxy",
23+
"openai.api_key": "openai api key",
24+
"openai.model": "openai model",
25+
"openai.org_id": "openai requesting organization",
26+
"openai.proxy": "http proxy",
27+
"output.lang": "summarizing language uses English by default",
28+
"openai.base_url": "what API base url to use.",
29+
"openai.timeout": "http timeout",
30+
"openai.max_tokens": "the maximum number of tokens to generate in the chat completion.",
31+
"openai.temperature": "What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.",
32+
"openai.provider": "service provider, only support 'openai' or 'azure'",
33+
"openai.model_name": "model deployment name for Azure cognitive service",
34+
"openai.skip_verify": "skip verify TLS certificate",
35+
"openai.headers": "custom headers for openai request",
36+
"openai.api_version": "openai api version",
37+
"openai.top_p": "An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.",
38+
"openai.frequency_penalty": "Number between 0.0 and 1.0 that penalizes new tokens based on their existing frequency in the text so far. Decreases the model's likelihood to repeat the same line verbatim.",
39+
"openai.presence_penalty": "Number between 0.0 and 1.0 that penalizes new tokens based on whether they appear in the text so far. Increases the model's likelihood to talk about new topics.",
40+
}
41+
42+
// configListCmd represents the config list command
43+
var configListCmd = &cobra.Command{
44+
Use: "list",
45+
Short: "show the config list",
46+
Run: func(cmd *cobra.Command, args []string) {
47+
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
48+
columnFmt := color.New(color.FgYellow).SprintfFunc()
49+
50+
// Create a new table with the header "Key" and "Value"
51+
tbl := table.New("Key", "Value")
52+
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
53+
54+
// Sort the keys
55+
keys := make([]string, 0, len(availableKeys))
56+
for key := range availableKeys {
57+
keys = append(keys, key)
58+
}
59+
60+
sort.Strings(keys)
61+
62+
// Add the key and value to the table
63+
for _, v := range keys {
64+
tbl.AddRow(v, viper.Get(v))
65+
}
66+
67+
// Print the table
68+
tbl.Print()
69+
},
70+
}

cmd/config_set.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"strings"
6+
7+
"github.com/fatih/color"
8+
"github.com/spf13/cobra"
9+
"github.com/spf13/viper"
10+
)
11+
12+
func init() {
13+
configCmd.AddCommand(configSetCmd)
14+
}
15+
16+
// configSetCmd represents the config set command
17+
var configSetCmd = &cobra.Command{
18+
Use: "set",
19+
Short: "update the config value",
20+
Args: cobra.MinimumNArgs(2),
21+
RunE: func(cmd *cobra.Command, args []string) error {
22+
// Check if key is available
23+
if _, ok := availableKeys[args[0]]; !ok {
24+
return errors.New("config key is not available, please use `codegpt config list` to see the available keys")
25+
}
26+
27+
// Set config value in viper
28+
if args[0] == "git.exclude_list" {
29+
viper.Set(args[0], strings.Split(args[1], ","))
30+
}
31+
32+
// Write config to file
33+
if err := viper.WriteConfig(); err != nil {
34+
return err
35+
}
36+
37+
// Print success message with config file location
38+
color.Green("you can see the config file: %s", viper.ConfigFileUsed())
39+
return nil
40+
},
41+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/appleboy/com v0.1.7
77
github.com/fatih/color v1.15.0
88
github.com/joho/godotenv v1.5.1
9+
github.com/rodaine/table v1.1.0
910
github.com/sashabaranov/go-openai v1.16.0
1011
github.com/spf13/cobra v1.7.0
1112
github.com/spf13/viper v1.17.0

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
103103
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
104104
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
105105
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
106+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
106107
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
107108
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
108109
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
@@ -147,6 +148,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
147148
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
148149
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
149150
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
151+
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
152+
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
150153
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
151154
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
152155
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
@@ -156,6 +159,8 @@ github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qR
156159
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
157160
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
158161
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
162+
github.com/rodaine/table v1.1.0 h1:/fUlCSdjamMY8VifdQRIu3VWZXYLY7QHFkVorS8NTr4=
163+
github.com/rodaine/table v1.1.0/go.mod h1:Qu3q5wi1jTQD6B6HsP6szie/S4w1QUQ8pq22pz9iL8g=
159164
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
160165
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
161166
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -182,6 +187,7 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
182187
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
183188
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
184189
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
190+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
185191
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
186192
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
187193
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=

0 commit comments

Comments
 (0)