Skip to content

Commit a833572

Browse files
committed
refactor: 設定の保存処理を、表示機能から分離
1 parent cc99bd9 commit a833572

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

cmd/config.go

+10-32
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66

77
"github.com/charmbracelet/bubbles/textinput"
88
tea "github.com/charmbracelet/bubbletea"
9-
"github.com/cocoide/commitify/internal/entity"
109
"github.com/fatih/color"
1110
"github.com/spf13/cobra"
11+
12+
"github.com/cocoide/commitify/internal/entity"
1213
)
1314

1415
var (
@@ -27,29 +28,29 @@ var (
2728
}
2829
)
2930

30-
type configModel struct {
31+
type configCmdModel struct {
3132
configKeyIndex int
3233
configOptionIndex int
3334
configKeySelected bool
3435
err error
3536
textInput textinput.Model
3637
}
3738

38-
func initConfigModel() configModel {
39+
func initConfigModel() configCmdModel {
3940
ti := textinput.New()
4041
ti.Focus()
4142

42-
return configModel{
43+
return configCmdModel{
4344
textInput: ti,
4445
err: nil,
4546
}
4647
}
4748

48-
func (cm configModel) Init() tea.Cmd {
49+
func (cm configCmdModel) Init() tea.Cmd {
4950
return textinput.Blink
5051
}
5152

52-
func (cm configModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
53+
func (cm configCmdModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5354
switch cm.configKeySelected {
5455
// 設定項目を選択する
5556
case false:
@@ -82,7 +83,7 @@ func (cm configModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
8283
case tea.KeyMsg:
8384
switch msg.Type {
8485
case tea.KeyEnter:
85-
saveConfig(cm)
86+
entity.SaveConfig(cm.configKeyIndex, -1, cm.textInput.Value())
8687
return cm, tea.Quit
8788
case tea.KeyCtrlC, tea.KeyEsc:
8889
return cm, tea.Quit
@@ -109,7 +110,7 @@ func (cm configModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
109110
cm.configOptionIndex++
110111
}
111112
case tea.KeyEnter:
112-
saveConfig(cm)
113+
entity.SaveConfig(cm.configKeyIndex, configOption[cm.configKeyIndex][cm.configOptionIndex], "")
113114
return cm, tea.Quit
114115
case tea.KeyCtrlC, tea.KeyEsc:
115116
return cm, tea.Quit
@@ -121,7 +122,7 @@ func (cm configModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
121122
return cm, nil
122123
}
123124

124-
func (cm configModel) View() string {
125+
func (cm configCmdModel) View() string {
125126
var b strings.Builder
126127

127128
switch cm.configKeySelected {
@@ -180,26 +181,3 @@ var configCmd = &cobra.Command{
180181
func init() {
181182
rootCmd.AddCommand(configCmd)
182183
}
183-
184-
func saveConfig(cm configModel) {
185-
currentConfig, err := entity.ReadConfig()
186-
if err != nil {
187-
fmt.Println(err)
188-
}
189-
190-
switch cm.configKeyIndex {
191-
case 0:
192-
currentConfig.ChatGptApiKey = cm.textInput.Value()
193-
case 1:
194-
currentConfig.UseLanguage = configOption[cm.configKeyIndex][cm.configOptionIndex]
195-
case 2:
196-
currentConfig.CommitFormat = configOption[cm.configKeyIndex][cm.configOptionIndex]
197-
case 3:
198-
currentConfig.AISource = configOption[cm.configKeyIndex][cm.configOptionIndex]
199-
}
200-
201-
err = entity.WriteConfig(currentConfig)
202-
if err != nil {
203-
fmt.Println(err)
204-
}
205-
}

internal/entity/config.go

+25
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,28 @@ func WriteConfig(config Config) error {
9595
}
9696
return nil
9797
}
98+
99+
func SaveConfig(configIndex, updateConfigParamInt int, updateConfigParamStr string) error {
100+
currentConfig, err := ReadConfig()
101+
if err != nil {
102+
return err
103+
}
104+
105+
switch configIndex {
106+
case 0:
107+
currentConfig.ChatGptApiKey = updateConfigParamStr
108+
case 1:
109+
currentConfig.UseLanguage = updateConfigParamInt
110+
case 2:
111+
currentConfig.CommitFormat = updateConfigParamInt
112+
case 3:
113+
currentConfig.AISource = updateConfigParamInt
114+
}
115+
116+
err = WriteConfig(currentConfig)
117+
if err != nil {
118+
return err
119+
}
120+
121+
return nil
122+
}

0 commit comments

Comments
 (0)