diff options
Diffstat (limited to 'run.go')
| -rw-r--r-- | run.go | 58 |
1 files changed, 50 insertions, 8 deletions
@@ -30,7 +30,7 @@ func RunCapture(cmdline string) string { return Chomp(test.Buffer) } -func RunWait(args []string) *Shell { +func RunWait(args []string) *OldShell { test := New() cmdline := strings.Join(args, " ") test.Exec(cmdline) @@ -86,7 +86,7 @@ func RunPath(path string, args []string) bool { return true } -func (cmd *Shell) Run(cmdline string) string { +func (cmd *OldShell) Run(cmdline string) string { cmd.InitProcess(cmdline) if cmd.Error != nil { return "" @@ -95,7 +95,7 @@ func (cmd *Shell) Run(cmdline string) string { return Chomp(cmd.Buffer) } -func (cmd *Shell) InitProcess(cmdline string) { +func (cmd *OldShell) InitProcess(cmdline string) { log.Log(RUN, "shell.InitProcess() START "+cmdline) cmd.Cmdline = Chomp(cmdline) // this is like 'chomp' in perl @@ -118,7 +118,7 @@ func (cmd *Shell) InitProcess(cmdline string) { cmd.Process = exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...) } -func (cmd *Shell) FileCreate(out string) { +func (cmd *OldShell) FileCreate(out string) { var newfile File var iof io.ReadCloser @@ -141,7 +141,7 @@ func (cmd *Shell) FileCreate(out string) { // NOTE: this might cause problems: // always remove the newlines at the end ? -func (cmd *Shell) Exec(cmdline string) { +func (cmd *OldShell) Exec(cmdline string) { log.Log(RUN, "shell.Run() START "+cmdline) cmd.InitProcess(cmdline) @@ -179,7 +179,7 @@ func (cmd *Shell) Exec(cmdline string) { } // nonblocking read until file errors -func (cmd *Shell) Capture(f *File) { +func (cmd *OldShell) Capture(f *File) { log.Log(RUN, "nbrREADER() START") if cmd.Buffer == nil { @@ -211,7 +211,7 @@ func (cmd *Shell) Capture(f *File) { } // returns true if filehandle buffer is empty -func (cmd *Shell) ReadToBuffer(f *File) bool { +func (cmd *OldShell) ReadToBuffer(f *File) bool { log.Log(RUN, "ReadToBuffer() START") nbr := f.Fnbreader oneByte := make([]byte, 1024) @@ -282,7 +282,6 @@ func RunCmd(workingpath string, parts []string) (error, bool, string) { 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 { @@ -293,6 +292,7 @@ func RunCmd(workingpath string, parts []string) (error, bool, string) { } else { log.Warn("cmd.Run() failed with %s\n", err) } + return err, false, string(output) } tmp := string(output) @@ -301,3 +301,45 @@ func RunCmd(workingpath string, parts []string) (error, bool, string) { // Print the output return nil, true, tmp } + +// send the path and the command +func RunCmdRun(workingpath string, parts []string) error { + if len(parts) == 0 { + log.Warn("command line was empty") + return errors.New("empty") + } + if parts[0] == "" { + log.Warn("command line was empty") + return errors.New("empty") + } + 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 + err := cmd.Run() + if err != nil { + log.Warn("ERROR working path =", workingpath, "thing =", thing, "cmdline =", parts) + log.Error(err) + log.Warn("cmd exited with error", err) + // panic("fucknuts") + + // 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) + } + return err + } + return nil +} |
