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 +}