-
Notifications
You must be signed in to change notification settings - Fork 53
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
Extra arguments environment variables #150
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2e19ec3
first draft of the populate function
0990230
Now extra arguments can use environment variables. Fixes #146
anubhavmishra 3c6d43e
now using environment variables while executing terraform commands fo…
anubhavmishra f742498
fix order of terraform command arguments
anubhavmishra df0edb3
Adding single quote around terraform command
anubhavmishra 8311c79
please work this time :)
anubhavmishra 8a85827
fix order for the quote
anubhavmishra 6078938
fix order for the quote
anubhavmishra 6fea2c0
Adding single quote around terraform command
anubhavmishra 637cbeb
now creating terraform command string to address bash interpolation
db31eda
now using 'sh' instead of 'bash' to execute terraform command and add…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package terraform | |
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"regexp" | ||
|
||
|
@@ -52,14 +53,31 @@ func (c *Client) Version() *version.Version { | |
|
||
// RunCommandWithVersion executes the provided version of terraform with | ||
// the provided args in path. | ||
func (c *Client) RunCommandWithVersion(log *logging.SimpleLogger, path string, args []string, v *version.Version) (string, error) { | ||
func (c *Client) RunCommandWithVersion(log *logging.SimpleLogger, path string, args []string, v *version.Version, env string) (string, error) { | ||
tfExecutable := "terraform" | ||
// if version is the same as the default, don't need to prepend the version name to the executable | ||
if !v.Equal(c.defaultVersion) { | ||
tfExecutable = fmt.Sprintf("%s%s", tfExecutable, v.String()) | ||
} | ||
terraformCmd := exec.Command(tfExecutable, args...) | ||
|
||
// set environment variables | ||
// this is to support scripts to use the ENVIRONMENT, ATLANTIS_TERRAFORM_VERSION | ||
// and WORKSPACE variables in their scripts | ||
// append current process's environment variables | ||
// this is to prevent the $PATH variable being removed from the environment | ||
envVars := []string{ | ||
fmt.Sprintf("ENVIRONMENT=%s", env), | ||
fmt.Sprintf("ATLANTIS_TERRAFORM_VERSION=%s", v.String()), | ||
fmt.Sprintf("WORKSPACE=%s", path), | ||
} | ||
envVars = append(envVars, os.Environ()...) | ||
|
||
// append terraform executable name with args | ||
tfCmd := fmt.Sprintf("%s %s", tfExecutable, strings.Join(args, " ")) | ||
|
||
terraformCmd := exec.Command("bash", "-c", tfCmd) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think sh.... |
||
terraformCmd.Dir = path | ||
terraformCmd.Env = envVars | ||
out, err := terraformCmd.CombinedOutput() | ||
commandStr := strings.Join(terraformCmd.Args, " ") | ||
if err != nil { | ||
|
@@ -77,19 +95,19 @@ func (c *Client) RunCommandWithVersion(log *logging.SimpleLogger, path string, a | |
func (c *Client) RunInitAndEnv(log *logging.SimpleLogger, path string, env string, extraInitArgs []string, version *version.Version) ([]string, error) { | ||
var outputs []string | ||
// run terraform init | ||
output, err := c.RunCommandWithVersion(log, path, append([]string{"init", "-no-color"}, extraInitArgs...), version) | ||
output, err := c.RunCommandWithVersion(log, path, append([]string{"init", "-no-color"}, extraInitArgs...), version, env) | ||
outputs = append(outputs, output) | ||
if err != nil { | ||
return outputs, err | ||
} | ||
|
||
// run terraform env new and select | ||
output, err = c.RunCommandWithVersion(log, path, []string{"env", "select", "-no-color", env}, version) | ||
output, err = c.RunCommandWithVersion(log, path, []string{"env", "select", "-no-color", env}, version, env) | ||
outputs = append(outputs, output) | ||
if err != nil { | ||
// if terraform env select fails we will run terraform env new | ||
// to create a new environment | ||
output, err = c.RunCommandWithVersion(log, path, []string{"env", "new", "-no-color", env}, version) | ||
output, err = c.RunCommandWithVersion(log, path, []string{"env", "new", "-no-color", env}, version, env) | ||
if err != nil { | ||
return outputs, err | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should document what
v
andenv
do in the docs here