|
| 1 | +/* |
| 2 | +Copyright (c) 2024 Murex |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in all |
| 12 | +copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +package toolchain |
| 24 | + |
| 25 | +import ( |
| 26 | + "bufio" |
| 27 | + "github.com/murex/tcr/report" |
| 28 | + "os/exec" |
| 29 | +) |
| 30 | + |
| 31 | +type ( |
| 32 | + // CommandRunner is in charge of managing the lifecycle of a command |
| 33 | + CommandRunner struct { |
| 34 | + command *exec.Cmd |
| 35 | + } |
| 36 | + |
| 37 | + // CommandStatus is the result status of a Command execution |
| 38 | + CommandStatus string |
| 39 | + |
| 40 | + // CommandResult contains the result from running a Command |
| 41 | + // - Status |
| 42 | + CommandResult struct { |
| 43 | + Status CommandStatus |
| 44 | + Output string |
| 45 | + } |
| 46 | +) |
| 47 | + |
| 48 | +// Failed indicates is a Command failed |
| 49 | +func (r CommandResult) Failed() bool { |
| 50 | + return r.Status == CommandStatusFail |
| 51 | +} |
| 52 | + |
| 53 | +// Passed indicates is a Command passed |
| 54 | +func (r CommandResult) Passed() bool { |
| 55 | + return r.Status == CommandStatusPass |
| 56 | +} |
| 57 | + |
| 58 | +// List of possible values for CommandStatus |
| 59 | +const ( |
| 60 | + CommandStatusPass CommandStatus = "pass" |
| 61 | + CommandStatusFail CommandStatus = "fail" |
| 62 | + CommandStatusUnknown CommandStatus = "unknown" |
| 63 | +) |
| 64 | + |
| 65 | +// commandRunner singleton instance |
| 66 | +var commandRunner = getCommandRunner() |
| 67 | + |
| 68 | +func getCommandRunner() *CommandRunner { |
| 69 | + return &CommandRunner{ |
| 70 | + command: nil, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// Run launches the execution of the provided command |
| 75 | +func (r *CommandRunner) Run(cmd *Command) (result CommandResult) { |
| 76 | + result = CommandResult{Status: CommandStatusUnknown, Output: ""} |
| 77 | + report.PostText(cmd.asCommandLine()) |
| 78 | + |
| 79 | + // Prepare the command |
| 80 | + r.command = exec.Command(cmd.Path, cmd.Arguments...) //nolint:gosec |
| 81 | + r.command.Dir = GetWorkDir() |
| 82 | + |
| 83 | + // Allow simultaneous trace and capture of command's stdout and stderr |
| 84 | + outReader, _ := r.command.StdoutPipe() |
| 85 | + errReader, _ := r.command.StderrPipe() |
| 86 | + doneOut, doneErr := make(chan bool), make(chan bool) |
| 87 | + r.reportCommandTrace(bufio.NewScanner(outReader), doneOut) |
| 88 | + r.reportCommandTrace(bufio.NewScanner(errReader), doneErr) |
| 89 | + |
| 90 | + // Start the command asynchronously |
| 91 | + errStart := r.command.Start() |
| 92 | + if errStart != nil { |
| 93 | + report.PostError("Failed to run command: ", errStart.Error()) |
| 94 | + r.command = nil |
| 95 | + return result |
| 96 | + } |
| 97 | + |
| 98 | + // Wait for the command to finish |
| 99 | + errWait := r.command.Wait() |
| 100 | + if errWait != nil { |
| 101 | + result.Status = CommandStatusFail |
| 102 | + } else { |
| 103 | + result.Status = CommandStatusPass |
| 104 | + } |
| 105 | + |
| 106 | + r.command = nil |
| 107 | + return result |
| 108 | +} |
| 109 | + |
| 110 | +func (*CommandRunner) reportCommandTrace(scanner *bufio.Scanner, done chan bool) { |
| 111 | + go func() { |
| 112 | + for scanner.Scan() { |
| 113 | + report.PostText(scanner.Text()) |
| 114 | + } |
| 115 | + done <- true |
| 116 | + }() |
| 117 | +} |
0 commit comments