Skip to content

Commit e5fcfd4

Browse files
committed
Readded the old Run methods, renamed the new to RunO
1 parent 7c98a8b commit e5fcfd4

File tree

15 files changed

+212
-133
lines changed

15 files changed

+212
-133
lines changed

build/build.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ func init() {
3636
////////////////////////////////////////////////////////////
3737

3838
func UpdateDependencies() error {
39-
if err := execr.Run("go", execr.SplitArguments("get -u"), execr.WithConsoleOutput(true)); err != nil {
39+
if err := execr.RunO("go", execr.WithArgsSplitted("get -u"), execr.WithConsoleOutput(true)); err != nil {
4040
return err
4141
}
42-
if err := execr.Run("go", execr.SplitArguments("mod tidy"), execr.WithConsoleOutput(true)); err != nil {
42+
if err := execr.RunO("go", execr.WithArgsSplitted("mod tidy"), execr.WithConsoleOutput(true)); err != nil {
4343
return err
4444
}
4545
return nil
@@ -51,19 +51,19 @@ func RunTests() error {
5151
}
5252
goTestReport := path.Join(reportPath, "go-test-report.txt")
5353
junitTestReport := path.Join(reportPath, "junit-test-report.xml")
54-
if err := execr.Run("go", execr.SplitArguments("install github.com/jstemmer/go-junit-report/v2@latest"), execr.WithConsoleOutput(true)); err != nil {
54+
if err := execr.RunO("go", execr.WithArgsSplitted("install github.com/jstemmer/go-junit-report/v2@latest"), execr.WithConsoleOutput(true)); err != nil {
5555
return err
5656
}
5757

58-
stdout, _, err := execr.RunGetOutput("go", execr.SplitArguments("test -v ./... "), execr.WithConsoleOutput(true))
58+
stdout, _, err := execr.RunOGetOutput("go", execr.WithArgsSplitted("test -v ./... "), execr.WithConsoleOutput(true))
5959
if err != nil {
6060
return nil
6161
}
6262
if err := os.WriteFile(goTestReport, []byte(stdout), os.ModePerm); err != nil {
6363
return err
6464
}
6565

66-
if err := execr.Run(path.Join(build.Default.GOPATH, "bin/go-junit-report"), execr.Arguments("-in", goTestReport, "-set-exit-code", "-out", junitTestReport), execr.WithConsoleOutput(true)); err != nil {
66+
if err := execr.RunO(path.Join(build.Default.GOPATH, "bin/go-junit-report"), execr.WithArgs("-in", goTestReport, "-set-exit-code", "-out", junitTestReport), execr.WithConsoleOutput(true)); err != nil {
6767
return err
6868
}
6969
return nil

execr/execr.go

Lines changed: 27 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
1+
// Package execr is a wrapper to run exec commands.
12
package execr
23

34
import (
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
}

execr/execr_with_options.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package execr
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"os"
7+
"os/exec"
8+
)
9+
10+
// Runs an executable with the given options.
11+
func RunO(executable string, options ...func(*RunOptions)) error {
12+
cmd := NewCmd(executable)
13+
return RunCommandO(cmd, options...)
14+
}
15+
16+
// Runs a command with the given options.
17+
func RunCommandO(cmd *exec.Cmd, options ...func(*RunOptions)) error {
18+
runOptions := prepare(cmd, options...)
19+
logArguments(cmd)
20+
if runOptions.outputToConsole {
21+
cmd.Stdout = os.Stdout
22+
cmd.Stderr = os.Stderr
23+
}
24+
err := cmd.Start()
25+
if err != nil {
26+
return err
27+
}
28+
err = cmd.Wait()
29+
return err
30+
}
31+
32+
// Runs an executable with the given options and returns the separate output from stdout and stderr.
33+
func RunOGetOutput(executable string, options ...func(*RunOptions)) (string, string, error) {
34+
cmd := NewCmd(executable)
35+
return RunCommandOGetOutput(cmd, options...)
36+
}
37+
38+
// Runs a command with the given options and returns the separate output from stdout and stderr.
39+
func RunCommandOGetOutput(cmd *exec.Cmd, options ...func(*RunOptions)) (string, string, error) {
40+
runOptions := prepare(cmd, options...)
41+
logArguments(cmd)
42+
var stdoutBuf, stderrBuf bytes.Buffer
43+
if runOptions.outputToConsole {
44+
cmd.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
45+
cmd.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)
46+
} else {
47+
cmd.Stdout = &stdoutBuf
48+
cmd.Stderr = &stderrBuf
49+
}
50+
err := cmd.Run()
51+
if runOptions.skipPostProcessOutput {
52+
return stdoutBuf.String(), stderrBuf.String(), err
53+
}
54+
return processOutputString(stdoutBuf.String()), processOutputString(stderrBuf.String()), err
55+
}
56+
57+
// Runs an executable with the given options and returns the output from stdout and stderr combined.
58+
func RunOGetCombinedOutput(executable string, options ...func(*RunOptions)) (string, error) {
59+
cmd := NewCmd(executable)
60+
return RunCommandOGetCombinedOutput(cmd, options...)
61+
}
62+
63+
// Runs a command with the given options and returns the output from stdout and stderr combined.
64+
func RunCommandOGetCombinedOutput(cmd *exec.Cmd, options ...func(*RunOptions)) (string, error) {
65+
runOptions := prepare(cmd, options...)
66+
logArguments(cmd)
67+
var outBuf bytes.Buffer
68+
if runOptions.outputToConsole {
69+
cmd.Stdout = io.MultiWriter(os.Stdout, &outBuf)
70+
cmd.Stderr = io.MultiWriter(os.Stderr, &outBuf)
71+
} else {
72+
cmd.Stdout = &outBuf
73+
cmd.Stderr = &outBuf
74+
}
75+
err := cmd.Run()
76+
if runOptions.skipPostProcessOutput {
77+
return outBuf.String(), err
78+
}
79+
return processOutputString(outBuf.String()), err
80+
}
81+
82+
////////////////////////////////////////////////////////////
83+
// Internal
84+
////////////////////////////////////////////////////////////
85+
86+
func prepare(cmd *exec.Cmd, options ...func(*RunOptions)) *RunOptions {
87+
// Build the options
88+
runOptions := &RunOptions{}
89+
for _, option := range options {
90+
option(runOptions)
91+
}
92+
// Arguments
93+
if len(runOptions.arguments) > 0 {
94+
cmd.Args = append(cmd.Args, runOptions.arguments...)
95+
}
96+
// Working directory
97+
if runOptions.workingDirectory != "" {
98+
cmd.Dir = runOptions.workingDirectory
99+
}
100+
return runOptions
101+
}

execr/misc.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package execr
2+
3+
import (
4+
"os/exec"
5+
"regexp"
6+
7+
"github.com/roemer/gotaskr/goext"
8+
"github.com/roemer/gotaskr/log"
9+
)
10+
11+
var argumentsRegex = regexp.MustCompile(`[^\s"]+|"((\\"|[^"])*)"`)
12+
13+
// Creates a new exec.Cmd with the given executable and arguments.
14+
func NewCmd(executable string, arguments ...string) *exec.Cmd {
15+
return exec.Command(executable, arguments...)
16+
}
17+
18+
// Creates a new exec.Cmd with the given executable and the argument string splitted into separate arguments.
19+
func NewCmdSplitted(executable string, arguments string) *exec.Cmd {
20+
return exec.Command(executable, SplitArgumentString(arguments)...)
21+
}
22+
23+
// Splits a string of arguments into a slice of strings, handling quoted arguments correctly.
24+
func SplitArgumentString(arguments string) []string {
25+
return argumentsRegex.FindAllString(arguments, -1)
26+
}
27+
28+
////////////////////////////////////////////////////////////
29+
// Internal
30+
////////////////////////////////////////////////////////////
31+
32+
func logArguments(cmd *exec.Cmd) {
33+
log.Debugf("Executing '%s' with arguments: %s", cmd.Path, cmd.Args[1:])
34+
}
35+
36+
func processOutputString(value string) string {
37+
return goext.TrimNewlineSuffix(value)
38+
}

execr/options.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ type RunOptions struct {
99
}
1010

1111
// Appends the given arguments.
12-
func WithArguments(arguments ...string) func(*RunOptions) {
12+
func WithArgs(arguments ...string) func(*RunOptions) {
1313
return func(options *RunOptions) {
1414
options.arguments = append(options.arguments, arguments...)
1515
}
1616
}
1717

1818
// Splits the given argument string into separate arguments and appends them.
19-
func WithArgumentsSplitted(arguments string) func(*RunOptions) {
19+
func WithArgsSplitted(arguments string) func(*RunOptions) {
2020
return func(options *RunOptions) {
21-
options.arguments = append(options.arguments, SplitArguments(arguments)...)
21+
options.arguments = append(options.arguments, SplitArgumentString(arguments)...)
2222
}
2323
}
2424

gotaskr_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ func TestLifeTimeTaskSetupAndTaskTeardownError(t *testing.T) {
376376
////////////////////
377377

378378
func getExitError(errorCode int) error {
379-
return execr.Run(getBashPath(), []string{"-c", fmt.Sprintf("exit %d", errorCode)})
379+
return execr.RunO(getBashPath(), execr.WithArgs("-c", fmt.Sprintf("exit %d", errorCode)))
380380
}
381381

382382
func getBashPath() string {

gttools/cypress.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,21 @@ func (tool *CypressTool) CypressOpenWithYarn(settings *CypressOpenSettings) erro
132132
func cypressExecute(cypressBinPath string, args []string, settings ToolSettingsBase) error {
133133
cmd := exec.Command(cypressBinPath, goext.RemoveEmpty(args)...)
134134
cmd.Dir = settings.WorkingDirectory
135-
return execr.RunCommand(cmd, execr.WithConsoleOutput(settings.OutputToConsole))
135+
return execr.RunCommandO(cmd, execr.WithConsoleOutput(settings.OutputToConsole))
136136
}
137137

138138
func cypressExecuteWithNpx(args []string, settings ToolSettingsBase) error {
139139
args = append([]string{"cypress"}, args...)
140140
cmd := exec.Command("npx", goext.RemoveEmpty(args)...)
141141
cmd.Dir = settings.WorkingDirectory
142-
return execr.RunCommand(cmd, execr.WithConsoleOutput(settings.OutputToConsole))
142+
return execr.RunCommandO(cmd, execr.WithConsoleOutput(settings.OutputToConsole))
143143
}
144144

145145
func cypressExecuteWithYarn(args []string, settings ToolSettingsBase) error {
146146
args = append([]string{"cypress"}, args...)
147147
cmd := exec.Command("yarn", goext.RemoveEmpty(args)...)
148148
cmd.Dir = settings.WorkingDirectory
149-
return execr.RunCommand(cmd, execr.WithConsoleOutput(settings.OutputToConsole))
149+
return execr.RunCommandO(cmd, execr.WithConsoleOutput(settings.OutputToConsole))
150150
}
151151

152152
func (settings *CypressRunSettings) buildCliArguments() []string {

0 commit comments

Comments
 (0)