Skip to content

Commit 2e5262f

Browse files
authored
Merge pull request #58 from cocoide/feature/57-refactor-file-arch
プロジェクトのファイル構成を修正
2 parents 13a94e0 + cf05d15 commit 2e5262f

12 files changed

+183
-145
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-
}

cmd/docs.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ var docsCmd = &cobra.Command{
1717
Short: "Document of commitify",
1818
Run: func(cmd *cobra.Command, args []string) {
1919
b, _ := static.Logo.ReadFile("logo.txt")
20-
fmt.Print(color.CyanString(string(b)) + "\n\n ・Languageは日本語と英語が選択できます\n\n ・CodeFormatはPrefix (例: feat: A)とEmoji (例: 🐛 Bugix), Normal (例: Feat A)が選べます")
20+
fmt.Println(color.CyanString(string(b)))
21+
fmt.Println("\n ・Languageは日本語と英語が選択できます")
22+
fmt.Println(" ・CodeFormatはPrefix (例: feat: A)とEmoji (例: 🐛 Bugix), Normal (例: Feat A)が選べます")
2123
},
2224
}
2325

cmd/suggest.go

+60-64
Original file line numberDiff line numberDiff line change
@@ -10,122 +10,102 @@ import (
1010
"github.com/charmbracelet/bubbles/textinput"
1111
tea "github.com/charmbracelet/bubbletea"
1212
"github.com/charmbracelet/lipgloss"
13-
"github.com/cocoide/commitify/internal/entity"
14-
"github.com/cocoide/commitify/internal/gateway"
15-
"github.com/cocoide/commitify/util"
1613
"github.com/fatih/color"
1714
"github.com/spf13/cobra"
15+
16+
"github.com/cocoide/commitify/internal/service"
1817
)
1918

2019
var (
2120
textStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("252")).Render
2221
spinnerStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("69"))
23-
helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Render
2422
)
2523

