Skip to content

Commit 58b57dd

Browse files
committed
fix: configを列挙型に変更
1 parent 7ce15a0 commit 58b57dd

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed

cmd/config.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ import (
1212
)
1313

1414
var (
15-
configKey = [...]string{"api-key", "language", "format"}
16-
configOption = [][]string{
15+
configKey = [...]string{"api-key", "language", "format", "ai-source"}
16+
configOption = [][]int{
17+
{},
18+
{int(entity.JP), int(entity.EN)},
19+
{int(entity.NormalFormat), int(entity.EmojiFormat), int(entity.PrefixFormat)},
20+
{int(entity.WrapServer), int(entity.OpenAiAPI)},
21+
}
22+
configOptionLabel = [][]string{
1723
{},
1824
{"Japanese", "English"},
19-
{"Format 1", "Format 2"},
25+
{"Normal Format", "Emoji Format", "PrefixFormat"},
26+
{"Wrap Server", "OpenAI API"},
2027
}
2128
)
2229

@@ -152,7 +159,7 @@ func (cm configModel) View() string {
152159
b.WriteString(white("設定内容を選んでください:\n"))
153160
b.WriteString(white(" ↑↓の矢印キーで項目を移動、Enterで選択\n"))
154161

155-
for i, option := range configOption[cm.configKeyIndex] {
162+
for i, option := range configOptionLabel[cm.configKeyIndex] {
156163
cyan := color.New(color.FgCyan).SprintFunc()
157164
hiCyan := color.New(color.FgHiCyan).SprintFunc()
158165
if i == cm.configOptionIndex {
@@ -194,6 +201,8 @@ func saveConfig(cm configModel) {
194201
currentConfig.UseLanguage = configOption[cm.configKeyIndex][cm.configOptionIndex]
195202
case 2:
196203
currentConfig.CommitFormat = configOption[cm.configKeyIndex][cm.configOptionIndex]
204+
case 3:
205+
currentConfig.AISource = configOption[cm.configKeyIndex][cm.configOptionIndex]
197206
}
198207

199208
err = entity.WriteConfig(currentConfig)

internal/entity/config.go

+54-8
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,77 @@ import (
44
"encoding/json"
55
"fmt"
66

7+
pb "github.com/cocoide/commitify/pkg/grpc"
78
"github.com/spf13/viper"
89
)
910

11+
type Language int
12+
13+
const (
14+
JP Language = iota
15+
EN
16+
)
17+
18+
type CodeFormat int
19+
20+
const (
21+
EmojiFormat CodeFormat = iota
22+
PrefixFormat
23+
NormalFormat
24+
)
25+
26+
type AISource int
27+
28+
const (
29+
WrapServer AISource = iota
30+
OpenAiAPI
31+
)
32+
1033
type Config struct {
11-
ChatGptApiKey string `json:"chatGptApiKey"`
12-
UseLanguage string `json:"UseLanguage"`
13-
CommitFormat string `json:"CommitFormat"`
34+
ChatGptApiKey string `json:"chatGpt_ApiKey"`
35+
UseLanguage int `json:"Use_Language"`
36+
CommitFormat int `json:"Commit_Format"`
37+
AISource int `json:"AI_Source"`
38+
}
39+
40+
func (c *Config) Config2PbVars() (pb.CodeFormatType, pb.LanguageType) {
41+
var codeFormatType pb.CodeFormatType
42+
switch c.CommitFormat {
43+
case int(EmojiFormat):
44+
codeFormatType = pb.CodeFormatType_EMOJI
45+
case int(PrefixFormat):
46+
codeFormatType = pb.CodeFormatType_PREFIX
47+
default:
48+
codeFormatType = pb.CodeFormatType_NORMAL
49+
}
50+
51+
var languageType pb.LanguageType
52+
switch c.UseLanguage {
53+
case int(JP):
54+
languageType = pb.LanguageType_JAPANESE
55+
default:
56+
languageType = pb.LanguageType_JAPANESE
57+
}
58+
59+
return codeFormatType, languageType
1460
}
1561

16-
func ReadConfig() (*Config, error) {
62+
func ReadConfig() (Config, error) {
1763
var result Config
1864

1965
viper.AddConfigPath(".")
2066
viper.SetConfigName("config")
2167
viper.SetConfigType("yaml")
2268
if err := viper.ReadInConfig(); err != nil {
23-
return &result, fmt.Errorf("error reading config file, %s", err.Error())
69+
return result, fmt.Errorf("error reading config file, %s", err.Error())
2470
}
2571
if err := viper.Unmarshal(&result); err != nil {
26-
return &result, fmt.Errorf("unable to decode into struct, %v", err.Error())
72+
return result, fmt.Errorf("unable to decode into struct, %v", err.Error())
2773
}
28-
return &result, nil
74+
return result, nil
2975
}
3076

31-
func WriteConfig(config *Config) error {
77+
func WriteConfig(config Config) error {
3278
viper.AddConfigPath(".")
3379
viper.SetConfigName("config")
3480
viper.SetConfigType("yaml")

0 commit comments

Comments
 (0)