Skip to content

Commit fe52414

Browse files
authored
Merge pull request #14 from cocoide/feature/message-select
コミットメッセージの複数選択をTUIで可能にする
2 parents 7a795c6 + e0fbf86 commit fe52414

File tree

6 files changed

+221
-22
lines changed

6 files changed

+221
-22
lines changed

cmd/suggest.go

Lines changed: 95 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,114 @@ package cmd
33
import (
44
"context"
55
"fmt"
6-
"log"
7-
"os/exec"
6+
"strings"
87

8+
tea "github.com/charmbracelet/bubbletea"
99
"github.com/cocoide/commitify/internal/gateway"
10+
"github.com/cocoide/commitify/internal/service"
1011
"github.com/cocoide/commitify/util"
12+
"github.com/fatih/color"
1113
"github.com/spf13/cobra"
1214
)
1315

14-
const (
15-
CommitMessagePrompt = "Generate commit message for [%s]"
16-
FormatNotice = ", format your commit as:\n- feat: [feature description]\n- bugfix: [bugfix description]"
17-
)
16+
type model struct {
17+
choices []string
18+
currentIdx int
19+
errorMsg string
20+
isLoading bool
21+
messages []string
22+
}
1823

19-
var suggestCmd = &cobra.Command{
20-
Use: "suggest",
21-
Short: "Suggestion of commit message for staging repository",
22-
Run: func(cmd *cobra.Command, args [] string) {
24+
type generateMessages struct {
25+
messages []string
26+
errorMsg string
27+
}
28+
29+
func (m model) Init() tea.Cmd {
30+
return func() tea.Msg {
2331
util.LoadEnv()
2432
ctx := context.Background()
2533
og := gateway.NewOpenAIGateway(ctx)
26-
result, err := exec.Command("git", "diff", "--staged").Output()
34+
ms := service.NewMessageService(og)
35+
messages, err := ms.GenerateCommitMessage()
2736
if err != nil {
28-
log.Fatal(err.Error())
37+
return generateMessages{errorMsg: "メッセージの生成に失敗: " + err.Error()}
2938
}
30-
// 設定に応じてPromptは動的に変化させる
31-
prompt := fmt.Sprintf(CommitMessagePrompt, string(result))
32-
answer, err := og.GetAnswerFromPrompt(prompt, 0.01)
33-
// 生成中のUIを非同期で表示
34-
if err != nil {
35-
log.Fatal(err.Error())
39+
return generateMessages{messages: messages}
40+
}
41+
}
42+
43+
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
44+
switch msg := msg.(type) {
45+
case generateMessages:
46+
if msg.errorMsg != "" {
47+
m.errorMsg = msg.errorMsg
48+
m.isLoading = false
49+
return m, nil
3650
}
37-
fmt.Println(answer)
51+
m.choices = msg.messages
52+
m.isLoading = false
53+
return m, nil
54+
case tea.KeyMsg:
55+
switch msg.Type {
56+
case tea.KeyUp:
57+
if m.currentIdx > 0 {
58+
m.currentIdx--
59+
}
60+
case tea.KeyDown:
61+
if m.currentIdx < len(m.choices)-1 {
62+
m.currentIdx++
63+
}
64+
case tea.KeyEnter:
65+
if err := util.ExecCommitMessage(m.choices[m.currentIdx]); err != nil {
66+
m.errorMsg = "コミットに失敗: " + err.Error()
67+
return m, tea.Quit
68+
}
69+
return m, tea.Quit
70+
case tea.KeyCtrlC, tea.KeyEsc:
71+
return m, tea.Quit
72+
}
73+
}
74+
return m, nil
75+
}
76+
77+
func (m model) View() string {
78+
if m.errorMsg != "" {
79+
red := color.New(color.FgRed).SprintFunc()
80+
return fmt.Sprintf(red(m.errorMsg))
81+
}
82+
if m.isLoading {
83+
return "🌎 Generating commit messages ..."
84+
}
85+
var b strings.Builder
86+
if m.errorMsg != "" {
87+
red := color.New(color.FgRed).SprintFunc()
88+
b.WriteString(red(m.errorMsg) + "\n\n")
89+
}
90+
white := color.New(color.FgWhite).SprintFunc()
91+
b.WriteString(white("🍕Please select an option:"))
92+
b.WriteString(white("\n Use arrow ↑↓ to navigate and press Enter to select.\n\n"))
93+
94+
for i, choice := range m.choices {
95+
cyan := color.New(color.FgCyan).SprintFunc()
96+
hiCyan := color.New(color.FgHiCyan).SprintFunc()
97+
if i == m.currentIdx {
98+
b.WriteString(fmt.Sprintf(hiCyan("➡️ %s\n"), choice))
99+
} else {
100+
b.WriteString(fmt.Sprintf(cyan(" %s\n"), choice))
101+
}
102+
}
103+
return b.String()
104+
}
105+
106+
var suggestCmd = &cobra.Command{
107+
Use: "suggest",
108+
Short: "Suggestion of commit message for staging repository",
109+
Aliases: []string{"s", "suggest"},
110+
Run: func(cmd *cobra.Command, args []string) {
111+
m := model{isLoading: true}
112+
p := tea.NewProgram(m)
113+
p.Run()
38114
},
39115
}
40116

go.mod

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module github.com/cocoide/commitify
33
go 1.19
44

55
require (
6+
github.com/charmbracelet/bubbletea v0.24.2
7+
github.com/fatih/color v1.15.0
68
github.com/joho/godotenv v1.5.1
79
github.com/sashabaranov/go-openai v1.15.1
810
github.com/spf13/cobra v1.7.0
@@ -14,15 +16,34 @@ require (
1416
github.com/hashicorp/hcl v1.0.0 // indirect
1517
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1618
github.com/magiconair/properties v1.8.7 // indirect
19+
github.com/mattn/go-colorable v0.1.13 // indirect
1720
github.com/mitchellh/mapstructure v1.5.0 // indirect
1821
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
1922
github.com/spf13/afero v1.9.5 // indirect
2023
github.com/spf13/cast v1.5.1 // indirect
2124
github.com/spf13/jwalterweatherman v1.1.0 // indirect
2225
github.com/spf13/pflag v1.0.5 // indirect
2326
github.com/subosito/gotenv v1.4.2 // indirect
24-
golang.org/x/sys v0.8.0 // indirect
25-
golang.org/x/text v0.9.0 // indirect
2627
gopkg.in/ini.v1 v1.67.0 // indirect
2728
gopkg.in/yaml.v3 v3.0.1 // indirect
2829
)
30+
31+
require (
32+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
33+
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
34+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
35+
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
36+
github.com/mattn/go-isatty v0.0.18 // indirect
37+
github.com/mattn/go-localereader v0.0.1 // indirect
38+
github.com/mattn/go-runewidth v0.0.14 // indirect
39+
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
40+
github.com/muesli/cancelreader v0.2.2 // indirect
41+
github.com/muesli/reflow v0.3.0 // indirect
42+
github.com/muesli/termenv v0.15.1 // indirect
43+
github.com/rivo/uniseg v0.2.0 // indirect
44+
github.com/spf13/pflag v1.0.5 // indirect
45+
golang.org/x/sync v0.1.0 // indirect
46+
golang.org/x/sys v0.8.0 // indirect
47+
golang.org/x/term v0.6.0 // indirect
48+
golang.org/x/text v0.9.0 // indirect
49+
)

go.sum

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,20 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
3838
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
3939
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
4040
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
41+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
42+
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
4143
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
44+
github.com/charmbracelet/bubbletea v0.24.2 h1:uaQIKx9Ai6Gdh5zpTbGiWpytMU+CfsPp06RaW2cx/SY=
45+
github.com/charmbracelet/bubbletea v0.24.2/go.mod h1:XdrNrV4J8GiyshTtx3DNuYkR1FDaJmO3l2nejekbsgg=
4246
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
4347
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
4448
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
4549
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
4650
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
4751
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
4852
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
53+
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
54+
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
4955
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
5056
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5157
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -56,6 +62,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
5662
github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po=
5763
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
5864
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
65+
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
66+
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
5967
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
6068
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
6169
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
@@ -136,17 +144,40 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
136144
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
137145
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
138146
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
147+
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
148+
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
139149
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
140150
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
151+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
152+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
153+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
154+
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
155+
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
156+
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
157+
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
158+
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
159+
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
160+
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
141161
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
142162
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
163+
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
164+
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
165+
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
166+
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
167+
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
168+
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
169+
github.com/muesli/termenv v0.15.1 h1:UzuTb/+hhlBugQz28rpzey4ZuKcZ03MeKsoG7IJZIxs=
170+
github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4ANqrZs2sQ=
143171
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
144172
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
145173
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
146174
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
147175
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
148176
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
149177
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
178+
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
179+
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
180+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
150181
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
151182
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
152183
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -278,6 +309,8 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
278309
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
279310
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
280311
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
312+
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
313+
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
281314
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
282315
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
283316
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -313,10 +346,15 @@ golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7w
313346
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
314347
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
315348
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
349+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
316350
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
351+
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
352+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
317353
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
318354
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
319355
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
356+
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
357+
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
320358
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
321359
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
322360
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

internal/service/message.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package service
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/cocoide/commitify/internal/gateway"
8+
"github.com/cocoide/commitify/util"
9+
)
10+
11+
const (
12+
CommitMessagePrompt = "Generate up to 5 commit messages for [%s]. Each message should be separated by only space"
13+
FormatNotice = ", format commit as:\n- feat: [feature description]\n- bugfix: [bugfix description]"
14+
)
15+
16+
// メッセージの生成、加工に関するクラス
17+
type MessageService interface {
18+
GenerateCommitMessage() ([]string, error)
19+
}
20+
21+
type messageService struct {
22+
og gateway.OpenAIGateway
23+
}
24+
25+
func NewMessageService(og gateway.OpenAIGateway) MessageService {
26+
return &messageService{og: og}
27+
}
28+
29+
func (s *messageService) GenerateCommitMessage() ([]string, error) {
30+
var result <-chan string
31+
stagingCode := util.ExecGetStagingCode()
32+
if len(stagingCode) < 1 {
33+
return nil, fmt.Errorf("There is no staging code")
34+
}
35+
prompt := fmt.Sprintf(CommitMessagePrompt, string(stagingCode))
36+
result = s.og.AsyncGetAnswerFromPrompt(prompt, 0.01)
37+
messages := strings.Split(<-result, "\n")
38+
return messages, nil
39+
}

util/exec.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os/exec"
7+
)
8+
9+
func ExecGetStagingCode() string {
10+
code, err := exec.Command("git", "diff", "--staged").Output()
11+
if err != nil {
12+
fmt.Printf("Gitでエラーが発生")
13+
log.Fatal(err.Error())
14+
}
15+
return string(code)
16+
}
17+
18+
func ExecCommitMessage(msg string) error {
19+
cmd := exec.Command("git", "commit", "-m", msg)
20+
if err := cmd.Run(); err != nil {
21+
return err
22+
}
23+
return nil
24+
}

util/loadenv.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66
"github.com/joho/godotenv"
77
)
88

9+
// Viperを導入したらgodotenvのコードは削除する
910
func LoadEnv() {
1011
err := godotenv.Load(".env")
1112
if err != nil {
1213
log.Printf("failed to load .env file: %v" + err.Error())
1314
} else {
14-
log.Print("success to load .env file")
15+
// log.Print("success to load .env file")
1516
}
1617
}

0 commit comments

Comments
 (0)