Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions shell/bash/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,34 @@ 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)
fmt.Fprintln(buf)
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()
}
Expand Down
15 changes: 15 additions & 0 deletions shell/bash/bash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -41,3 +48,11 @@ go build
echo + "go test"
go test
`

var exampleSilentScript = `
set -e

go build

go test
`
29 changes: 21 additions & 8 deletions shell/powershell/powershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,35 @@ 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)
fmt.Fprintln(buf)
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()
}
Expand All @@ -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
Expand Down
19 changes: 18 additions & 1 deletion shell/powershell/powershell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

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