1+ // Package execr is a wrapper to run exec commands.
12package execr
23
34import (
45 "bytes"
56 "io"
67 "os"
78 "os/exec"
8- "regexp"
9-
10- "github.com/roemer/gotaskr/goext"
11- "github.com/roemer/gotaskr/log"
129)
1310
14- var argumentsRegex = regexp .MustCompile (`[^\s"]+|"((\\"|[^"])*)"` )
15-
16- // Creates a new exec.Cmd with the given executable and arguments.
17- func NewCmd (executable string , arguments ... string ) * exec.Cmd {
18- return exec .Command (executable , arguments ... )
11+ // Runs an executable with the given arguments.
12+ func Run (outputToConsole bool , executable string , arguments ... string ) error {
13+ cmd := exec .Command (executable , arguments ... )
14+ return RunCommand (outputToConsole , cmd )
1915}
2016
21- // Creates a new exec.Cmd with the given executable and the argument string splitted into separate arguments.
22- func NewCmdSplitted (executable string , arguments string ) * exec.Cmd {
23- return exec .Command (executable , SplitArguments (arguments )... )
17+ // Runs an executable with the given arguments and returns the output.
18+ func RunGetOutput (outputToConsole bool , executable string , arguments ... string ) (string , string , error ) {
19+ cmd := exec .Command (executable , arguments ... )
20+ return RunCommandGetOutput (outputToConsole , cmd )
2421}
2522
26- // Runs an executable with the given arguments.
27- func Run ( executable string , arguments [] string , options ... func ( * RunOptions )) error {
28- cmd := NewCmd (executable , arguments ... )
29- return RunCommand ( cmd , options ... )
23+ // Runs an executable with the given arguments and returns the output .
24+ func RunGetCombinedOutput ( outputToConsole bool , executable string , arguments ... string ) ( string , error ) {
25+ cmd := exec . Command (executable , arguments ... )
26+ return RunCommandGetCombinedOutput ( outputToConsole , cmd )
3027}
3128
32- // Runs a command with the given arguments.
33- func RunCommand (cmd * exec.Cmd , options ... func (* RunOptions )) error {
34- runOptions := prepare (cmd , options ... )
29+ // Runs a command and writes the stdout and stderr into the console in realtime.
30+ func RunCommand (outputToConsole bool , cmd * exec.Cmd ) error {
3531 logArguments (cmd )
36- if runOptions .outputToConsole {
32+
33+ if outputToConsole {
3734 cmd .Stdout = os .Stdout
3835 cmd .Stderr = os .Stderr
3936 }
@@ -45,91 +42,34 @@ func RunCommand(cmd *exec.Cmd, options ...func(*RunOptions)) error {
4542 return err
4643}
4744
48- // Runs an executable with the given arguments and returns the separate output from stdout and stderr.
49- func RunGetOutput (executable string , arguments []string , options ... func (* RunOptions )) (string , string , error ) {
50- cmd := NewCmd (executable , arguments ... )
51- return RunCommandGetOutput (cmd , options ... )
52- }
53-
54- // Runs a command with the given arguments and returns the separate output from stdout and stderr.
55- func RunCommandGetOutput (cmd * exec.Cmd , options ... func (* RunOptions )) (string , string , error ) {
56- runOptions := prepare (cmd , options ... )
45+ func RunCommandGetOutput (outputToConsole bool , cmd * exec.Cmd ) (string , string , error ) {
5746 logArguments (cmd )
47+
5848 var stdoutBuf , stderrBuf bytes.Buffer
59- if runOptions . outputToConsole {
49+ if outputToConsole {
6050 cmd .Stdout = io .MultiWriter (os .Stdout , & stdoutBuf )
6151 cmd .Stderr = io .MultiWriter (os .Stderr , & stderrBuf )
6252 } else {
6353 cmd .Stdout = & stdoutBuf
6454 cmd .Stderr = & stderrBuf
6555 }
6656 err := cmd .Run ()
67- if runOptions .skipPostProcessOutput {
68- return stdoutBuf .String (), stderrBuf .String (), err
69- }
70- return processOutputString (stdoutBuf .String ()), processOutputString (stderrBuf .String ()), err
71- }
72-
73- // Runs an executable with the given arguments and returns the output from stdout and stderr combined.
74- func RunGetCombinedOutput (executable string , arguments []string , options ... func (* RunOptions )) (string , error ) {
75- cmd := NewCmd (executable , arguments ... )
76- return RunCommandGetCombinedOutput (cmd , options ... )
57+ outStr , errStr := processOutputString (stdoutBuf .String ()), processOutputString (stderrBuf .String ())
58+ return outStr , errStr , err
7759}
7860
79- // Runs a command with the given arguments and returns the output from stdout and stderr combined.
80- func RunCommandGetCombinedOutput (cmd * exec.Cmd , options ... func (* RunOptions )) (string , error ) {
81- runOptions := prepare (cmd , options ... )
61+ func RunCommandGetCombinedOutput (outputToConsole bool , cmd * exec.Cmd ) (string , error ) {
8262 logArguments (cmd )
63+
8364 var outBuf bytes.Buffer
84- if runOptions . outputToConsole {
65+ if outputToConsole {
8566 cmd .Stdout = io .MultiWriter (os .Stdout , & outBuf )
8667 cmd .Stderr = io .MultiWriter (os .Stderr , & outBuf )
8768 } else {
8869 cmd .Stdout = & outBuf
8970 cmd .Stderr = & outBuf
9071 }
9172 err := cmd .Run ()
92- if runOptions .skipPostProcessOutput {
93- return outBuf .String (), err
94- }
95- return processOutputString (outBuf .String ()), err
96- }
97-
98- // Splits a string of arguments into a slice of strings, handling quoted arguments correctly.
99- func SplitArguments (arguments string ) []string {
100- return argumentsRegex .FindAllString (arguments , - 1 )
101- }
102-
103- // Returns a slice of strings containing the given arguments.
104- func Arguments (arguments ... string ) []string {
105- return arguments
106- }
107-
108- ////////////////////////////////////////////////////////////
109- // Internal
110- ////////////////////////////////////////////////////////////
111-
112- func prepare (cmd * exec.Cmd , options ... func (* RunOptions )) * RunOptions {
113- // Build the options
114- runOptions := & RunOptions {}
115- for _ , option := range options {
116- option (runOptions )
117- }
118- // Arguments
119- if len (runOptions .arguments ) > 0 {
120- cmd .Args = append (cmd .Args , runOptions .arguments ... )
121- }
122- // Working directory
123- if runOptions .workingDirectory != "" {
124- cmd .Dir = runOptions .workingDirectory
125- }
126- return runOptions
127- }
128-
129- func logArguments (cmd * exec.Cmd ) {
130- log .Debugf ("Executing '%s' with arguments: %s" , cmd .Path , cmd .Args [1 :])
131- }
132-
133- func processOutputString (value string ) string {
134- return goext .TrimNewlineSuffix (value )
73+ outStr := processOutputString (outBuf .String ())
74+ return outStr , err
13575}
0 commit comments