Skip to content

Commit 7ed1522

Browse files
authored
feat: Refactor Part4 (#8)
* feat: Refactor Part4 * feat(cmd): add sub commands and flags add sub command load add flags for root command * WIP(commit): add options for commit add options: -a/--all -s/--signoff n/a Signed-off-by: shipengqi <pooky.shipengqi@gmail.com> --------- Signed-off-by: shipengqi <pooky.shipengqi@gmail.com>
1 parent f1dfd4d commit 7ed1522

File tree

11 files changed

+112
-48
lines changed

11 files changed

+112
-48
lines changed

cmd/cz/cz.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ import (
77
"github.com/spf13/cobra"
88

99
"github.com/shipengqi/commitizen/internal/git"
10+
"github.com/shipengqi/commitizen/internal/options"
1011
"github.com/shipengqi/commitizen/internal/render"
1112
)
1213

1314
func New() *cobra.Command {
15+
o := options.New()
1416
c := &cobra.Command{
1517
Use: "commitizen",
1618
Long: `Command line utility to standardize git commit messages.`,
1719
RunE: func(cmd *cobra.Command, args []string) error {
18-
isrepo, err := git.IsGitRepo()
20+
isRepo, err := git.IsGitRepo()
1921
if err != nil {
2022
return err
2123
}
22-
if !isrepo {
24+
if !isRepo {
2325
return errors.New("not a git repository")
2426
}
2527
tmpl, err := render.Load([]byte(render.DefaultCommitTemplate))
@@ -30,20 +32,28 @@ func New() *cobra.Command {
3032
if err != nil {
3133
return err
3234
}
33-
fmt.Println(string(msg))
34-
// output, err := git.Commit(msg)
35-
// if err != nil {
36-
// return err
37-
// }
38-
// fmt.Println(output)
35+
36+
output, err := git.Commit(msg, o.GitOptions)
37+
if err != nil {
38+
return err
39+
}
40+
fmt.Println(output)
3941
return nil
4042
},
4143
}
4244

4345
c.SilenceUsage = true
4446
c.SilenceErrors = true
47+
cobra.EnableCommandSorting = false
48+
c.CompletionOptions.DisableDefaultCmd = true
49+
50+
f := c.Flags()
51+
f.SortFlags = false
52+
c.DisableFlagsInUseLine = true
53+
o.AddFlags(f)
4554

4655
c.AddCommand(NewInitCmd())
56+
c.AddCommand(NewLoadCmd())
4757

4858
return c
4959
}

cmd/cz/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func NewInitCmd() *cobra.Command {
1414
c := &cobra.Command{
1515
Use: "init",
16-
Short: "Initialize the 'git cz' and templates.",
16+
Short: "Initialize this tool to git-core as git-cz.",
1717
RunE: func(cmd *cobra.Command, args []string) error {
1818
src, err := exec.LookPath(os.Args[0])
1919
if err != nil {

cmd/cz/load.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
11
package cz
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
func NewLoadCmd() *cobra.Command {
8+
c := &cobra.Command{
9+
Use: "load",
10+
Short: "Load templates.",
11+
RunE: func(cmd *cobra.Command, args []string) error {
12+
return nil
13+
},
14+
}
15+
16+
return c
17+
}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ require (
66
github.com/charmbracelet/bubbles v0.17.1
77
github.com/charmbracelet/bubbletea v0.25.0
88
github.com/charmbracelet/lipgloss v0.9.1
9-
github.com/shipengqi/golib v0.2.8
9+
github.com/shipengqi/golib v0.2.10
1010
github.com/spf13/cobra v1.8.0
11+
github.com/spf13/pflag v1.0.5
1112
gopkg.in/yaml.v3 v3.0.1
1213
)
1314

@@ -27,9 +28,8 @@ require (
2728
github.com/muesli/termenv v0.15.2 // indirect
2829
github.com/rivo/uniseg v0.2.0 // indirect
2930
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect
30-
github.com/spf13/pflag v1.0.5 // indirect
3131
golang.org/x/sync v0.1.0 // indirect
32-
golang.org/x/sys v0.16.0 // indirect
32+
golang.org/x/sys v0.17.0 // indirect
3333
golang.org/x/term v0.6.0 // indirect
3434
golang.org/x/text v0.4.0 // indirect
3535
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
4747
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
4848
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=
4949
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
50-
github.com/shipengqi/golib v0.2.8 h1:uBbE5dk4pJUE+K7GdCKLQGiJ7QqmLeLkrrEE8WYgKoI=
51-
github.com/shipengqi/golib v0.2.8/go.mod h1:1i6lohzfvyibjgvcqdtGV6ut14QGltI2gn+R42XDdg4=
50+
github.com/shipengqi/golib v0.2.10 h1:obnq1jdYYRsA9ja7yUPmH0jqx/3Q0H1gNARSpn/Va2E=
51+
github.com/shipengqi/golib v0.2.10/go.mod h1:NZSkI7eecrMMNvVyv0FPvUID8BF9DxMP3V3WZREnXqc=
5252
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
5353
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
5454
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
@@ -59,8 +59,8 @@ golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
5959
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
6060
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6161
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
62-
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
63-
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
62+
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
63+
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
6464
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
6565
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
6666
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=

internal/git/git.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ func IsGitRepo() (bool, error) {
3535
return true, nil
3636
}
3737

38+
// WorkingTreeRoot return path of the top-level directory of the working tree
39+
func WorkingTreeRoot() (path string, err error) {
40+
output, err := cliutil.ExecContext(context.TODO(), "git", "rev-parse", "--show-toplevel")
41+
if err != nil {
42+
return "", err
43+
}
44+
return strings.TrimSpace(string(output)), nil
45+
}
46+
3847
func ExecPath() (string, error) {
3948
stdout, err := cliutil.ExecContext(context.TODO(), "git", "--exec-path")
4049
if err != nil {
@@ -43,7 +52,7 @@ func ExecPath() (string, error) {
4352
return strings.TrimSpace(stdout), nil
4453
}
4554

46-
func Commit(msg []byte) (string, error) {
55+
func Commit(msg []byte, opts *Options) (string, error) {
4756
temp, err := os.CreateTemp("", "COMMIT_MESSAGE_")
4857
if err != nil {
4958
return "", err
@@ -52,17 +61,21 @@ func Commit(msg []byte) (string, error) {
5261
if _, err = temp.Write(msg); err != nil {
5362
return "", err
5463
}
55-
stdout, err := cliutil.ExecContext(context.TODO(), "git", "commit", "-F", temp.Name())
64+
65+
tokens := []string{
66+
"commit",
67+
"-F",
68+
temp.Name(),
69+
}
70+
if opts.Add {
71+
tokens = append(tokens, "-a")
72+
}
73+
if opts.SignOff {
74+
tokens = append(tokens, "-s")
75+
}
76+
stdout, err := cliutil.ExecContext(context.TODO(), "git", tokens...)
5677
if err != nil {
5778
return "", err
5879
}
5980
return strings.TrimSpace(stdout), nil
6081
}
61-
62-
func Add() error {
63-
return nil
64-
}
65-
66-
func Push() error {
67-
return nil
68-
}

internal/render/errors.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package render
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
var (
9+
ErrCanceled = errors.New("canceled")
10+
)
11+
12+
type MissingErr struct {
13+
field string
14+
}
15+
16+
func (e MissingErr) Error() string {
17+
return fmt.Sprintf("%s is required", e.field)
18+
}
19+
20+
func NewMissingErr(field string) error {
21+
return MissingErr{field: field}
22+
}

internal/render/template.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package render
22

33
import (
44
"bytes"
5-
"errors"
65
"fmt"
76
"strings"
87
"text/template"
@@ -74,7 +73,7 @@ func (t *Template) Run() ([]byte, error) {
7473
return nil, err
7574
}
7675
if v.model.Canceled() {
77-
return nil, errors.New("canceled")
76+
return nil, ErrCanceled
7877
}
7978
val := v.model.Value()
8079
// hardcode for the select options
@@ -102,18 +101,18 @@ func (t *Template) Run() ([]byte, error) {
102101

103102
func (t *Template) init() error {
104103
if isEmptyStr(t.Format) {
105-
return errors.New("format is required")
104+
return NewMissingErr("format")
106105
}
107106

108107
for _, item := range t.Items {
109108
if isEmptyStr(item.Name) {
110-
return errors.New("item.name is required")
109+
return NewMissingErr("item.name")
111110
}
112111
if isEmptyStr(item.Desc) {
113-
return errors.New("item.desc is required")
112+
return NewMissingErr("item.desc")
114113
}
115114
if isEmptyStr(item.Type) {
116-
return errors.New("item.type is required")
115+
return NewMissingErr("item.type")
117116
}
118117

119118
var m ui.Model
@@ -147,7 +146,7 @@ func (t *Template) createSelectItem(label string, options []Option) *ui.SelectMo
147146
}
148147

149148
func (t *Template) createInputItem(name, label string, required bool) *ui.InputModel {
150-
m := ui.NewInput(label)
149+
m := ui.NewInput(label).WithWidth(30)
151150
if required {
152151
m.WithValidateFunc(NotBlankValidator(name))
153152
}
@@ -166,7 +165,7 @@ func (t *Template) createTextAreaItem(name, label string, required bool) *ui.Tex
166165
func NotBlankValidator(name string) func(s string) error {
167166
return func(s string) error {
168167
if strings.TrimSpace(s) == "" {
169-
return fmt.Errorf("%s is required", name)
168+
return NewMissingErr(name)
170169
}
171170
return nil
172171
}

internal/ui/textinput.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import (
88
)
99

1010
const (
11-
DefaultInputWidth = 20
12-
DefaultInputCharLimit = 156
11+
DefaultInputWidth = 20
1312
)
1413

1514
type InputModel struct {
@@ -35,7 +34,6 @@ type InputModel struct {
3534

3635
func NewInput(label string) *InputModel {
3736
ti := textinput.New()
38-
ti.CharLimit = DefaultInputCharLimit
3937
ti.Width = DefaultInputWidth
4038
ti.EchoMode = textinput.EchoMode(EchoNormal)
4139
ti.Focus()
@@ -74,6 +72,11 @@ func (m *InputModel) WithEchoMode(mode EchoMode) *InputModel {
7472
return m
7573
}
7674

75+
func (m *InputModel) WithWidth(width int) *InputModel {
76+
m.input.Width = width
77+
return m
78+
}
79+
7780
func (m *InputModel) Init() tea.Cmd {
7881
return textinput.Blink
7982
}

internal/ui/types.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import (
55
tea "github.com/charmbracelet/bubbletea"
66
)
77

8+
// EchoMode sets the input behavior of the text input field.
9+
// EchoMode is an alias for the textinput.EchoMode.
10+
type EchoMode textinput.EchoMode
11+
812
// Model is an alias for the tea.Model.
913
type Model interface {
1014
tea.Model
@@ -13,10 +17,6 @@ type Model interface {
1317
Canceled() bool
1418
}
1519

16-
// EchoMode sets the input behavior of the text input field.
17-
// EchoMode is an alias for the textinput.EchoMode.
18-
type EchoMode textinput.EchoMode
19-
2020
const (
2121
// EchoNormal displays text as is. This is the default behavior.
2222
EchoNormal EchoMode = iota
@@ -43,11 +43,5 @@ const (
4343
colorValidateErr = "1"
4444
)
4545

46-
const DONE = "DONE"
47-
4846
// DefaultValidateFunc is a verification function that does nothing
4947
func DefaultValidateFunc(_ string) error { return nil }
50-
51-
func Done() tea.Msg {
52-
return DONE
53-
}

0 commit comments

Comments
 (0)