summaryrefslogtreecommitdiff
path: root/cmd.go
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-09-16 20:43:19 -0600
committertjpcc <tjp@ctrl-c.club>2023-09-16 20:43:19 -0600
commit90fa2795ad177b9add2fe46382576993e96ece4b (patch)
tree0e2847f8694fa66a914680329c4515101b5a45ab /cmd.go
Initial commit
Gemini repository browsing
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
+}