diff options
Diffstat (limited to 'cmd.go')
-rw-r--r-- | cmd.go | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -0,0 +1,46 @@ +package syw + +import ( + "bytes" + "context" + "errors" + "os/exec" +) + +var gitbinpath string + +func findbin() string { + if gitbinpath == "" { + gitbinpath, _ = exec.LookPath("git") + if gitbinpath == "" { + panic("failed to find 'git' executable") + } + } + return gitbinpath +} + +func runCmd(ctx context.Context, args []string) (*cmdResult, error) { + cmd := exec.CommandContext(ctx, findbin(), args...) + + outbuf := &bytes.Buffer{} + cmd.Stdout = outbuf + errbuf := &bytes.Buffer{} + cmd.Stderr = errbuf + + var eerr *exec.ExitError + if err := cmd.Run(); err != nil && !errors.As(err, &eerr) { + return nil, err + } + + return &cmdResult{ + status: cmd.ProcessState.ExitCode(), + out: outbuf, + err: errbuf, + }, nil +} + +type cmdResult struct { + status int + out *bytes.Buffer + err *bytes.Buffer +} |