Skip to content

Commit 7b17125

Browse files
committed
refactor(all): refactor code
refactor code Signed-off-by: mritd <[email protected]>
1 parent d225fe3 commit 7b17125

File tree

7 files changed

+73
-150
lines changed

7 files changed

+73
-150
lines changed

apps.go

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

33
import (
44
"fmt"
5+
tea "github.com/charmbracelet/bubbletea"
6+
"time"
57

68
"github.com/urfave/cli/v2"
79
)
@@ -11,7 +13,7 @@ var mainApp = &cli.App{
1113
Usage: "Git Flow ToolKit",
1214
Version: fmt.Sprintf("%s %s %s", version, buildDate, commitID),
1315
Authors: []*cli.Author{{Name: "mritd", Email: "[email protected]"}},
14-
Copyright: "Copyright (c) 2020 mritd, All rights reserved.",
16+
Copyright: "Copyright (c) " + time.Now().Format("2006") + " mritd, All rights reserved.",
1517
EnableBashCompletion: true,
1618
Action: func(c *cli.Context) error {
1719
return cli.ShowAppHelp(c)
@@ -44,7 +46,7 @@ func newBranchApp(ct string) *cli.App {
4446
UsageText: fmt.Sprintf("git %s BRANCH", ct),
4547
Version: fmt.Sprintf("%s %s %s", version, buildDate, commitID),
4648
Authors: []*cli.Author{{Name: "mritd", Email: "[email protected]"}},
47-
Copyright: "Copyright (c) 2020 mritd, All rights reserved.",
49+
Copyright: "Copyright (c) " + time.Now().Format("2006") + " mritd, All rights reserved.",
4850
EnableBashCompletion: true,
4951
Action: func(c *cli.Context) error {
5052
if c.NArg() != 1 {
@@ -66,13 +68,23 @@ func commitApp() *cli.App {
6668
UsageText: "git ci",
6769
Version: fmt.Sprintf("%s %s %s", version, buildDate, commitID),
6870
Authors: []*cli.Author{{Name: "mritd", Email: "[email protected]"}},
69-
Copyright: "Copyright (c) 2020 mritd, All rights reserved.",
71+
Copyright: "Copyright (c) " + time.Now().Format("2006") + " mritd, All rights reserved.",
7072
EnableBashCompletion: true,
7173
Action: func(c *cli.Context) error {
7274
if c.NArg() != 0 {
7375
return cli.ShowAppHelp(c)
7476
}
75-
return runCommit()
77+
78+
m := model{
79+
views: []tea.Model{
80+
newSelectorModel(),
81+
newInputsModel(),
82+
newSpinnerModel(),
83+
newResultModel(),
84+
},
85+
}
86+
87+
return tea.NewProgram(&m).Start()
7688
},
7789
}
7890
}
@@ -84,7 +96,7 @@ func checkMessageApp() *cli.App {
8496
UsageText: "commit-msg FILE",
8597
Version: fmt.Sprintf("%s %s %s", version, buildDate, commitID),
8698
Authors: []*cli.Author{{Name: "mritd", Email: "[email protected]"}},
87-
Copyright: "Copyright (c) 2020 mritd, All rights reserved.",
99+
Copyright: "Copyright (c) " + time.Now().Format("2006") + " mritd, All rights reserved.",
88100
EnableBashCompletion: true,
89101
Action: func(c *cli.Context) error {
90102
if c.NArg() != 1 {
@@ -102,7 +114,7 @@ func pushApp() *cli.App {
102114
UsageText: "git ps",
103115
Version: fmt.Sprintf("%s %s %s", version, buildDate, commitID),
104116
Authors: []*cli.Author{{Name: "mritd", Email: "[email protected]"}},
105-
Copyright: "Copyright (c) 2020 mritd, All rights reserved.",
117+
Copyright: "Copyright (c) " + time.Now().Format("2006") + " mritd, All rights reserved.",
106118
EnableBashCompletion: true,
107119
Action: func(c *cli.Context) error {
108120
if c.NArg() != 0 {

common.go

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

git_wapper.go

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7-
tea "github.com/charmbracelet/bubbletea"
7+
"io"
88
"io/ioutil"
99
"os"
10+
"os/exec"
1011
"regexp"
12+
"runtime"
1113
"strings"
1214
)
1315

@@ -16,12 +18,12 @@ func createBranch(name string) error {
1618
if err != nil {
1719
return fmt.Errorf("the current directory is not a git repository")
1820
}
19-
return gitCommand(os.Stdout, []string{"checkout", "-b", name})
21+
return gitCommand(os.Stdout, "checkout", "-b", name)
2022
}
2123

2224
func hasStagedFiles() error {
2325
var buf bytes.Buffer
24-
err := gitCommand(&buf, []string{"diff", "--cached", "--name-only"})
26+
err := gitCommand(&buf, "diff", "--cached", "--name-only")
2527
if err != nil {
2628
return err
2729
}
@@ -33,7 +35,7 @@ func hasStagedFiles() error {
3335

3436
func currentBranch() (string, error) {
3537
var buf bytes.Buffer
36-
err := gitCommand(&buf, []string{"symbolic-ref", "--short", "HEAD"})
38+
err := gitCommand(&buf, "symbolic-ref", "--short", "HEAD")
3739
if err != nil {
3840
return "", err
3941
}
@@ -50,7 +52,7 @@ func push() error {
5052
if err != nil {
5153
return err
5254
}
53-
return gitCommand(os.Stdout, []string{"push", "origin", branch})
55+
return gitCommand(os.Stdout, "push", "origin", branch)
5456
}
5557

5658
func commitMessageCheck(f string) error {
@@ -68,25 +70,7 @@ func commitMessageCheck(f string) error {
6870
return nil
6971
}
7072

71-
func runCommit() error {
72-
err := repoCheck()
73-
if err != nil {
74-
return fmt.Errorf("the current directory is not a git repository")
75-
}
76-
77-
m := model{
78-
views: []tea.Model{
79-
newSelectorModel(),
80-
newInputsModel(),
81-
newSpinnerModel(),
82-
newResultModel(),
83-
},
84-
}
85-
86-
return tea.NewProgram(&m).Start()
87-
}
88-
89-
func execCommit(msg commitMsg) error {
73+
func commit(msg commitMsg) error {
9074
if err := hasStagedFiles(); err != nil {
9175
return err
9276
}
@@ -110,28 +94,28 @@ func execCommit(msg commitMsg) error {
11094
}
11195

11296
var errBuf bytes.Buffer
113-
err = gitCommand(&errBuf, []string{"commit", "-F", f.Name()})
97+
err = gitCommand(&errBuf, "commit", "-F", f.Name())
11498
if err != nil {
11599
return errors.New(strings.TrimSpace(errBuf.String()))
116100
}
117101

118102
return nil
119103
}
120104

121-
func createSOB() string {
105+
func createSOB() (string, error) {
122106
name, email, err := gitAuthor()
123107
if err != nil {
124-
panic(err)
108+
return "", err
125109
}
126-
return fmt.Sprintf("Signed-off-by: %s %s", name, email)
110+
return fmt.Sprintf("Signed-off-by: %s %s", name, email), nil
127111
}
128112

129113
func gitAuthor() (string, string, error) {
130114
name := "Undefined"
131115
email := "Undefined"
132116

133117
var buf bytes.Buffer
134-
err := gitCommand(&buf, []string{"var", "GIT_AUTHOR_IDENT"})
118+
err := gitCommand(&buf, "var", "GIT_AUTHOR_IDENT")
135119
if err != nil {
136120
return "", "", err
137121
}
@@ -147,5 +131,30 @@ func gitAuthor() (string, string, error) {
147131
}
148132

149133
func repoCheck() error {
150-
return gitCommand(ioutil.Discard, []string{"rev-parse", "--show-toplevel"})
134+
var buf bytes.Buffer
135+
err := gitCommand(&buf, "rev-parse", "--show-toplevel")
136+
if err != nil {
137+
return errors.New(strings.TrimSpace(buf.String()))
138+
}
139+
return nil
140+
}
141+
142+
func gitCommand(out io.Writer, cmds ...string) error {
143+
var cmd *exec.Cmd
144+
switch runtime.GOOS {
145+
case "windows":
146+
cmd = exec.Command("git.exe", cmds...)
147+
case "linux", "darwin":
148+
cmd = exec.Command("git", cmds...)
149+
default:
150+
return fmt.Errorf("unsupported platform")
151+
}
152+
153+
cmd.Stdin = os.Stdin
154+
if out != nil {
155+
cmd.Stdout = out
156+
cmd.Stderr = out
157+
}
158+
159+
return cmd.Run()
151160
}

install.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func install(dir string) error {
5050
},
5151
func() (string, error) {
5252
// ignore unset failed error
53-
_ = gitCommand(ioutil.Discard, []string{"config", "--global", "--unset", "core.hooksPath"})
53+
_ = gitCommand(ioutil.Discard, "config", "--global", "--unset", "core.hooksPath")
5454
return "✔ Unset commit hooks...", nil
5555
},
5656
func() (string, error) {
@@ -109,7 +109,7 @@ func install(dir string) error {
109109
// return "✔ Set commit hooks...", nil
110110
//},
111111
func() (string, error) {
112-
err := gitCommand(ioutil.Discard, []string{"test"})
112+
err := gitCommand(ioutil.Discard, "test")
113113
if err != nil {
114114
return "", fmt.Errorf("💔 install failed: %s", err)
115115
}
@@ -163,11 +163,11 @@ func uninstall(dir string) error {
163163
},
164164
func() (string, error) {
165165
// ignore unset failed error
166-
_ = gitCommand(ioutil.Discard, []string{"config", "--global", "--unset", "core.hooksPath"})
166+
_ = gitCommand(ioutil.Discard, "config", "--global", "--unset", "core.hooksPath")
167167
return "✔ Unset commit hooks...", nil
168168
},
169169
func() (string, error) {
170-
err := gitCommand(ioutil.Discard, []string{"test"})
170+
err := gitCommand(ioutil.Discard, "test")
171171
if err == nil {
172172
return "", fmt.Errorf("💔 uninstall failed: %s", err)
173173
}

ui.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ func (m model) Init() tea.Cmd {
5454
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5555
switch msg.(type) {
5656
case done: // If the view returns a done message, it means that the stage has been processed
57-
// Copy error
5857
m.err = msg.(done).err
59-
if m.err == nil {
58+
// If there is error in msg, immediately display an error message and exit
59+
if m.err != nil {
60+
m.viewIndex = RESULT
61+
} else {
6062
// Call the next view
6163
m.viewIndex++
62-
} else {
63-
m.viewIndex = RESULT
6464
}
6565

6666
// some special views need to determine the state of the data to update
@@ -84,13 +84,18 @@ func (m model) View() string {
8484
}
8585

8686
func (m model) commit() tea.Msg {
87+
sob, err := createSOB()
88+
if err != nil {
89+
return done{err: err}
90+
}
91+
8792
return commitMsg{
8893
Type: m.views[SELECTOR].(selectorModel).choice,
8994
Scope: m.views[INPUTS].(inputsModel).inputs[0].Value(),
9095
Subject: m.views[INPUTS].(inputsModel).inputs[1].Value(),
9196
Body: m.views[INPUTS].(inputsModel).inputs[2].Value(),
9297
Footer: m.views[INPUTS].(inputsModel).inputs[3].Value(),
93-
SOB: createSOB(),
98+
SOB: sob,
9499
}
95100
}
96101

ui_spinner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (m spinnerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5050
m.spinner, cmd = m.spinner.Update(spinner.Tick())
5151
return m, tea.Batch(cmd, func() tea.Msg {
5252
time.Sleep(500 * time.Millisecond)
53-
return done{err: execCommit(msg)}
53+
return done{err: commit(msg)}
5454
})
5555
default:
5656
var cmd tea.Cmd

version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v2.0.1
1+
v2.1.0

0 commit comments

Comments
 (0)