-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutil.go
42 lines (34 loc) · 823 Bytes
/
util.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
package main
import (
"bytes"
"fmt"
"os/exec"
"strings"
"github.com/cli/safeexec"
)
// gh shells out to gh, returning STDOUT/STDERR and any error
func gh(args ...string) (sout, eout bytes.Buffer, err error) {
ghBin, err := safeexec.LookPath("gh")
if err != nil {
err = fmt.Errorf("could not find gh. Is it installed? error: %w", err)
return
}
cmd := exec.Command(ghBin, args...)
cmd.Stderr = &eout
cmd.Stdout = &sout
err = cmd.Run()
if err != nil {
err = fmt.Errorf("failed to run gh. error: %w, stderr: %s", err, eout.String())
return
}
return
}
func resolveRepository() (string, error) {
sout, _, err := gh("repo", "view")
if err != nil {
return "", err
}
viewOut := strings.Split(sout.String(), "\n")[0]
repo := strings.TrimSpace(strings.Split(viewOut, ":")[1])
return repo, nil
}