Exec handles command execution for all cases, parameterized by the args. It executes the given command string, waiting for the command to finish, handling the given arguments appropriately. If there is any error, it adds it to the shell, and triggers CancelExecution. - errOk = don't call AddError so
(errOk, start, output bool, cmd any, args ...any)
| 27 | // a goroutine forked to Wait for it and close its IO |
| 28 | // - output = return the output of the command as a string (otherwise return is "") |
| 29 | func (sh *Shell) Exec(errOk, start, output bool, cmd any, args ...any) string { |
| 30 | out := "" |
| 31 | if !errOk && len(sh.Errors) > 0 { |
| 32 | return out |
| 33 | } |
| 34 | cmdIO := exec.NewCmdIO(&sh.Config) |
| 35 | cmdIO.StackStart() |
| 36 | if start { |
| 37 | cmdIO.PushIn(nil) // no stdin for bg |
| 38 | } |
| 39 | cl, scmd, sargs := sh.ExecArgs(cmdIO, errOk, cmd, args...) |
| 40 | if scmd == "" { |
| 41 | return out |
| 42 | } |
| 43 | var err error |
| 44 | if cl != nil { |
| 45 | switch { |
| 46 | case start: |
| 47 | err = cl.Start(&cmdIO.StdIOState, scmd, sargs...) |
| 48 | case output: |
| 49 | cmdIO.PushOut(nil) |
| 50 | out, err = cl.Output(&cmdIO.StdIOState, scmd, sargs...) |
| 51 | default: |
| 52 | err = cl.Run(&cmdIO.StdIOState, scmd, sargs...) |
| 53 | } |
| 54 | if !errOk { |
| 55 | sh.AddError(err) |
| 56 | } |
| 57 | } else { |
| 58 | ran := false |
| 59 | ran, out = sh.RunBuiltinOrCommand(cmdIO, errOk, output, scmd, sargs...) |
| 60 | if !ran { |
| 61 | sh.isCommand.Push(false) |
| 62 | switch { |
| 63 | case start: |
| 64 | err = sh.Config.StartIO(cmdIO, scmd, sargs...) |
| 65 | sh.Jobs.Push(cmdIO) |
| 66 | go func() { |
| 67 | if !cmdIO.OutIsPipe() { |
| 68 | fmt.Printf("[%d] %s\n", len(sh.Jobs), cmdIO.String()) |
| 69 | } |
| 70 | cmdIO.Cmd.Wait() |
| 71 | cmdIO.PopToStart() |
| 72 | sh.DeleteJob(cmdIO) |
| 73 | }() |
| 74 | case output: |
| 75 | cmdIO.PushOut(nil) |
| 76 | out, err = sh.Config.OutputIO(cmdIO, scmd, sargs...) |
| 77 | default: |
| 78 | err = sh.Config.RunIO(cmdIO, scmd, sargs...) |
| 79 | } |
| 80 | if !errOk { |
| 81 | sh.AddError(err) |
| 82 | } |
| 83 | sh.isCommand.Pop() |
| 84 | } |
| 85 | } |
| 86 | if !start { |
no test coverage detected