summaryrefslogtreecommitdiff
path: root/run.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-02-12 21:51:29 -0600
committerJeff Carr <[email protected]>2024-02-12 21:51:29 -0600
commit98730aed8ae78d41ee45f923b5110e1912f0a2fb (patch)
treea810f7d08b7776526902e53e989a6c5fbbcba2ed /run.go
parent79d5ca71a8acb40baddb1bd3699058d36413d86e (diff)
Diffstat (limited to 'run.go')
-rw-r--r--run.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/run.go b/run.go
index 15e4633..a239d88 100644
--- a/run.go
+++ b/run.go
@@ -3,11 +3,13 @@ package shell
import (
"bufio"
"bytes"
+ "errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
+ "syscall"
"time"
"github.com/svent/go-nbreader"
@@ -237,3 +239,65 @@ func (cmd *Shell) ReadToBuffer(f *File) bool {
io.WriteString(cmd.Buffer, strings.Trim(string(oneByte), "\x00"))
return false
}
+
+// send the path and the command
+func RunCmd(workingpath string, parts []string) (error, bool, string) {
+ if len(parts) == 0 {
+ log.Warn("command line was empty")
+ return errors.New("empty"), false, ""
+ }
+ if parts[0] == "" {
+ log.Warn("command line was empty")
+ return errors.New("empty"), false, ""
+ }
+ thing := parts[0]
+ parts = parts[1:]
+ log.Log(INFO, "working path =", workingpath, "thing =", thing, "cmdline =", parts)
+
+ // Create the command
+ cmd := exec.Command(thing, parts...)
+
+ // Set the working directory
+ cmd.Dir = workingpath
+
+ // Execute the command
+ output, err := cmd.CombinedOutput()
+ if err != nil {
+ if thing == "git" {
+ log.Log(INFO, "git ERROR. maybe okay", workingpath, "thing =", thing, "cmdline =", parts)
+ log.Log(INFO, "git ERROR. maybe okay err =", err)
+ if err.Error() == "exit status 1" {
+ log.Log(INFO, "git ERROR. normal exit status 1")
+ if parts[0] == "diff-index" {
+ log.Log(INFO, "git normal diff-index when repo dirty")
+ return nil, false, "git diff-index exit status 1"
+ }
+ }
+ }
+
+ log.Warn("ERROR working path =", workingpath, "thing =", thing, "cmdline =", parts)
+ log.Warn("ERROR working path =", workingpath, "thing =", thing, "cmdline =", parts)
+ log.Warn("ERROR working path =", workingpath, "thing =", thing, "cmdline =", parts)
+ log.Error(err)
+ log.Warn("output was", string(output))
+ log.Warn("cmd exited with error", err)
+ // panic("fucknuts")
+ return err, false, string(output)
+
+ // The command failed (non-zero exit status)
+ if exitErr, ok := err.(*exec.ExitError); ok {
+ // Assert that it is an exec.ExitError and get the exit code
+ if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
+ log.Warn("Exit Status: %d\n", status.ExitStatus())
+ }
+ } else {
+ log.Warn("cmd.Run() failed with %s\n", err)
+ }
+ }
+
+ tmp := string(output)
+ tmp = strings.TrimSpace(tmp)
+
+ // Print the output
+ return nil, true, tmp
+}