-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-dpcommit.go
154 lines (127 loc) · 3.8 KB
/
git-dpcommit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
)
const (
fixType = "[fix]: A code/bug fix;"
featType = "[feat]: A new feature;"
docsType = "[docs]: Documentation only changes;"
testType = "[test]: Adding missing tests or correcting existing tests;"
refactorType = "[refactor]: A code change that neither fixes a bug nor adds a feature;"
styleType = "[style]: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc);"
perfType = "[perf]: A code change that improves performance;"
buildType = "[build]: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm);"
ciType = "[ci]: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs);"
breakingType = "[breaking change]: Change that will require other changes in dependant applications;"
colorYellow = "\033[33m"
colorGreen = "\033[32m"
colorReset = "\033[0m"
textBold = "\033[1m"
colorRed = "\033[31m"
)
var (
mapCommitTypes = map[string]string{
fixType: "fix",
featType: "feat",
docsType: "docs",
testType: "test",
refactorType: "refactor",
styleType: "style",
perfType: "perf",
buildType: "build",
ciType: "ci",
breakingType: "breaking change",
}
)
func main() {
var devToolCmd = &cobra.Command{
Use: ".",
Short: "A commit message CLI helper",
Run: runCli,
}
var rootCmd = &cobra.Command{Use: "root"}
rootCmd.AddCommand(devToolCmd)
rootCmd.Execute()
}
func printGreenYellow(tag, text string) {
fmt.Println(textBold + colorGreen + tag + colorReset + colorYellow + text + colorReset)
}
func printRed(text string) {
fmt.Println(colorRed + text + colorReset)
}
func printGreen(text string) {
fmt.Println(colorGreen + text + colorReset)
}
func getCommitType(selectedValue string) string {
return mapCommitTypes[selectedValue]
}
func runCli(cmd *cobra.Command, args []string) {
commitTypeOptions := []string{
fixType,
featType,
docsType,
testType,
refactorType,
styleType,
perfType,
buildType,
ciType,
breakingType,
}
var commitType string
prompt := &survey.Select{
Message: "Select the commit type:",
Options: commitTypeOptions,
}
survey.AskOne(prompt, &commitType)
commitMessage := stringPrompt("What is the commit message?")
commitType = getCommitType(commitType)
commitScope := stringPrompt("[Optional - ENTER to ignore] What is the commit scope?")
fullMessage := fmt.Sprintf("%s: %s", commitType, commitMessage)
if commitScope != "" {
fullMessage = fmt.Sprintf("%s(%s): %s", commitType, commitScope, commitMessage)
}
fmt.Printf("Your commit message is: %s.\n", fullMessage)
commit(fullMessage)
printGreen("Done!")
}
func stringPrompt(label string) string {
var s string
r := bufio.NewReader(os.Stdin)
for {
fmt.Fprint(os.Stderr, label+" ")
s, _ = r.ReadString('\n')
if s != "" {
break
}
}
return strings.TrimSpace(s)
}
func commit(message string) {
fmt.Println("commiting...")
path, err := exec.Command("pwd").Output()
if err != nil {
printRed(fmt.Sprintf("Oops! Error to get current path: %s", err.Error()))
log.Fatal(err)
}
location := string(path[:])
command := fmt.Sprintf("git commit -m \"%s\"", message)
printGreenYellow("[Running] ", fmt.Sprintf("%s from %s", command, location))
out, errRun := exec.Command("bash", "-c", command).Output()
if errRun != nil {
printRed(fmt.Sprintf("Oops! Error to commit: %s", errRun.Error()))
if strings.Contains(errRun.Error(), "exit status 128") {
printRed("It looks like you are not logged in git, try: [git config --global user.email \"[email protected]\"]")
}
log.Fatal(errRun)
}
output := string(out[:])
printGreen(output)
}