Skip to content

Commit bfe949e

Browse files
authored
Merge pull request #27 from cocoide/feature/3-setting-language
configコマンドの追加 #3 #10
2 parents c170612 + 354b9e8 commit bfe949e

File tree

11 files changed

+242
-80
lines changed

11 files changed

+242
-80
lines changed

cmd/config.go

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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+
white := color.New(color.FgWhite).SprintFunc()
124+
b.WriteString(white("設定項目を選んでください:\n"))
125+
b.WriteString(white(" ↑↓の矢印キーで項目を移動、Enterで選択\n"))
126+
127+
for i, choice := range configKey {
128+
cyan := color.New(color.FgCyan).SprintFunc()
129+
hiCyan := color.New(color.FgHiCyan).SprintFunc()
130+
if i == cm.configKeyIndex {
131+
b.WriteString(fmt.Sprintf(hiCyan("➡️ %s\n"), choice))
132+
} else {
133+
b.WriteString(fmt.Sprintf(cyan(" %s\n"), choice))
134+
}
135+
}
136+
137+
// 設定項目に値をセットする
138+
case true:
139+
// 選択肢のない項目はテキストエリアを表示
140+
switch len(configOption[cm.configKeyIndex]) {
141+
case 0:
142+
white := color.New(color.FgWhite).SprintFunc()
143+
b.WriteString(white(fmt.Sprintf(
144+
"ここに%sを入力: %s\n",
145+
configKey[cm.configKeyIndex],
146+
cm.textInput.View(),
147+
)))
148+
b.WriteString(white(" Enterキーで確定"))
149+
150+
default:
151+
white := color.New(color.FgWhite).SprintFunc()
152+
b.WriteString(white("設定内容を選んでください:\n"))
153+
b.WriteString(white(" ↑↓の矢印キーで項目を移動、Enterで選択\n"))
154+
155+
for i, option := range configOption[cm.configKeyIndex] {
156+
cyan := color.New(color.FgCyan).SprintFunc()
157+
hiCyan := color.New(color.FgHiCyan).SprintFunc()
158+
if i == cm.configOptionIndex {
159+
b.WriteString(fmt.Sprintf(hiCyan("➡️ %s\n"), option))
160+
} else {
161+
b.WriteString(fmt.Sprintf(cyan(" %s\n"), option))
162+
}
163+
}
164+
}
165+
}
166+
167+
return b.String()
168+
}
169+
170+
var configCmd = &cobra.Command{
171+
Use: "config",
172+
Short: "設定を変更します",
173+
Long: `設定を変更します。設定項目はコマンドを実行すると表示されます。`,
174+
Run: func(cmd *cobra.Command, args []string) {
175+
p := tea.NewProgram(initConfigModel())
176+
p.Run()
177+
},
178+
}
179+
180+
func init() {
181+
rootCmd.AddCommand(configCmd)
182+
}
183+
184+
func saveConfig(cm configModel) {
185+
currentConfig, err := util.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+
}
198+
199+
err = util.WriteConfig(currentConfig)
200+
if err != nil {
201+
fmt.Println(err)
202+
}
203+
}

cmd/settings.go

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

