Skip to content

Commit 8fbdfa0

Browse files
committed
Merge branch 'main' into feature/loading
2 parents 2eec2c8 + 53cd10d commit 8fbdfa0

File tree

21 files changed

+894
-104
lines changed

21 files changed

+894
-104
lines changed

.github/workflows/release.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ jobs:
1515
with:
1616
fetch-depth: 0 # Changelog を正しく動作させるために必要
1717

18+
- name: Install GoReleaser and mockgen
19+
run: |
20+
wget https://github.com/goreleaser/goreleaser/releases/latest/download/goreleaser_amd64 -O /tmp/goreleaser
21+
chmod +x /tmp/goreleaser
22+
sudo mv /tmp/goreleaser /usr/local/bin/goreleaser
23+
go get github.com/golang/mock/mockgen
24+
env:
25+
GO111MODULE: on
1826
# Go をセットアップ
1927
- uses: actions/setup-go@v3
2028
with:

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.PHONY: test
2+
3+
test:
4+
go generate ./...
5+
go test -v ./internal/service

cmd/config.go

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/charmbracelet/bubbles/textinput"
8+
tea "github.com/charmbracelet/bubbletea"
9+
"github.com/cocoide/commitify/util"
10+
"github.com/fatih/color"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
var (
15+
configKey = [...]string{"api-key", "language", "format"}
16+
configOption = [][]string{
17+
{},
18+
{"Japanese", "English"},
19+
{"Format 1", "Format 2"},
20+
}
21+
)
22+
23+
type configModel struct {
24+
configKeyIndex int
25+
configOptionIndex int
26+
configKeySelected bool
27+
err error
28+
textInput textinput.Model
29+
}
30+
31+
func initConfigModel() configModel {
32+
ti := textinput.New()
33+
ti.Focus()
34+
35+
return configModel{
36+
textInput: ti,
37+
err: nil,
38+
}
39+
}
40+
41+
func (cm configModel) Init() tea.Cmd {
42+
return textinput.Blink
43+
}
44+
45+
func (cm configModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
46+
switch cm.configKeySelected {
47+
// 設定項目を選択する
48+
case false:
49+
switch msg := msg.(type) {
50+
case tea.KeyMsg:
51+
switch msg.Type {
52+
case tea.KeyUp:
53+
if cm.configKeyIndex > 0 {
54+
cm.configKeyIndex--
55+
}
56+
case tea.KeyDown:
57+
if cm.configKeyIndex < len(configKey)-1 {
58+
cm.configKeyIndex++
59+
}
60+
case tea.KeyEnter:
61+
cm.configKeySelected = true
62+
return cm, nil
63+
case tea.KeyCtrlC, tea.KeyEsc:
64+
return cm, tea.Quit
65+
}
66+
}
67+
68+
// 設定項目に値をセットする
69+
case true:
70+
switch len(configOption[cm.configKeyIndex]) {
71+
// 選択肢のない項目は入力を受け付ける
72+
case 0:
73+
var cmd tea.Cmd
74+
switch msg := msg.(type) {
75+
case tea.KeyMsg:
76+
switch msg.Type {
77+
case tea.KeyEnter:
78+
saveConfig(cm)
79+
return cm, tea.Quit
80+
case tea.KeyCtrlC, tea.KeyEsc:
81+
return cm, tea.Quit
82+
}
83+
case error:
84+
cm.err = msg
85+
return cm, nil
86+
}
87+
88+
cm.textInput, cmd = cm.textInput.Update(msg)
89+
return cm, cmd
90+
91+
// 選択肢がある場合はセレクターで表示する
92+
default:
93+
switch msg := msg.(type) {
94+
case tea.KeyMsg:
95+
switch msg.Type {
96+
case tea.KeyUp:
97+
if cm.configOptionIndex > 0 {
98+
cm.configOptionIndex--
99+
}
100+
case tea.KeyDown:
101+
if cm.configOptionIndex < len(configOption[cm.configKeyIndex])-1 {
102+
cm.configOptionIndex++
103+
}
104+
case tea.KeyEnter:
105+
saveConfig(cm)
106+
return cm, tea.Quit
107+
case tea.KeyCtrlC, tea.KeyEsc:
108+
return cm, tea.Quit
109+
}
110+
}
111+
}
112+
}
113+
114+
return cm, nil
115+
}
116+
117+
func (cm configModel) View() string {
118+
var b strings.Builder
119+
120+
switch cm.configKeySelected {
121+
// 設定項目を選んでいない時
122+
case false:
123+
b.WriteString(color.WhiteString("設定項目を選んでください:\n"))
124+
b.WriteString(color.WhiteString(" ↑↓の矢印キーで項目を移動、Enterで選択\n"))
125+
126+
for i, choice := range configKey {
127+
if i == cm.configKeyIndex {
128+
b.WriteString(fmt.Sprintf(color.HiCyanString("➡️ %s\n"), choice))
129+
} else {
130+
b.WriteString(fmt.Sprintf(color.CyanString(" %s\n"), choice))
131+
}
132+
}
133+
134+
// 設定項目に値をセットする
135+
case true:
136+
// 選択肢のない項目はテキストエリアを表示
137+
switch len(configOption[cm.configKeyIndex]) {
138+
case 0:
139+
b.WriteString(color.WhiteString(fmt.Sprintf(
140+
"ここに%sを入力: %s\n",
141+
configKey[cm.configKeyIndex],
142+
cm.textInput.View(),
143+
)))
144+
b.WriteString(color.WhiteString(" Enterキーで確定"))
145+
146+
default:
147+
b.WriteString(color.WhiteString("設定内容を選んでください:\n"))
148+
b.WriteString(color.WhiteString(" ↑↓の矢印キーで項目を移動、Enterで選択\n"))
149+
150+
for i, option := range configOption[cm.configKeyIndex] {
151+
if i == cm.configOptionIndex {
152+
b.WriteString(fmt.Sprintf(color.HiCyanString("➡️ %s\n"), option))
153+
} else {
154+
b.WriteString(fmt.Sprintf(color.CyanString(" %s\n"), option))
155+
}
156+
}
157+
}
158+
}
159+
160+
return b.String()
161+
}
162+
163+
var configCmd = &cobra.Command{
164+
Use: "config",
165+
Short: "設定を変更します",
166+
Long: `設定を変更します。設定項目はコマンドを実行すると表示されます。`,
167+
Run: func(cmd *cobra.Command, args []string) {
168+
p := tea.NewProgram(initConfigModel())
169+
p.Run()
170+
},
171+
}
172+
173+
func init() {
174+
rootCmd.AddCommand(configCmd)
175+
}
176+
177+
func saveConfig(cm configModel) {
178+
currentConfig, err := util.ReadConfig()
179+
if err != nil {
180+
fmt.Println(err)
181+
}
182+
183+
switch cm.configKeyIndex {
184+
case 0:
185+
currentConfig.ChatGptApiKey = cm.textInput.Value()
186+
case 1:
187+
currentConfig.UseLanguage = configOption[cm.configKeyIndex][cm.configOptionIndex]
188+
case 2:
189+
currentConfig.CommitFormat = configOption[cm.configKeyIndex][cm.configOptionIndex]
190+
}
191+
192+
err = util.WriteConfig(currentConfig)
193+
if err != nil {
194+
fmt.Println(err)
195+
}
196+
}

