-
Notifications
You must be signed in to change notification settings - Fork 3
/
gh.go
94 lines (83 loc) · 2.18 KB
/
gh.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
package bump
import (
"bytes"
"fmt"
"os/exec"
"github.com/cli/safeexec"
)
type ghCLI struct{}
func NewGh() *ghCLI {
return &ghCLI{}
}
func (g *ghCLI) ViewRepository() (sout, eout *bytes.Buffer, err error) {
return gh("repo", "view")
}
func (g *ghCLI) ListRelease(repo string, isCurrent bool) (sout, eout *bytes.Buffer, err error) {
if isCurrent {
sout, eout, err = gh("release", "list")
} else {
sout, eout, err = gh("release", "list", "-R", repo)
}
return
}
func (g *ghCLI) ViewRelease(repo string, isCurrent bool) (sout, eout *bytes.Buffer, err error) {
if isCurrent {
sout, eout, err = gh("release", "view")
} else {
sout, eout, err = gh("release", "view", "-R", repo)
}
return
}
func (g *ghCLI) CreateRelease(version string, repo string, isCurrent bool, option *ReleaseOption) (sout, eout *bytes.Buffer, err error) {
args := []string{"release", "create", version}
if len(option.AssetFiles) > 0 {
args = append(args, option.AssetFiles...)
}
if !isCurrent {
args = append(args, []string{"-R", repo}...)
}
if option.IsDraft {
args = append(args, "--draft")
}
if option.IsPrerelease {
args = append(args, "--prerelease")
}
if option.DiscussionCategory != "" {
args = append(args, []string{"--discussion-category", option.DiscussionCategory}...)
}
if option.GenerateNotes {
args = append(args, "--generate-notes")
}
if option.Notes != "" {
args = append(args, []string{"--notes", option.Notes}...)
}
if option.NotesFilename != "" {
args = append(args, []string{"--notes-file", option.NotesFilename}...)
}
if option.Target != "" {
args = append(args, []string{"--target", option.Target}...)
}
if option.Title != "" {
args = append(args, []string{"-t", option.Title}...)
}
sout, eout, err = gh(args...)
return
}
func gh(args ...string) (sout, eout *bytes.Buffer, err error) {
sout = new(bytes.Buffer)
eout = new(bytes.Buffer)
bin, err := safeexec.LookPath("gh")
if err != nil {
err = fmt.Errorf("could not find gh. err: %w", err)
return
}
cmd := exec.Command(bin, args...)
cmd.Stdout = sout
cmd.Stderr = eout
err = cmd.Run()
if err != nil {
err = fmt.Errorf("failed to run gh. err: %w, eout: %s", err, eout.String())
return
}
return
}