Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all shells: Pass env vars through a key value store #98

Merged
merged 4 commits into from Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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