From a14f0bc41df58648510618cc5c35e135ca164345 Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 9 Apr 2025 11:09:22 +0200 Subject: [PATCH 1/8] add combined_output optin --- README.md | 11 +++++++++++ examples/script.js | 11 ++++++++++- exec.go | 13 ++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bfcad9e..9ecb849 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,17 @@ Once built, you can run your newly extended `k6` using: ```shell ./k6 run examples/script.js ``` +## Configuration + +Possible options + +* dir specify working dir +* continue_on_error contintue script when return code is not zeored +* include_stdout_on_error exposes stdout when return code is not zeored +* combined_output combine stdout and error in output +* env specify enviroment variables + +see examples/script.js for details ## Example diff --git a/examples/script.js b/examples/script.js index 9cf495a..69f7a7c 100644 --- a/examples/script.js +++ b/examples/script.js @@ -52,8 +52,17 @@ export default function () { + // With combine stdout en stderr + console.log("-------------------- Example 5 -------------------------------") + var output = exec.command("sh", ["-c", "echo -n 'normal output '; echo 'and an error' 1>&2"], { + "combined_output": true, + }); + console.log (output) + + + // without error handling the test will stop when the following command fails - console.log("-------------------- Example 5 -------------------------------") + console.log("-------------------- Example 6 -------------------------------") console.log(exec.command("ls",["-a","-l"], { "dir": "sub-directory" // optional directory in which the command has to be run })); diff --git a/exec.go b/exec.go index a837628..716c154 100644 --- a/exec.go +++ b/exec.go @@ -30,6 +30,7 @@ type CommandOptions struct { ContinueOnError bool IncludeStdoutOnError bool Env []string + CombinedOutput bool } type myExitError struct { @@ -62,7 +63,11 @@ func (exec *EXEC) Exports() modules.Exports { // Command is a wrapper for Go exec.Command func (*EXEC) Command(name string, args []string, option CommandOptions) (string, error) { + var out []byte + var err error + cmd := exec.Command(name, args...) + if option.Dir != "" { cmd.Dir = option.Dir } @@ -71,7 +76,13 @@ func (*EXEC) Command(name string, args []string, option CommandOptions) (string, cmd.Env = append(cmd.Environ(), option.Env...) } - out, err := cmd.Output() + if option.CombinedOutput { + out, err = cmd.CombinedOutput() + + } else { + out, err = cmd.Output() + } + if err != nil && !option.ContinueOnError { log.Fatal(err.Error() + " on command: " + name + " " + strings.Join(args, " ")) } From fc3cbdf1d7686899fe6aec1d4a8893699e5f3c3a Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 9 Apr 2025 11:17:58 +0200 Subject: [PATCH 2/8] fix linting --- exec.go | 1 - 1 file changed, 1 deletion(-) diff --git a/exec.go b/exec.go index 716c154..589cdb6 100644 --- a/exec.go +++ b/exec.go @@ -78,7 +78,6 @@ func (*EXEC) Command(name string, args []string, option CommandOptions) (string, if option.CombinedOutput { out, err = cmd.CombinedOutput() - } else { out, err = cmd.Output() } From 7603e2621a5b3f047a3c4fbce54820172936deca Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 9 Apr 2025 11:20:41 +0200 Subject: [PATCH 3/8] fix readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9ecb849..2c69bcd 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,8 @@ Once built, you can run your newly extended `k6` using: Possible options * dir specify working dir -* continue_on_error contintue script when return code is not zeored -* include_stdout_on_error exposes stdout when return code is not zeored +* continue_on_error contintue script when return code is not zero +* include_stdout_on_error exposes stdout when return code is not zero * combined_output combine stdout and error in output * env specify enviroment variables From 30da0468b6c60ad709b2331c3f13e42c9300eee8 Mon Sep 17 00:00:00 2001 From: mark-00 Date: Mon, 5 May 2025 20:51:01 +0200 Subject: [PATCH 4/8] Update README.md make the option's name consistent with the other options Co-authored-by: pablochacin --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2c69bcd..23d7d73 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Possible options * dir specify working dir * continue_on_error contintue script when return code is not zero * include_stdout_on_error exposes stdout when return code is not zero -* combined_output combine stdout and error in output +* combine_output combines stdout and error in output * env specify enviroment variables see examples/script.js for details From 7dfdf60f1bd58d3ee31a740a80e71082597d5015 Mon Sep 17 00:00:00 2001 From: mark-00 Date: Mon, 5 May 2025 20:51:12 +0200 Subject: [PATCH 5/8] Update examples/script.js make the option's name consistent with the other options Co-authored-by: pablochacin --- examples/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/script.js b/examples/script.js index 69f7a7c..eeeb7d1 100644 --- a/examples/script.js +++ b/examples/script.js @@ -55,7 +55,7 @@ export default function () { // With combine stdout en stderr console.log("-------------------- Example 5 -------------------------------") var output = exec.command("sh", ["-c", "echo -n 'normal output '; echo 'and an error' 1>&2"], { - "combined_output": true, + "combine_output": true, }); console.log (output) From 17b03186b884e2756b20868e3381cbaedf15d1a3 Mon Sep 17 00:00:00 2001 From: mark-00 Date: Mon, 5 May 2025 20:51:22 +0200 Subject: [PATCH 6/8] Update exec.go make the option's name consistent with the other options Co-authored-by: pablochacin --- exec.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exec.go b/exec.go index 589cdb6..7fa666e 100644 --- a/exec.go +++ b/exec.go @@ -30,7 +30,7 @@ type CommandOptions struct { ContinueOnError bool IncludeStdoutOnError bool Env []string - CombinedOutput bool + CombineOutput bool } type myExitError struct { From da81d404fa1f457ecd0c9f893c488c094bb7c85b Mon Sep 17 00:00:00 2001 From: mark-00 Date: Mon, 5 May 2025 20:51:38 +0200 Subject: [PATCH 7/8] Update exec.go make the option's name consistent with the other options Co-authored-by: pablochacin --- exec.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exec.go b/exec.go index 7fa666e..49fc368 100644 --- a/exec.go +++ b/exec.go @@ -76,7 +76,7 @@ func (*EXEC) Command(name string, args []string, option CommandOptions) (string, cmd.Env = append(cmd.Environ(), option.Env...) } - if option.CombinedOutput { + if option.CombineOutput { out, err = cmd.CombinedOutput() } else { out, err = cmd.Output() From 22a55884aa37a3561cd8558d4c55c9f1705a29f8 Mon Sep 17 00:00:00 2001 From: mark-00 Date: Mon, 5 May 2025 20:51:53 +0200 Subject: [PATCH 8/8] Update examples/script.js make the option's name consistent with the other options Co-authored-by: pablochacin --- examples/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/script.js b/examples/script.js index eeeb7d1..0701865 100644 --- a/examples/script.js +++ b/examples/script.js @@ -52,7 +52,7 @@ export default function () { - // With combine stdout en stderr + // With combined stdout and stderr console.log("-------------------- Example 5 -------------------------------") var output = exec.command("sh", ["-c", "echo -n 'normal output '; echo 'and an error' 1>&2"], { "combine_output": true,