-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
121 lines (112 loc) · 2.74 KB
/
exec.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
package main
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
)
func asExitErr(err error) *exec.ExitError {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
return exitErr
}
return nil
}
type runCmdOpts struct {
env map[string]string
dir string
stdout io.Writer
stderr io.Writer
noLog bool
}
func runCmd(ctx context.Context, opts *runCmdOpts, command string, args ...string) (string, error) {
if opts == nil {
opts = &runCmdOpts{}
}
logger := getLogger(ctx)
if opts.noLog {
logger = discardLogger
}
cmd := exec.CommandContext(ctx, command, args...)
cmd.Dir = opts.dir
cmd.Env = os.Environ()
for k, v := range opts.env {
addCmdEnv(cmd, k, v)
}
var stdoutBuf, stderrBuf bytes.Buffer
cmd.Stdout = &stdoutBuf
if opts.stdout != nil {
cmd.Stdout = io.MultiWriter(cmd.Stdout, opts.stdout)
}
cmd.Stderr = &stderrBuf
if opts.stderr != nil {
cmd.Stderr = io.MultiWriter(cmd.Stderr, opts.stderr)
}
logger.Debug("running command", "command", command, "args", args)
err := cmd.Run()
if err != nil {
err = errors.Join(err, errors.New(stderrBuf.String()))
return "", err
}
return strings.TrimSpace(stdoutBuf.String()), nil
}
// runCmdHandleLines lets us run a command that has a large output and handle
// one line at a time without waiting for the command to finish or buffering
// the entire output in memory.
func runCmdHandleLines(
ctx context.Context,
dir string,
cmdLine []string,
handleLine func(line string, cancel context.CancelFunc),
) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
cmd := exec.CommandContext(ctx, cmdLine[0], cmdLine[1:]...)
cmd.Dir = dir
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
err = cmd.Start()
if err != nil {
return err
}
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
line := scanner.Text()
handleLine(line, cancel)
}
err = cmd.Wait()
if err == nil {
return nil
}
if !errors.Is(err, context.Canceled) && err.Error() != "signal: killed" {
return err
}
return nil
}
func getGithubRepoFromRemote(ctx context.Context, dir, remote string) (string, error) {
orig, err := runCmd(ctx, &runCmdOpts{
dir: dir,
}, "git", "remote", "get-url", remote)
if err != nil {
return "", err
}
orig = strings.TrimSpace(orig)
remoteURL := strings.TrimSuffix(orig, ".git")
// trim everything before the last : for ssh remotes
remoteURL = remoteURL[strings.LastIndex(remoteURL, ":")+1:]
parts := strings.Split(remoteURL, "/")
if len(parts) < 2 {
return "", fmt.Errorf("remote url is not a properly formated github repo url: %s", orig)
}
return strings.Join(parts[len(parts)-2:], "/"), nil
}
func addCmdEnv(cmd *exec.Cmd, key string, val any) {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%v", key, val))
}