summaryrefslogtreecommitdiff
path: root/cmd.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd.go')
-rw-r--r--cmd.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/cmd.go b/cmd.go
new file mode 100644
index 0000000..fca36cb
--- /dev/null
+++ b/cmd.go
@@ -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
+}