Skip to content

Commit

Permalink
all shells: Pass env vars through a key value store (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
azr committed Feb 16, 2022
1 parent f98795f commit f7d4bf8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions cmd/packer-sdc/internal/test-data/.gitignore
@@ -0,0 +1 @@
*.mdx
13 changes: 0 additions & 13 deletions shell-local/config.go
Expand Up @@ -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 == "" {
Expand Down
2 changes: 2 additions & 0 deletions shell-local/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions shell-local/run.go
Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions shell/shell.go
Expand Up @@ -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"`
Expand Down

0 comments on commit f7d4bf8

Please sign in to comment.