From 8b167b9942bb2afddf873644355177c4c9fb04bc Mon Sep 17 00:00:00 2001 From: TP Honey Date: Thu, 26 Aug 2021 10:52:25 +0100 Subject: [PATCH] (feat) add silent version of bash and powershell Script --- shell/bash/bash.go | 27 ++++++++++++++++++++------- shell/bash/bash_test.go | 15 +++++++++++++++ shell/powershell/powershell.go | 29 +++++++++++++++++++++-------- shell/powershell/powershell_test.go | 19 ++++++++++++++++++- 4 files changed, 74 insertions(+), 16 deletions(-) diff --git a/shell/bash/bash.go b/shell/bash/bash.go index c72e169..9c3d7ed 100644 --- a/shell/bash/bash.go +++ b/shell/bash/bash.go @@ -21,9 +21,16 @@ func Command() (string, []string) { return "/bin/sh", []string{"-e"} } -// Script converts a slice of individual shell commands to -// a posix-compliant shell script. func Script(commands []string) string { + return script(commands, true) +} + +func SilentScript(commands []string) string { + return script(commands, false) +} + +// Script converts a slice of individual shell commands to a posix-compliant shell script. +func script(commands []string, trace bool) string { buf := new(bytes.Buffer) fmt.Fprintln(buf) fmt.Fprintf(buf, optionScript) @@ -31,11 +38,17 @@ func Script(commands []string) string { for _, command := range commands { escaped := fmt.Sprintf("%q", command) escaped = strings.Replace(escaped, "$", `\$`, -1) - buf.WriteString(fmt.Sprintf( - traceScript, - escaped, - command, - )) + var stringToWrite string + if trace { + stringToWrite = fmt.Sprintf( + traceScript, + escaped, + command, + ) + } else { + stringToWrite = "\n" + command + "\n" + } + buf.WriteString(stringToWrite) } return buf.String() } diff --git a/shell/bash/bash_test.go b/shell/bash/bash_test.go index e8f18e7..9639fe7 100644 --- a/shell/bash/bash_test.go +++ b/shell/bash/bash_test.go @@ -32,6 +32,13 @@ func TestScript(t *testing.T) { } } +func TestSilentScript(t *testing.T) { + got, want := SilentScript([]string{"go build", "go test"}), exampleSilentScript + if got != want { + t.Errorf("Want %q, got %q", want, got) + } +} + var exampleScript = ` set -e @@ -41,3 +48,11 @@ go build echo + "go test" go test ` + +var exampleSilentScript = ` +set -e + +go build + +go test +` diff --git a/shell/powershell/powershell.go b/shell/powershell/powershell.go index cd64def..5b60835 100644 --- a/shell/powershell/powershell.go +++ b/shell/powershell/powershell.go @@ -24,9 +24,17 @@ func Command() (string, []string) { } } +func Script(commands []string) string { + return script(commands, true) +} + +func SilentScript(commands []string) string { + return script(commands, false) +} + // Script converts a slice of individual shell commands to // a powershell script. -func Script(commands []string) string { +func script(commands []string, trace bool) string { buf := new(bytes.Buffer) fmt.Fprintln(buf) fmt.Fprintf(buf, optionScript) @@ -34,11 +42,17 @@ func Script(commands []string) string { for _, command := range commands { escaped := fmt.Sprintf("%q", "+ "+command) escaped = strings.Replace(escaped, "$", "`$", -1) - buf.WriteString(fmt.Sprintf( - traceScript, - escaped, - command, - )) + var stringToWrite string + if trace { + stringToWrite = fmt.Sprintf( + traceScript, + escaped, + command, + ) + } else { + stringToWrite = "\n" + command + "\nif ($LastExitCode -gt 0) { exit $LastExitCode }\n" + } + buf.WriteString(stringToWrite) } return buf.String() } @@ -47,8 +61,7 @@ func Script(commands []string) string { // to set shell options, in this case, to exit on error. const optionScript = `$erroractionpreference = "stop"` -// traceScript is a helper script that is added to -// the build script to trace a command. +// traceScript is a helper script that is added to the build script to trace a command. const traceScript = ` echo %s %s diff --git a/shell/powershell/powershell_test.go b/shell/powershell/powershell_test.go index 2c0efde..4825be0 100644 --- a/shell/powershell/powershell_test.go +++ b/shell/powershell/powershell_test.go @@ -32,7 +32,14 @@ func TestCommands(t *testing.T) { func TestScript(t *testing.T) { got, want := Script([]string{"go build", "go test"}), exampleScript if got != want { - t.Errorf("Want %q, got %q", want, got) + t.Errorf("Want %q\ngot %q", want, got) + } +} + +func TestSilentScript(t *testing.T) { + got, want := SilentScript([]string{"go build", "go test"}), exampleSilentScript + if got != want { + t.Errorf("Want \n%q\ngot\n%q", want, got) } } @@ -47,3 +54,13 @@ echo "+ go test" go test if ($LastExitCode -gt 0) { exit $LastExitCode } ` + +var exampleSilentScript = ` +$erroractionpreference = "stop" + +go build +if ($LastExitCode -gt 0) { exit $LastExitCode } + +go test +if ($LastExitCode -gt 0) { exit $LastExitCode } +`