From fabd1a651771c099aec387f0f8b1fa9c5aec0ba1 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 12 Mar 2018 11:25:39 -0700 Subject: [PATCH] windows cmd env vars --- common/shell-local/config.go | 11 +++++++++ common/shell-local/run.go | 24 +++++++------------ .../shell-local/post-processor_test.go | 2 +- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/common/shell-local/config.go b/common/shell-local/config.go index 64dfe25c1..f4514a014 100644 --- a/common/shell-local/config.go +++ b/common/shell-local/config.go @@ -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 { diff --git a/common/shell-local/run.go b/common/shell-local/run.go index a16573e7a..9e17d2d87 100644 --- a/common/shell-local/run.go +++ b/common/shell-local/run.go @@ -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 -// ) diff --git a/post-processor/shell-local/post-processor_test.go b/post-processor/shell-local/post-processor_test.go index afec79f81..5fabac124 100644 --- a/post-processor/shell-local/post-processor_test.go +++ b/post-processor/shell-local/post-processor_test.go @@ -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}}"} }