Skip to content

Commit 1a812de

Browse files
authored
Merge pull request #17 from cocoide/feature/config
設定ファイルを読み込む便利関数の作成、それをGPTとの通信に適応
2 parents fe52414 + acca189 commit 1a812de

File tree

5 files changed

+76
-17
lines changed

5 files changed

+76
-17
lines changed

cmd/settings.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,42 @@ package cmd
33
import (
44
"fmt"
55
"log"
6+
7+
"github.com/cocoide/commitify/util"
68
"github.com/spf13/cobra"
7-
"github.com/spf13/viper"
89
)
910

1011
var setAPIKeyCmd = &cobra.Command{
1112
Use: "set-apikey [api_key]",
1213
Short: "API Key settings for ChatGPT",
13-
Args: cobra.ExactArgs(1),
14-
Run: func (cmd *cobra.Command, args [] string) {
15-
apikey := args[0]
16-
viper.Set("chatgpt.api_key", apikey)
17-
if err := viper.WriteConfig(); err != nil {
18-
log.Fatal("An error occurred while writing the configuration file:", err)
14+
Args: cobra.ExactArgs(1),
15+
Run: func(cmd *cobra.Command, args []string) {
16+
config, _ := util.ReadConfig()
17+
config.ChatGptToken = args[0]
18+
if err := util.WriteConfig(config); err != nil {
19+
log.Fatal("Failed to write into config", err)
1920
}
20-
fmt.Println("ChatGPT API key has been set")
21+
fmt.Println("ChatGPT Token has been set")
2122
},
2223
}
2324

2425
var showAPIKeyCmd = &cobra.Command{
25-
Use: "show-apikey",
26+
Use: "show-apikey",
2627
Short: "Display ChatGPT API key",
27-
Run: func (cmd *cobra.Command, args [] string) {
28-
apikey := viper.GetString("chatgpt.api_key")
29-
if apikey == "" {
28+
Run: func(cmd *cobra.Command, args []string) {
29+
config, err := util.ReadConfig()
30+
if err != nil {
31+
log.Fatal("Failed to read config:", err)
32+
}
33+
if config.ChatGptToken == "" {
3034
fmt.Println("API key is not set")
3135
} else {
32-
fmt.Println("ChatGPT APIKey:", apikey)
36+
fmt.Println("ChatGPT APIKey:", config.ChatGptToken)
3337
}
3438
},
3539
}
3640

3741
func init() {
3842
rootCmd.AddCommand(setAPIKeyCmd)
3943
rootCmd.AddCommand(showAPIKeyCmd)
40-
}
44+
}

config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
chatgpttoken:

internal/entity/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package entity
2+
3+
type Config struct {
4+
ChatGptToken string `json:"chatGptToken"`
5+
}

internal/gateway/openai.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package gateway
22

33
import (
44
"context"
5-
"os"
5+
"log"
66

7+
"github.com/cocoide/commitify/util"
78
"github.com/sashabaranov/go-openai"
89
)
910

@@ -18,8 +19,11 @@ type openAIGateway struct {
1819
}
1920

2021
func NewOpenAIGateway(ctx context.Context) OpenAIGateway {
21-
OPENAI_SECRET := os.Getenv("OPENAI_SECRET")
22-
client := openai.NewClient(OPENAI_SECRET)
22+
config, err := util.ReadConfig()
23+
if err != nil {
24+
log.Fatalf("Failed to read config: %v", err)
25+
}
26+
client := openai.NewClient(config.ChatGptToken)
2327
return &openAIGateway{client: client, ctx: ctx}
2428
}
2529

util/config.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package util
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/cocoide/commitify/internal/entity"
8+
"github.com/spf13/viper"
9+
)
10+
11+
func ReadConfig() (*entity.Config, error) {
12+
var result entity.Config
13+
viper.AddConfigPath(".")
14+
viper.SetConfigName("config")
15+
viper.SetConfigType("yaml")
16+
if err := viper.ReadInConfig(); err != nil {
17+
return &result, fmt.Errorf("Error reading config file, %s", err.Error())
18+
}
19+
if err := viper.Unmarshal(&result); err != nil {
20+
return &result, fmt.Errorf("Unable to decode into struct, %v", err.Error())
21+
}
22+
return &result, nil
23+
}
24+
25+
func WriteConfig(config *entity.Config) error {
26+
viper.AddConfigPath(".")
27+
viper.SetConfigName("config")
28+
viper.SetConfigType("yaml")
29+
configMap := make(map[string]interface{})
30+
configBytes, err := json.Marshal(config)
31+
if err != nil {
32+
return fmt.Errorf("Error marshalling config: %s", err.Error())
33+
}
34+
err = json.Unmarshal(configBytes, &configMap)
35+
if err != nil {
36+
return fmt.Errorf("Error unmarshalling config: %s", err.Error())
37+
}
38+
if err := viper.MergeConfigMap(configMap); err != nil {
39+
return err
40+
}
41+
if err := viper.WriteConfig(); err != nil {
42+
return fmt.Errorf("Error saving config file, %s", err.Error())
43+
}
44+
return nil
45+
}

0 commit comments

Comments
 (0)