-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommand.go
163 lines (131 loc) · 3.65 KB
/
command.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
155
156
157
158
159
160
161
162
163
package main
import (
"errors"
"os"
"os/exec"
"os/user"
"strings"
)
// Gdp is the interface which has methods deploying and publising.
type Gdp interface {
IsMasterOrMainBranch() bool
IsExistTagInLocal(tag string) bool
IsExistTagInRemote(tag string) bool
GetMergeCommitList(toTag string) (string, error)
GetLatestTag() string
Deploy(tag string) error
Publish(tag string, commits string) error
}
// Command implements Git interface.
type Command struct {
}
// NewCommand is GdpCommand's constructor.
func NewCommand() (Gdp, error) {
if err := availableCommand("git"); err != nil {
return nil, err
}
if err := availableCommand("hub"); err != nil {
return nil, err
}
if !isExistsCredential() {
return nil, errors.New("please setup hub's credential file")
}
return &Command{}, nil
}
// IsMasterOrMainBranch checks current branch is master|main or not.
func (c *Command) IsMasterOrMainBranch() bool {
out, err := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD").CombinedOutput()
if err != nil {
return false
}
branch := strings.TrimRight(string(out), "\n")
if branch != "master" && branch != "main" {
return false
}
return true
}
// IsExistTagInLocal checks the tag exist or not in local repository.
func (c *Command) IsExistTagInLocal(tag string) bool {
out, err := exec.Command("git", "show", tag).CombinedOutput()
if err != nil {
return false
}
if string(out) == "" {
return false
}
return true
}
// IsExistTagInRemote checks the tag exist or not in remote(origin) repository.
func (c *Command) IsExistTagInRemote(tag string) bool {
out, err := exec.Command("git", "ls-remote", "--tags", "origin", tag).CombinedOutput()
if err != nil {
return false
}
if string(out) == "" {
return false
}
return true
}
// GetMergeCommitList gets merge-commits list from previous tag to the tag
func (c *Command) GetMergeCommitList(toTag string) (string, error) {
fromTag := getPreviousTag(toTag)
if fromTag != "" {
fromTag = fromTag + ".."
}
format := "--pretty=format:- %an: %b"
out, err := exec.Command("git", "log", "--merges", "--first-parent", format, fromTag+toTag).CombinedOutput()
if err != nil {
return "", errors.New(string(out))
}
return strings.TrimRight(string(out), "\n"), nil
}
// GetLatestTag gets lastest tag name.
func (c *Command) GetLatestTag() string {
out, err := exec.Command("git", "describe", "--abbrev=0", "--tags").CombinedOutput()
if err != nil {
return "" // No Tag
}
return strings.TrimRight(string(out), "\n")
}
// Deploy adds the tag and push the tag to remote(origin) repository.
func (c *Command) Deploy(tag string) error {
out, err := exec.Command("git", "tag", tag).CombinedOutput()
if err != nil {
return errors.New(string(out))
}
out, err = exec.Command("git", "push", "origin", tag).CombinedOutput()
if err != nil {
return errors.New(string(out))
}
return nil
}
// Publish creates the release note in Github.
func (c *Command) Publish(tag string, message string) error {
out, err := exec.Command("hub", "release", "create", "-m", message, tag).CombinedOutput()
if err != nil {
return errors.New(string(out))
}
return nil
}
func availableCommand(name string) error {
out, err := exec.Command(name, "--version").CombinedOutput()
if err != nil {
if string(out) == "" {
return err
}
return errors.New(string(out))
}
return nil
}
func isExistsCredential() bool {
u, _ := user.Current()
_, err := os.Stat(u.HomeDir + "/.config/hub")
return err == nil
}
func getPreviousTag(tag string) string {
out, err := exec.Command("git", "describe", "--abbrev=0", "--tags", tag+"^").CombinedOutput()
if err != nil {
return "" // No Tag
}
return strings.TrimRight(string(out), "\n")
}