26-
type model struct {
24+
type suggestModel struct {
2725
choices []string
2826
currentIdx int
2927
errorMsg string
3028
isLoading bool
3129
isEditing bool
3230
spinner spinner.Model
3331
textInput textinput.Model
32+
scs *service.SuggestCmdService
3433
}
3534

36-
func (m *model) Init() tea.Cmd {
37-
conf, err := entity.ReadConfig()
38-
if err != nil {
39-
log.Fatal("設定情報の取得に失敗: ", err)
40-
}
41-
42-
var gi gateway.GatewayInterface
43-
switch conf.AISource {
44-
case int(entity.WrapServer):
45-
gi = gateway.NewGrpcServeGateway()
46-
default:
47-
gi = gateway.NewGrpcServeGateway()
48-
}
49-
35+
func (sm *suggestModel) Init() tea.Cmd {
5036
go func() {
51-
messages, err := gi.FetchCommitMessages()
37+
messages, err := sm.scs.GenerateCommitMessages()
5238
if err != nil {
5339
log.Fatal("コミットメッセージの生成に失敗: ", err)
5440
os.Exit(-1)
5541
}
56-
m.choices = messages
57-
m.isLoading = false
42+
sm.choices = messages
43+
sm.isLoading = false
5844
}()
5945

6046
return textinput.Blink
6147
}
6248

63-
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
49+
func (sm *suggestModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
6450
var cmd tea.Cmd
65-
m.textInput, cmd = m.textInput.Update(msg)
51+
sm.textInput, cmd = sm.textInput.Update(msg)
6652
switch msg := msg.(type) {
6753
case tea.KeyMsg:
6854
switch msg.Type {
6955
case tea.KeyTab:
70-
m.isEditing = true
71-
m.textInput.Focus()
72-
m.textInput.SetValue(m.choices[m.currentIdx])
73-
m.textInput.CharLimit = 100
74-
m.textInput.Width = 100
75-
return m, cmd
56+
sm.isEditing = true
57+
sm.textInput.Focus()
58+
sm.textInput.SetValue(sm.choices[sm.currentIdx])
59+
sm.textInput.CharLimit = 100
60+
sm.textInput.Width = 100
61+
return sm, cmd
7662
case tea.KeyUp:
77-
if m.currentIdx > 0 {
78-
m.currentIdx--
63+
if sm.currentIdx > 0 {
64+
sm.currentIdx--
7965
}
8066
case tea.KeyDown:
81-
if m.currentIdx < len(m.choices)-1 {
82-
m.currentIdx++
67+
if sm.currentIdx < len(sm.choices)-1 {
68+
sm.currentIdx++
8369
}
8470
case tea.KeyEnter:
85-
if err := util.ExecCommitMessage(m.choices[m.currentIdx]); err != nil {
86-
m.errorMsg = "コミットに失敗: " + err.Error()
87-
return m, tea.Quit
71+
if err := sm.scs.SubmitCommit(sm.choices[sm.currentIdx]); err != nil {
72+
sm.errorMsg = "コミットに失敗: " + err.Error()
73+
return sm, tea.Quit
8874
}
89-
return m, tea.Quit
75+
return sm, tea.Quit
9076
case tea.KeyCtrlC, tea.KeyEsc:
91-
return m, tea.Quit
77+
return sm, tea.Quit
9278
}
9379
case spinner.TickMsg:
9480
var cmd tea.Cmd
95-
m.spinner, cmd = m.spinner.Update(msg)
96-
return m, cmd
81+
sm.spinner, cmd = sm.spinner.Update(msg)
82+
return sm, cmd
9783
}
98-
return m, m.spinner.Tick
84+
return sm, sm.spinner.Tick
9985
}
10086

101-
func (m *model) resetSpinner() {
102-
m.spinner = spinner.New()
103-
m.spinner.Style = spinnerStyle
104-
m.spinner.Spinner = spinner.Globe
105-
}
106-
107-
func (m *model) View() string {
108-
if m.errorMsg != "" {
109-
return color.RedString(m.errorMsg)
87+
func (sm *suggestModel) View() string {
88+
if sm.errorMsg != "" {
89+
return color.RedString(sm.errorMsg)
11090
}
111-
if m.isLoading {
112-
s := fmt.Sprintf("\n %s %s\n\n", m.spinner.View(), textStyle("コミットメッセージ生成中"))
91+
if sm.isLoading {
92+
s := fmt.Sprintf("\n %s %s\n\n", sm.spinner.View(), textStyle("コミットメッセージ生成中"))
11393
return s
11494
}
11595
var b strings.Builder
116-
if m.errorMsg != "" {
117-
b.WriteString(color.RedString(m.errorMsg) + "\n\n")
96+
if sm.errorMsg != "" {
97+
b.WriteString(color.RedString(sm.errorMsg) + "\n\n")
11898
}
119-
if m.isEditing {
120-
return m.textInput.View()
99+
if sm.isEditing {
100+
return sm.textInput.View()
121101
}
122102

123103
b.WriteString(color.WhiteString("🍕 Please select and enter to commit"))
124104
b.WriteString(color.WhiteString("\n Use arrow ↑↓ to navigate and press Enter to select."))
125105
b.WriteString(color.WhiteString("\n ( enter Tab key to edit message )\n\n"))
126106

127-
for i, choice := range m.choices {
128-
if i == m.currentIdx {
107+
for i, choice := range sm.choices {
108+
if i == sm.currentIdx {
129109
b.WriteString(fmt.Sprintf(color.HiCyanString("➡️ %s\n"), choice))
130110
} else {
131111
b.WriteString(fmt.Sprintf(color.CyanString(" %s\n"), choice))
@@ -134,28 +114,44 @@ func (m *model) View() string {
134114
return b.String()
135115
}
136116

137-
func initialModel() model {
117+
// モデルの初期化処理
118+
func NewSuggestModel() *suggestModel {
138119
ti := textinput.New()
139120
ti.Focus()
140121

141-
return model{
122+
// suggestコマンドのサービスの取得
123+
scs, err := service.NewSuggestCmdService()
124+
if err != nil {
125+
log.Fatal(err)
126+
os.Exit(-1)
127+
}
128+
129+
return &suggestModel{
142130
choices: []string{""},
143131
currentIdx: 0,
144132
errorMsg: "",
145133
isLoading: true,
146134
isEditing: false,
147135
textInput: ti,
136+
scs: scs,
148137
}
149138
}
150139

140+
// スピナーの初期化
141+
func (sm *suggestModel) initSpinner() {
142+
sm.spinner = spinner.New()
143+
sm.spinner.Style = spinnerStyle
144+
sm.spinner.Spinner = spinner.Globe
145+
}
146+
151147
var suggestCmd = &cobra.Command{
152148
Use: "suggest",
153149
Short: "Suggestion of commit message for staging repository",
154150
Aliases: []string{"s", "suggest"},
155151
Run: func(cmd *cobra.Command, args []string) {
156-
m := initialModel()
157-
m.resetSpinner()
158-
p := tea.NewProgram(&m)
152+
sm := NewSuggestModel()
153+
sm.initSpinner()
154+
p := tea.NewProgram(sm)
159155
p.Run()
160156
},
161157
}

0 commit comments

Comments
 (0)