ability to set environment variable in shell script

pull/1/head
Brad Rydzewski 5 years ago
parent 1c863ab7b0
commit a8307ea13b

@ -4,6 +4,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
### Added
- semver environment variables
- optional environment variables in scripts
## [1.1.0] - 2019-07-14
### Added

@ -26,8 +26,15 @@ func Command() (string, []string) {
// Script converts a slice of individual shell commands to
// a powershell script.
func Script(commands []string) string {
var buf bytes.Buffer
func Script(commands []string, environ map[string]string) string {
buf := new(bytes.Buffer)
for k, v := range environ {
fmt.Fprintln(buf)
fmt.Fprintf(buf, exportScript, k, v)
}
fmt.Fprintln(buf)
fmt.Fprintf(buf, optionScript)
fmt.Fprintln(buf)
for _, command := range commands {
escaped := fmt.Sprintf("%q", "+ "+command)
escaped = strings.Replace(escaped, "$", "`$", -1)
@ -37,18 +44,16 @@ func Script(commands []string) string {
command,
))
}
return fmt.Sprintf(
buildScript,
buf.String(),
)
return buf.String()
}
// buildScript is a helper script this is added to the build
// to prepare the environment and execute the build commands.
const buildScript = `
$erroractionpreference = "stop"
%s
`
// optionScript is a helper script this is added to the build
// to set shell options, in this case, to exit on error.
const optionScript = `$erroractionpreference = "stop"`
// exportScript is a helper script that is added to
// the build script to export environment variables.
const exportScript = "$Env:%s = %q"
// traceScript is a helper script that is added to
// the build script to trace a command.

@ -30,7 +30,15 @@ func TestCommands(t *testing.T) {
}
func TestScript(t *testing.T) {
got, want := Script([]string{"go build", "go test"}), exampleScript
got, want := Script([]string{"go build", "go test"}, nil), exampleScript
if got != want {
t.Errorf("Want %q, got %q", want, got)
}
}
func TestScriptEnviron(t *testing.T) {
env := map[string]string{"GOOS": "linux"}
got, want := Script([]string{"go build", "go test"}, env), exampleScriptEnviron
if got != want {
t.Errorf("Want %q, got %q", want, got)
}
@ -44,5 +52,15 @@ go build
echo "+ go test"
go test
`
var exampleScriptEnviron = `
$Env:GOOS = "linux"
$erroractionpreference = "stop"
echo "+ go build"
go build
echo "+ go test"
go test
`

@ -25,8 +25,15 @@ func Command() (string, []string) {
// Script converts a slice of individual shell commands to
// a posix-compliant shell script.
func Script(commands []string) string {
var buf bytes.Buffer
func Script(commands []string, environ map[string]string) string {
buf := new(bytes.Buffer)
for k, v := range environ {
fmt.Fprintln(buf)
fmt.Fprintf(buf, exportScript, k, v)
}
fmt.Fprintln(buf)
fmt.Fprintf(buf, optionScript)
fmt.Fprintln(buf)
for _, command := range commands {
escaped := fmt.Sprintf("%q", command)
escaped = strings.Replace(escaped, "$", `\$`, -1)
@ -36,18 +43,16 @@ func Script(commands []string) string {
command,
))
}
return fmt.Sprintf(
buildScript,
buf.String(),
)
return buf.String()
}
// buildScript is a helper script this is added to the build
// to prepare the environment and execute the build commands.
const buildScript = `
set -e
%s
`
// optionScript is a helper script this is added to the build
// to set shell options, in this case, to exit on error.
const optionScript = "set -e"
// exportScript is a helper script that is added to
// the build script to export environment variables.
const exportScript = "export %s=%q"
// traceScript is a helper script that is added to
// the build script to trace a command.

@ -28,7 +28,15 @@ func TestCommands(t *testing.T) {
}
func TestScript(t *testing.T) {
got, want := Script([]string{"go build", "go test"}), exampleScript
got, want := Script([]string{"go build", "go test"}, nil), exampleScript
if got != want {
t.Errorf("Want %q, got %q", want, got)
}
}
func TestScriptEnviron(t *testing.T) {
env := map[string]string{"GOOS": "linux"}
got, want := Script([]string{"go build", "go test"}, env), exampleScriptEnviron
if got != want {
t.Errorf("Want %q, got %q", want, got)
}
@ -42,5 +50,15 @@ go build
echo + "go test"
go test
`
var exampleScriptEnviron = `
export GOOS="linux"
set -e
echo + "go build"
go build
echo + "go test"
go test
`

@ -18,6 +18,6 @@ func Command() (string, []string) {
// Script converts a slice of individual shell commands to
// a powershell script.
func Script(commands []string) string {
return powershell.Script(commands)
func Script(commands []string, environ map[string]string) string {
return powershell.Script(commands, environ)
}

Loading…
Cancel
Save