doExecute implements Unix/Linux/macOS specific command execution. It handles direct command execution with optional shell wrapping.
(output io.Writer)
| 16 | // doExecute implements Unix/Linux/macOS specific command execution. |
| 17 | // It handles direct command execution with optional shell wrapping. |
| 18 | func (e *executor) doExecute(output io.Writer) error { |
| 19 | var ( |
| 20 | cmd *exec.Cmd |
| 21 | displayCmd string |
| 22 | ) |
| 23 | |
| 24 | // Build command and display string. |
| 25 | if len(e.args) == 0 { |
| 26 | cmd = exec.Command("bash", "-c", e.command) |
| 27 | displayCmd = e.command |
| 28 | } else { |
| 29 | cmd = exec.Command(e.command, e.args...) |
| 30 | displayCmd = e.command + " " + strings.Join(e.args, " ") |
| 31 | } |
| 32 | |
| 33 | // Display execution info. |
| 34 | if e.title != "" { |
| 35 | color.Printf(color.Title, "\n%s\n", e.title) |
| 36 | color.Printf(color.Hint, "▶ %s\n", displayCmd) |
| 37 | } |
| 38 | |
| 39 | // Verify and set working directory. |
| 40 | if e.workDir != "" { |
| 41 | if !fileio.PathExists(e.workDir) { |
| 42 | return fmt.Errorf("work directory does not exist: %s", e.workDir) |
| 43 | } |
| 44 | cmd.Dir = e.workDir |
| 45 | } |
| 46 | |
| 47 | // Set up environment and stdin. |
| 48 | cmd.Env = os.Environ() |
| 49 | cmd.Stdin = os.Stdin |
| 50 | |
| 51 | // Create and configure log file. |
| 52 | logFile, err := e.createLogFile(cmd) |
| 53 | if err != nil { |
| 54 | return fmt.Errorf("failed to setup logging: %w", err) |
| 55 | } |
| 56 | if logFile != nil { |
| 57 | defer logFile.Close() |
| 58 | } |
| 59 | |
| 60 | // Route output to appropriate destinations. |
| 61 | e.configureOutputs(cmd, logFile, output) |
| 62 | |
| 63 | // Execute command and return result. |
| 64 | if err := cmd.Run(); err != nil { |
| 65 | return fmt.Errorf("faild to execute command: %w", err) |
| 66 | } |
| 67 | |
| 68 | return nil |
| 69 | } |
no test coverage detected