windows cmd env vars

This commit is contained in:
Megan Marsh 2018-03-12 11:25:39 -07:00
parent 9651432378
commit fabd1a6517
3 changed files with 20 additions and 17 deletions

View File

@ -38,6 +38,8 @@ type Config struct {
// An array of environment variables that will be injected before
// your command(s) are executed.
Vars []string `mapstructure:"environment_vars"`
EnvVarFormat string
// End dedupe with postprocessor
// The command used to execute the script. The '{{ .Path }}' variable
@ -162,6 +164,15 @@ func Validate(config *Config) error {
"the Script or Scripts options instead"))
}
}
// This is currently undocumented and not a feature users are expected to
// interact with.
if config.EnvVarFormat == "" {
if (runtime.GOOS == "windows") && !config.UseLinuxPathing {
config.EnvVarFormat = `set "%s=%s" && `
} else {
config.EnvVarFormat = "%s='%s' "
}
}
// Do a check for bad environment variables, such as '=foo', 'foobar'
for _, kv := range config.Vars {

View File

@ -6,7 +6,6 @@ import (
"io/ioutil"
"log"
"os"
"runtime"
"sort"
"strings"
@ -36,7 +35,10 @@ func Run(ui packer.Ui, config *Config) (bool, error) {
}
// Create environment variables to set before executing the command
flattenedEnvVars := createFlattenedEnvVars(config)
flattenedEnvVars, err := createFlattenedEnvVars(config)
if err != nil {
return false, err
}
for _, script := range scripts {
interpolatedCmds, err := createInterpolatedCommands(config, script, flattenedEnvVars)
@ -123,8 +125,8 @@ func createInterpolatedCommands(config *Config, script string, flattenedEnvVars
return interpolatedCmds, nil
}
func createFlattenedEnvVars(config *Config) (flattened string) {
flattened = ""
func createFlattenedEnvVars(config *Config) (string, error) {
flattened := ""
envVars := make(map[string]string)
// Always available Packer provided env vars
@ -146,18 +148,8 @@ func createFlattenedEnvVars(config *Config) (flattened string) {
}
sort.Strings(keys)
// Re-assemble vars surrounding value with single quotes and flatten
if runtime.GOOS == "windows" {
log.Printf("MEGAN NEED TO IMPLEMENT")
// createEnvVarsSourceFileWindows()
}
for _, key := range keys {
flattened += fmt.Sprintf("%s='%s' ", key, envVars[key])
flattened += fmt.Sprintf(config.EnvVarFormat, key, envVars[key])
}
return
return flattened, nil
}
// func createFlattenedEnvVarsWindows(
// // The default shell, cmd, can set vars via dot sourcing
// // set TESTXYZ=XYZ
// )

View File

@ -139,7 +139,7 @@ func TestPostProcessorPrepare_ExecuteCommand(t *testing.T) {
p = new(PostProcessor)
p.Configure(raws)
if runtime.GOOS != "windows" {
expected = []string{"sh", "-c", `chmod +x "{{.Script}}"; {{.Vars}} "{{.Script}}"`}
expected = []string{"/bin/sh", "-c", "{{.Vars}}", "{{.Script}}"}
} else {
expected = []string{"cmd", "/C", "{{.Vars}}", "{{.Script}}"}
}