-
Notifications
You must be signed in to change notification settings - Fork 0
configコマンドの追加 #3 #10 #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eadd802
doc: コマンドリストのドキュメントを追加 #10
mochi-yu 20400ac
fix: 設定項目をTokeではなくApiKeyに変更
mochi-yu a62741b
feat: `config.yaml`ファイルが無ければ新規で作成する処理を追加
mochi-yu 6f5d1eb
fix: 設定周りの不要な項目を削除
mochi-yu 681ba99
feat: config項目の追加
mochi-yu 91190ba
feat: `config`コマンドを実装
mochi-yu 5c02115
fix: settingコマンドをrevert
mochi-yu b835d8b
Merge branch 'main' into feature/3-setting-language
mochi-yu 354b9e8
fix: 設定ファイルがあるかどうかの確認を、起動時に行うように変更
mochi-yu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/charmbracelet/bubbles/textinput" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/cocoide/commitify/util" | ||
"github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
configKey = [...]string{"api-key", "language", "format"} | ||
configOption = [][]string{ | ||
{}, | ||
{"Japanese", "English"}, | ||
{"Format 1", "Format 2"}, | ||
} | ||
) | ||
|
||
type configModel struct { | ||
configKeyIndex int | ||
configOptionIndex int | ||
configKeySelected bool | ||
err error | ||
textInput textinput.Model | ||
} | ||
|
||
func initConfigModel() configModel { | ||
ti := textinput.New() | ||
ti.Focus() | ||
|
||
return configModel{ | ||
textInput: ti, | ||
err: nil, | ||
} | ||
} | ||
|
||
func (cm configModel) Init() tea.Cmd { | ||
return textinput.Blink | ||
} | ||
|
||
func (cm configModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
switch cm.configKeySelected { | ||
// 設定項目を選択する | ||
case false: | ||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.Type { | ||
case tea.KeyUp: | ||
if cm.configKeyIndex > 0 { | ||
cm.configKeyIndex-- | ||
} | ||
case tea.KeyDown: | ||
if cm.configKeyIndex < len(configKey)-1 { | ||
cm.configKeyIndex++ | ||
} | ||
case tea.KeyEnter: | ||
cm.configKeySelected = true | ||
return cm, nil | ||
case tea.KeyCtrlC, tea.KeyEsc: | ||
return cm, tea.Quit | ||
} | ||
} | ||
|
||
// 設定項目に値をセットする | ||
case true: | ||
switch len(configOption[cm.configKeyIndex]) { | ||
// 選択肢のない項目は入力を受け付ける | ||
case 0: | ||
var cmd tea.Cmd | ||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.Type { | ||
case tea.KeyEnter: | ||
saveConfig(cm) | ||
return cm, tea.Quit | ||
case tea.KeyCtrlC, tea.KeyEsc: | ||
return cm, tea.Quit | ||
} | ||
case error: | ||
cm.err = msg | ||
return cm, nil | ||
} | ||
|
||
cm.textInput, cmd = cm.textInput.Update(msg) | ||
return cm, cmd | ||
|
||
// 選択肢がある場合はセレクターで表示する | ||
default: | ||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.Type { | ||
case tea.KeyUp: | ||
if cm.configOptionIndex > 0 { | ||
cm.configOptionIndex-- | ||
} | ||
case tea.KeyDown: | ||
if cm.configOptionIndex < len(configOption[cm.configKeyIndex])-1 { | ||
cm.configOptionIndex++ | ||
} | ||
case tea.KeyEnter: | ||
saveConfig(cm) | ||
return cm, tea.Quit | ||
case tea.KeyCtrlC, tea.KeyEsc: | ||
return cm, tea.Quit | ||
} | ||
} | ||
} | ||
} | ||
|
||
return cm, nil | ||
} | ||
|
||
func (cm configModel) View() string { | ||
var b strings.Builder | ||
|
||
switch cm.configKeySelected { | ||
// 設定項目を選んでいない時 | ||
case false: | ||
white := color.New(color.FgWhite).SprintFunc() | ||
b.WriteString(white("設定項目を選んでください:\n")) | ||
b.WriteString(white(" ↑↓の矢印キーで項目を移動、Enterで選択\n")) | ||
|
||
for i, choice := range configKey { | ||
cyan := color.New(color.FgCyan).SprintFunc() | ||
hiCyan := color.New(color.FgHiCyan).SprintFunc() | ||
if i == cm.configKeyIndex { | ||
b.WriteString(fmt.Sprintf(hiCyan("➡️ %s\n"), choice)) | ||
} else { | ||
b.WriteString(fmt.Sprintf(cyan(" %s\n"), choice)) | ||
} | ||
} | ||
|
||
// 設定項目に値をセットする | ||
case true: | ||
// 選択肢のない項目はテキストエリアを表示 | ||
switch len(configOption[cm.configKeyIndex]) { | ||
case 0: | ||
white := color.New(color.FgWhite).SprintFunc() | ||
b.WriteString(white(fmt.Sprintf( | ||
"ここに%sを入力: %s\n", | ||
configKey[cm.configKeyIndex], | ||
cm.textInput.View(), | ||
))) | ||
b.WriteString(white(" Enterキーで確定")) | ||
|
||
default: | ||
white := color.New(color.FgWhite).SprintFunc() | ||
b.WriteString(white("設定内容を選んでください:\n")) | ||
b.WriteString(white(" ↑↓の矢印キーで項目を移動、Enterで選択\n")) | ||
|
||
for i, option := range configOption[cm.configKeyIndex] { | ||
cyan := color.New(color.FgCyan).SprintFunc() | ||
hiCyan := color.New(color.FgHiCyan).SprintFunc() | ||
if i == cm.configOptionIndex { | ||
b.WriteString(fmt.Sprintf(hiCyan("➡️ %s\n"), option)) | ||
} else { | ||
b.WriteString(fmt.Sprintf(cyan(" %s\n"), option)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return b.String() | ||
} | ||
|
||
var configCmd = &cobra.Command{ | ||
Use: "config", | ||
Short: "設定を変更します", | ||
Long: `設定を変更します。設定項目はコマンドを実行すると表示されます。`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
p := tea.NewProgram(initConfigModel()) | ||
p.Run() | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(configCmd) | ||
} | ||
|
||
func saveConfig(cm configModel) { | ||
currentConfig, err := util.ReadConfig() | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
switch cm.configKeyIndex { | ||
case 0: | ||
currentConfig.ChatGptApiKey = cm.textInput.Value() | ||
case 1: | ||
currentConfig.UseLanguage = configOption[cm.configKeyIndex][cm.configOptionIndex] | ||
case 2: | ||
currentConfig.CommitFormat = configOption[cm.configKeyIndex][cm.configOptionIndex] | ||
} | ||
|
||
err = util.WriteConfig(currentConfig) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
## コマンドリスト | ||
|
||
### コマンド | ||
- `suggest`:コミットメッセージの生成 | ||
- `config`:各種の設定 | ||
- 選択肢を出して設定項目を選んでもらう | ||
- 設定項目はAPIキー、日本語/英語、など | ||
|
||
### フラッグ | ||
- `--help` `-h`:CLIのコマンドとオプション一覧を表示 | ||
- `--docs` `-d`:コミットメッセージに関するドキュメント | ||
- `--version` `-v`:アプリのバージョン表示 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package entity | ||
|
||
type Config struct { | ||
ChatGptToken string `json:"chatGptToken"` | ||
ChatGptApiKey string `json:"chatGptApiKey"` | ||
UseLanguage string `json:"UseLanguage"` | ||
CommitFormat string `json:"CommitFormat"` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
他のpackageでも参照する予定だから、enumパッケージ等作成して(objectでもいいかな)欲しいかも!
例
type Format int
const(
PrefixFormat Format = itoa
EmojiFormat
)
→
これを
enum.PrefixFormatみたいに使う感じ
あとentityにも結びつけられたらいいかな