-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
65 lines (58 loc) · 1.38 KB
/
repository.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
package cmcm
import (
"bytes"
"errors"
"fmt"
"os/exec"
"strings"
"github.com/cli/safeexec"
)
func parseRepository(repository string) (owner, repo string, err error) {
if repository == "" {
owner, repo, err = resolveRepository()
if err != nil {
return "", "", err
}
} else {
s := strings.Split(repository, "/")
if len(s) != 2 {
return "", "", errors.New("invalid repository name")
}
owner = s[0]
repo = s[1]
}
return owner, repo, nil
}
func resolveRepository() (owner, repo string, err error) {
args := []string{"repo", "view"}
stdOut, _, err := gh(args...)
if err != nil {
return "", "", fmt.Errorf("failed to view repo: %w", err)
}
viewOut := strings.Split(stdOut.String(), "\n")[0]
ownerRepo := strings.Split(strings.TrimSpace(strings.Split(viewOut, ":")[1]), "/")
if len(ownerRepo) != 2 {
return "", "", errors.New("failed to parse repository")
}
owner = ownerRepo[0]
repo = ownerRepo[1]
return owner, repo, nil
}
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
}