Merge pull request #13 from tphoney/add_silent_script

(feat) add silent version of bash and powershell Script
pull/14/head
TP Honey 3 years ago committed by GitHub
commit 7a15ed7215
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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()
}

@ -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
`

@ -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

@ -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 }
`

Loading…
Cancel
Save