cmd/suggest.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ type generateMessages struct {
2828

2929
func (m model) Init() tea.Cmd {
3030
return func() tea.Msg {
31-
util.LoadEnv()
3231
ctx := context.Background()
3332
og := gateway.NewOpenAIGateway(ctx)
3433
ms := service.NewMessageService(og)

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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ require (
2929
)
3030

3131
require (
32+
github.com/atotto/clipboard v0.1.4 // indirect
3233
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
34+
github.com/charmbracelet/bubbles v0.16.1 // indirect
35+
github.com/charmbracelet/lipgloss v0.7.1 // indirect
3336
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
34-
github.com/inconshreveable/mousetrap v1.1.0 // indirect
3537
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
3638
github.com/mattn/go-isatty v0.0.18 // indirect
3739
github.com/mattn/go-localereader v0.0.1 // indirect
@@ -41,7 +43,6 @@ require (
4143
github.com/muesli/reflow v0.3.0 // indirect
4244
github.com/muesli/termenv v0.15.1 // indirect
4345
github.com/rivo/uniseg v0.2.0 // indirect
44-
github.com/spf13/pflag v1.0.5 // indirect
4546
golang.org/x/sync v0.1.0 // indirect
4647
golang.org/x/sys v0.8.0 // indirect
4748
golang.org/x/term v0.6.0 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,17 @@ 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/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
42+
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
4143
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
4244
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
4345
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
46+
github.com/charmbracelet/bubbles v0.16.1 h1:6uzpAAaT9ZqKssntbvZMlksWHruQLNxg49H5WdeuYSY=
47+
github.com/charmbracelet/bubbles v0.16.1/go.mod h1:2QCp9LFlEsBQMvIYERr7Ww2H2bA7xen1idUDIzm/+Xc=
4448
github.com/charmbracelet/bubbletea v0.24.2 h1:uaQIKx9Ai6Gdh5zpTbGiWpytMU+CfsPp06RaW2cx/SY=
4549
github.com/charmbracelet/bubbletea v0.24.2/go.mod h1:XdrNrV4J8GiyshTtx3DNuYkR1FDaJmO3l2nejekbsgg=
50+
github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=
51+
github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=
4652
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
4753
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
4854
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=

internal/entity/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package entity
22

33
type Config struct {
4-
ChatGptToken string `json:"chatGptToken"`
4+
ChatGptApiKey string `json:"chatGptApiKey"`
5+
UseLanguage string `json:"UseLanguage"`
6+
CommitFormat string `json:"CommitFormat"`
57
}

internal/gateway/openai.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func NewOpenAIGateway(ctx context.Context) OpenAIGateway {
2323
if err != nil {
2424
log.Fatalf("Failed to read config: %v", err)
2525
}
26-
client := openai.NewClient(config.ChatGptToken)
26+
client := openai.NewClient(config.ChatGptApiKey)
2727
return &openAIGateway{client: client, ctx: ctx}
2828
}
2929

main.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
package main
22

33
import (
4-
"log"
4+
"fmt"
5+
"os"
56

67
"github.com/cocoide/commitify/cmd"
7-
"github.com/spf13/viper"
88
)
99

1010
func main() {
11-
viper.SetConfigName("config")
12-
viper.AddConfigPath(".")
13-
viper.SetConfigType("yaml")
14-
viper.AutomaticEnv()
15-
16-
if err := viper.ReadInConfig(); err != nil {
17-
log.Println("An error occurred while reading the configuration file:", err)
11+
// configファイルがあるかどうかを確認
12+
_, err := os.Stat("config.yaml")
13+
if os.IsNotExist(err) {
14+
if _, err := os.Create("config.yaml"); err != nil {
15+
fmt.Printf("error creating config file, %s", err.Error())
16+
}
1817
}
1918

2019
cmd.Execute()

util/config.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import (
1010

1111
func ReadConfig() (*entity.Config, error) {
1212
var result entity.Config
13+
1314
viper.AddConfigPath(".")
1415
viper.SetConfigName("config")
1516
viper.SetConfigType("yaml")
1617
if err := viper.ReadInConfig(); err != nil {
17-
return &result, fmt.Errorf("Error reading config file, %s", err.Error())
18+
return &result, fmt.Errorf("error reading config file, %s", err.Error())
1819
}
1920
if err := viper.Unmarshal(&result); err != nil {
20-
return &result, fmt.Errorf("Unable to decode into struct, %v", err.Error())
21+
return &result, fmt.Errorf("unable to decode into struct, %v", err.Error())
2122
}
2223
return &result, nil
2324
}
@@ -29,17 +30,17 @@ func WriteConfig(config *entity.Config) error {
2930
configMap := make(map[string]interface{})
3031
configBytes, err := json.Marshal(config)
3132
if err != nil {
32-
return fmt.Errorf("Error marshalling config: %s", err.Error())
33+
return fmt.Errorf("error marshalling config: %s", err.Error())
3334
}
3435
err = json.Unmarshal(configBytes, &configMap)
3536
if err != nil {
36-
return fmt.Errorf("Error unmarshalling config: %s", err.Error())
37+
return fmt.Errorf("error unmarshalling config: %s", err.Error())
3738
}
3839
if err := viper.MergeConfigMap(configMap); err != nil {
3940
return err
4041
}
4142
if err := viper.WriteConfig(); err != nil {
42-
return fmt.Errorf("Error saving config file, %s", err.Error())
43+
return fmt.Errorf("error saving config file, %s", err.Error())
4344
}
4445
return nil
4546
}

0 commit comments

Comments
 (0)