From f7d4bf877a45a9d253c548e902d6d1bd7d907d23 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 16 Feb 2022 11:37:40 +0100 Subject: [PATCH] all shells: Pass env vars through a key value store (#98) --- cmd/packer-sdc/internal/test-data/.gitignore | 1 + shell-local/config.go | 13 ------------- shell-local/config.hcl2spec.go | 2 ++ shell-local/run.go | 6 ++++++ shell/shell.go | 9 +++++++-- 5 files changed, 16 insertions(+), 15 deletions(-) create mode 100644 cmd/packer-sdc/internal/test-data/.gitignore diff --git a/cmd/packer-sdc/internal/test-data/.gitignore b/cmd/packer-sdc/internal/test-data/.gitignore new file mode 100644 index 000000000..1b9355bd3 --- /dev/null +++ b/cmd/packer-sdc/internal/test-data/.gitignore @@ -0,0 +1 @@ +*.mdx diff --git a/shell-local/config.go b/shell-local/config.go index 37cdf4e02..cb04e62b2 100644 --- a/shell-local/config.go +++ b/shell-local/config.go @@ -96,19 +96,6 @@ func Validate(config *Config) error { } } - // Clean up input - if config.Inline != nil && len(config.Inline) == 0 { - config.Inline = make([]string, 0) - } - - if config.Scripts == nil { - config.Scripts = make([]string, 0) - } - - if config.Vars == nil { - config.Vars = make([]string, 0) - } - // Verify that the user has given us a command to run if config.Command == "" && len(config.Inline) == 0 && len(config.Scripts) == 0 && config.Script == "" { diff --git a/shell-local/config.hcl2spec.go b/shell-local/config.hcl2spec.go index 085322dfe..92e2db360 100644 --- a/shell-local/config.hcl2spec.go +++ b/shell-local/config.hcl2spec.go @@ -23,6 +23,7 @@ type FlatConfig struct { Scripts []string `cty:"scripts" hcl:"scripts"` ValidExitCodes []int `mapstructure:"valid_exit_codes" cty:"valid_exit_codes" hcl:"valid_exit_codes"` Vars []string `mapstructure:"environment_vars" cty:"environment_vars" hcl:"environment_vars"` + Env map[string]string `mapstructure:"env" cty:"env" hcl:"env"` EnvVarFormat *string `mapstructure:"env_var_format" cty:"env_var_format" hcl:"env_var_format"` Command *string `cty:"command" hcl:"command"` ExecuteCommand []string `mapstructure:"execute_command" cty:"execute_command" hcl:"execute_command"` @@ -57,6 +58,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "scripts": &hcldec.AttrSpec{Name: "scripts", Type: cty.List(cty.String), Required: false}, "valid_exit_codes": &hcldec.AttrSpec{Name: "valid_exit_codes", Type: cty.List(cty.Number), Required: false}, "environment_vars": &hcldec.AttrSpec{Name: "environment_vars", Type: cty.List(cty.String), Required: false}, + "env": &hcldec.AttrSpec{Name: "env", Type: cty.Map(cty.String), Required: false}, "env_var_format": &hcldec.AttrSpec{Name: "env_var_format", Type: cty.String, Required: false}, "command": &hcldec.AttrSpec{Name: "command", Type: cty.String, Required: false}, "execute_command": &hcldec.AttrSpec{Name: "execute_command", Type: cty.List(cty.String), Required: false}, diff --git a/shell-local/run.go b/shell-local/run.go index a82f4ba7d..71404db7c 100644 --- a/shell-local/run.go +++ b/shell-local/run.go @@ -206,6 +206,12 @@ func createFlattenedEnvVars(config *Config) (string, error) { envVars[keyValue[0]] = strings.Replace(keyValue[1], "'", `'"'"'`, -1) } + for k, v := range config.Env { + // Store pair, replacing any single quotes in value so they parse + // correctly with required environment variable format + envVars[k] = strings.Replace(v, "'", `'"'"'`, -1) + } + // Create a list of env var keys in sorted order var keys []string for k := range envVars { diff --git a/shell/shell.go b/shell/shell.go index 180955cbf..0e69cd955 100644 --- a/shell/shell.go +++ b/shell/shell.go @@ -36,10 +36,15 @@ type Provisioner struct { // for examples such as 3010 - "The requested operation is successful. ValidExitCodes []int `mapstructure:"valid_exit_codes"` - // An array of environment variables that will be injected before - // your command(s) are executed. + // An array of environment variables that will be injected before your + // command(s) are executed. Any duplicate vars will be overridden by `env`. Vars []string `mapstructure:"environment_vars"` + // An map of environment variables that will be injected before your + // command(s) are executed. Any duplicate `environment_vars` will be + // overridden by `env`. + Env map[string]string `mapstructure:"env"` + // This is used in the template generation to format environment variables // inside the `ExecuteCommand` template. EnvVarFormat string `mapstructure:"env_var_format"`