cmd/docs.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package cmd
5+
6+
import (
7+
"github.com/cocoide/commitify/static"
8+
"github.com/fatih/color"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
// docsCmd represents the docs command
13+
var docsCmd = &cobra.Command{
14+
Use: "docs",
15+
Short: "Document of commitify",
16+
Run: func(cmd *cobra.Command, args []string) {
17+
b, _ := static.Logo.ReadFile("logo.txt")
18+
color.Cyan(string(b))
19+
},
20+
}
21+
22+
func init() {
23+
rootCmd.AddCommand(docsCmd)
24+
}

cmd/settings.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

cmd/suggest.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ type generateMessages struct {
3131

3232
func (m model) Init() tea.Cmd {
3333
return func() tea.Msg {
34-
util.LoadEnv()
3534
ctx := context.Background()
3635
og := gateway.NewOpenAIGateway(ctx)
3736
ms := service.NewMessageService(og)
38-
messages, err := ms.GenerateCommitMessage()
37+
stagingCode := util.ExecGetStagingCode()
38+
messages, err := ms.GenerateCommitMessage(stagingCode)
3939
if err != nil {
4040
return generateMessages{errorMsg: "メッセージの生成に失敗: " + err.Error()}
4141
}
@@ -89,29 +89,24 @@ func (m *model) resetSpinner() {
8989

9090
func (m model) View() string {
9191
if m.errorMsg != "" {
92-
red := color.New(color.FgRed).SprintFunc()
93-
return fmt.Sprintf(red(m.errorMsg))
92+
return color.RedString(m.errorMsg)
9493
}
9594
if m.isLoading {
9695
s := fmt.Sprintf("\n %s %s\n\n", m.spinner.View(), textStyle("Generating commit messages..."))
9796
return s
9897
}
9998
var b strings.Builder
10099
if m.errorMsg != "" {
101-
red := color.New(color.FgRed).SprintFunc()
102-
b.WriteString(red(m.errorMsg) + "\n\n")
100+
b.WriteString(color.RedString(m.errorMsg) + "\n\n")
103101
}
104-
white := color.New(color.FgWhite).SprintFunc()
105-
b.WriteString(white("🍕Please select an option:"))
106-
b.WriteString(white("\n Use arrow ↑↓ to navigate and press Enter to select.\n\n"))
102+
b.WriteString(color.WhiteString("🍕Please select an option:"))
103+
b.WriteString(color.WhiteString("\n Use arrow ↑↓ to navigate and press Enter to select.\n\n"))
107104

108105
for i, choice := range m.choices {
109-
cyan := color.New(color.FgCyan).SprintFunc()
110-
hiCyan := color.New(color.FgHiCyan).SprintFunc()
111106
if i == m.currentIdx {
112-
b.WriteString(fmt.Sprintf(hiCyan("➡️ %s\n"), choice))
107+
b.WriteString(fmt.Sprintf(color.HiCyanString("➡️ %s\n"), choice))
113108
} else {
114-
b.WriteString(fmt.Sprintf(cyan(" %s\n"), choice))
109+
b.WriteString(fmt.Sprintf(color.CyanString(" %s\n"), choice))
115110
}
116111
}
117112
return b.String()

doc/command_list.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## コマンドリスト
2+
3+
### コマンド
4+
- `suggest`:コミットメッセージの生成
5+
- `config`:各種の設定
6+
- 選択肢を出して設定項目を選んでもらう
7+
- 設定項目はAPIキー、日本語/英語、など
8+
9+
### フラッグ
10+
- `--help` `-h`:CLIのコマンドとオプション一覧を表示
11+
- `--docs` `-d`:コミットメッセージに関するドキュメント
12+
- `--version` `-v`:アプリのバージョン表示

go.mod

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ require (
77
github.com/charmbracelet/bubbletea v0.24.2
88
github.com/charmbracelet/lipgloss v0.7.1
99
github.com/fatih/color v1.15.0
10-
github.com/joho/godotenv v1.5.1
10+
github.com/golang/mock v1.4.4
1111
github.com/sashabaranov/go-openai v1.15.1
1212
github.com/spf13/cobra v1.7.0
1313
github.com/spf13/viper v1.16.0
14+
google.golang.org/grpc v1.55.0
15+
google.golang.org/protobuf v1.30.0
1416
)
1517

1618
require (
@@ -31,8 +33,11 @@ require (
3133
)
3234

3335
require (
36+
github.com/atotto/clipboard v0.1.4 // indirect
3437
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
38+
github.com/charmbracelet/lipgloss v0.7.1 // indirect
3539
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
40+
github.com/golang/protobuf v1.5.3 // indirect
3641
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
3742
github.com/mattn/go-isatty v0.0.18 // indirect
3843
github.com/mattn/go-localereader v0.0.1 // indirect
@@ -42,8 +47,10 @@ require (
4247
github.com/muesli/reflow v0.3.0 // indirect
4348
github.com/muesli/termenv v0.15.1 // indirect
4449
github.com/rivo/uniseg v0.2.0 // indirect
50+
golang.org/x/net v0.10.0 // indirect
4551
golang.org/x/sync v0.1.0 // indirect
4652
golang.org/x/sys v0.8.0 // indirect
47-
golang.org/x/term v0.6.0 // indirect
53+
golang.org/x/term v0.8.0 // indirect
4854
golang.org/x/text v0.9.0 // indirect
55+
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
4956
)

0 commit comments

Comments
 (0)