-
Notifications
You must be signed in to change notification settings - Fork 0
/
commit.go
87 lines (70 loc) · 1.39 KB
/
commit.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
package main
import (
"log"
"os"
"os/exec"
"strings"
"github.com/AlecAivazis/survey"
)
var qs = []*survey.Question{
{
Name: "tag",
Prompt: &survey.Select{
Message: "Tag",
Options: []string{"feat", "fix", "style", "refactor", "test", "docs", "clean", "perf", "chore"},
},
},
{
Name: "subject",
Validate: survey.Required,
Prompt: &survey.Input{Message: "Subject"},
},
{
Name: "issue",
Prompt: &survey.Input{Message: "Issue"},
},
{
Name: "scope",
Prompt: &survey.Input{Message: "Scope"},
},
}
type answersType struct {
Issue string
Tag string
Scope string
Subject string
}
func main() {
answers := takeSurvey()
message := formatCommitMessage(answers)
commit(message)
}
func takeSurvey() answersType {
answers := answersType{}
err := survey.Ask(qs, &answers)
if err != nil {
log.Fatal(err)
}
return answers
}
func formatCommitMessage(answers answersType) string {
var message strings.Builder
if answers.Issue != "" {
message.WriteString(answers.Issue + " ")
}
message.WriteString(answers.Tag)
if answers.Scope != "" {
message.WriteString("(" + answers.Scope + ")")
}
message.WriteString(": " + answers.Subject)
return message.String()
}
func commit(message string) {
cmd := exec.Command("git", "commit", "-m", message)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}