Skip to content

Commit

Permalink
Autoload only .auto.tfvars files
Browse files Browse the repository at this point in the history
  • Loading branch information
rliebz authored and apparentlymart committed Jul 6, 2017
1 parent 006744b commit 8d98fde
Show file tree
Hide file tree
Showing 31 changed files with 118 additions and 91 deletions.
8 changes: 4 additions & 4 deletions command/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ Options:
flag can be set multiple times.
-var-file=foo Set variables in the Terraform configuration from
a file. If "terraform.tfvars" is present, it will be
automatically loaded if this flag is not specified.
a file. If "terraform.tfvars" or any ".auto.tfvars"
files are present, they will be automatically loaded.
`
Expand Down Expand Up @@ -345,8 +345,8 @@ Options:
flag can be set multiple times.
-var-file=foo Set variables in the Terraform configuration from
a file. If "terraform.tfvars" is present, it will be
automatically loaded if this flag is not specified.
a file. If "terraform.tfvars" or any ".auto.tfvars"
files are present, they will be automatically loaded.
`
Expand Down
4 changes: 2 additions & 2 deletions command/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ Options:
flag can be set multiple times.
-var-file=foo Set variables in the Terraform configuration from
a file. If "terraform.tfvars" is present, it will be
automatically loaded if this flag is not specified.
a file. If "terraform.tfvars" or any ".auto.tfvars"
files are present, they will be automatically loaded.
`
Expand Down
4 changes: 2 additions & 2 deletions command/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ Options:
with the "-config" flag.
-var-file=foo Set variables in the Terraform configuration from
a file. If "terraform.tfvars" is present, it will be
automatically loaded if this flag is not specified.
a file. If "terraform.tfvars" or any ".auto.tfvars"
files are present, they will be automatically loaded.
`
Expand Down
23 changes: 17 additions & 6 deletions command/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,19 @@ func (m *Meta) process(args []string, vars bool) ([]string, error) {
return nil, err
}

err = nil
var preArgs []string

if _, err = os.Stat(DefaultVarsFilename); err == nil {
m.autoKey = "var-file-default"
preArgs = append(preArgs, "-"+m.autoKey, DefaultVarsFilename)
}

if _, err = os.Stat(DefaultVarsFilename + ".json"); err == nil {
m.autoKey = "var-file-default"
preArgs = append(preArgs, "-"+m.autoKey, DefaultVarsFilename+".json")
}

err = nil
for err != io.EOF {
var fis []os.FileInfo
fis, err = f.Readdir(128)
Expand All @@ -412,7 +423,7 @@ func (m *Meta) process(args []string, vars bool) ([]string, error) {
for _, fi := range fis {
name := fi.Name()
// Ignore directories, non-var-files, and ignored files
if fi.IsDir() || !isVarFile(name) || config.IsIgnoredFile(name) {
if fi.IsDir() || !isAutoVarFile(name) || config.IsIgnoredFile(name) {
continue
}

Expand Down Expand Up @@ -570,8 +581,8 @@ func (m *Meta) SetWorkspace(name string) error {
return nil
}

// isVarFile determines if the file ends with .tfvars or .tfvars.json
func isVarFile(path string) bool {
return strings.HasSuffix(path, ".tfvars") ||
strings.HasSuffix(path, ".tfvars.json")
// isAutoVarFile determines if the file ends with .auto.tfvars or .auto.tfvars.json
func isAutoVarFile(path string) bool {
return strings.HasSuffix(path, ".auto.tfvars") ||
strings.HasSuffix(path, ".auto.tfvars.json")
}
30 changes: 22 additions & 8 deletions command/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,25 @@ func TestMeta_process(t *testing.T) {
defer os.Chdir(cwd)

// Create two vars files
file1 := "file1.tfvars"
defaultVarsfile := "terraform.tfvars"
err = ioutil.WriteFile(
filepath.Join(d, file1),
filepath.Join(d, defaultVarsfile),
[]byte(""),
0644)
if err != nil {
t.Fatalf("err: %s", err)
}
file2 := "file2.tfvars"
fileFirstAlphabetical := "a-file.auto.tfvars"
err = ioutil.WriteFile(
filepath.Join(d, file2),
filepath.Join(d, fileFirstAlphabetical),
[]byte(""),
0644)
if err != nil {
t.Fatalf("err: %s", err)
}
fileLastAlphabetical := "z-file.auto.tfvars"
err = ioutil.WriteFile(
filepath.Join(d, fileLastAlphabetical),
[]byte(""),
0644)
if err != nil {
Expand All @@ -366,13 +374,19 @@ func TestMeta_process(t *testing.T) {
if args[0] != "-var-file-default" {
t.Fatalf("expected %q, got %q", "-var-file-default", args[0])
}
if args[1] != file1 {
t.Fatalf("expected %q, got %q", file1, args[1])
if args[1] != defaultVarsfile {
t.Fatalf("expected %q, got %q", defaultVarsfile, args[3])
}
if args[2] != "-var-file-default" {
t.Fatalf("expected %q, got %q", "-var-file-default", args[0])
}
if args[3] != file2 {
t.Fatalf("expected %q, got %q", file2, args[3])
if args[3] != fileFirstAlphabetical {
t.Fatalf("expected %q, got %q", fileFirstAlphabetical, args[1])
}
if args[4] != "-var-file-default" {
t.Fatalf("expected %q, got %q", "-var-file-default", args[0])
}
if args[5] != fileLastAlphabetical {
t.Fatalf("expected %q, got %q", fileLastAlphabetical, args[3])
}
}
4 changes: 2 additions & 2 deletions command/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ Options:
flag can be set multiple times.
-var-file=foo Set variables in the Terraform configuration from
a file. If "terraform.tfvars" is present, it will be
automatically loaded if this flag is not specified.
a file. If "terraform.tfvars" or any ".auto.tfvars"
files are present, they will be automatically loaded.
`
return strings.TrimSpace(helpText)
}
Expand Down
4 changes: 2 additions & 2 deletions command/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ Options:
flag can be set multiple times.
-var-file=foo Set variables in the Terraform configuration from
a file. If "terraform.tfvars" is present, it will be
automatically loaded if this flag is not specified.
a file. If "terraform.tfvars" or any ".auto.tfvars"
files are present, they will be automatically loaded.
-vcs=true If true (default), push will upload only files
committed to your VCS, if detected.
Expand Down
4 changes: 2 additions & 2 deletions command/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ Options:
flag can be set multiple times.
-var-file=foo Set variables in the Terraform configuration from
a file. If "terraform.tfvars" is present, it will be
automatically loaded if this flag is not specified.
a file. If "terraform.tfvars" or any ".auto.tfvars"
files are present, they will be automatically loaded.
`
return strings.TrimSpace(helpText)
Expand Down
8 changes: 4 additions & 4 deletions contrib/zsh-completion/_terraform
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ __apply() {
'-state=[(path) Path to read and save state (unless state-out is specified). Defaults to "terraform.tfstate".]' \
'-state-out=[(path) Path to write state to that is different than "-state". This can be used to preserve the old state.]' \
'-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times.]' \
'-var-file=[(path) Set variables in the Terraform configuration from a file. If "terraform.tfvars" is present, it will be automatically loaded if this flag is not specified.]'
'-var-file=[(path) Set variables in the Terraform configuration from a file. If "terraform.tfvars" or any ".auto.tfvars" files are present, they will be automatically loaded.]'
}

__destroy() {
Expand All @@ -39,7 +39,7 @@ __destroy() {
'-state=[Path to read and save state (unless state-out is specified). Defaults to "terraform.tfstate".]' \
'-state-out=[Path to write state to that is different than "-state". This can be used to preserve the old state.]' \
'-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times.]' \
'-var-file=[(path) Set variables in the Terraform configuration from a file. If "terraform.tfvars" is present, it will be automatically loaded if this flag is not specified.]'
'-var-file=[(path) Set variables in the Terraform configuration from a file. If "terraform.tfvars" or any ".auto.tfvars" files are present, they will be automatically loaded.]'
}

__get() {
Expand Down Expand Up @@ -77,7 +77,7 @@ __plan() {
'-refresh=[(true) Update state prior to checking for differences.]' \
'-state=[(statefile) Path to a Terraform state file to use to look up Terraform-managed resources. By default it will use the state "terraform.tfstate" if it exists.]' \
'-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times.]' \
'-var-file=[(path) Set variables in the Terraform configuration from a file. If "terraform.tfvars" is present, it will be automatically loaded if this flag is not specified.]'
'-var-file=[(path) Set variables in the Terraform configuration from a file. If "terraform.tfvars" or any ".auto.tfvars" files are present, they will be automatically loaded.]'
}

__push() {
Expand All @@ -93,7 +93,7 @@ __refresh() {
'-state=[(path) Path to read and save state (unless state-out is specified). Defaults to "terraform.tfstate".]' \
'-state-out=[(path) Path to write state to that is different than "-state". This can be used to preserve the old state.]' \
'-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times.]' \
'-var-file=[(path) Set variables in the Terraform configuration from a file. If "terraform.tfvars" is present, it will be automatically loaded if this flag is not specified.]'
'-var-file=[(path) Set variables in the Terraform configuration from a file. If "terraform.tfvars" or any ".auto.tfvars" files are present, they will be automatically loaded.]'
}

__taint() {
Expand Down
4 changes: 2 additions & 2 deletions examples/azure-2-vms-loadbalancer-lbrules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ This data is outputted when `terraform apply` is called, and can be queried usin
Azure requires that an application is added to Azure Active Directory to generate the `client_id`, `client_secret`, and `tenant_id` needed by Terraform (`subscription_id` can be recovered from your Azure account details). Please go [here](https://www.terraform.io/docs/providers/azurerm/) for full instructions on how to create this to populate your `provider.tf` file.

## terraform.tfvars
If a `terraform.tfvars` file is present in the current directory, Terraform automatically loads it to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use `-var-file` to load it.
If a `terraform.tfvars` or any `.auto.tfvars` files are present in the current directory, Terraform automatically loads them to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use the `-var-file` flag or the `.auto.tfvars` extension to load it.

If you are committing this template to source control, please insure that you add this file to your .gitignore file.

## variables.tf
The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template.
The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template.
4 changes: 2 additions & 2 deletions examples/azure-cdn-with-storage-account/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ This data is outputted when `terraform apply` is called, and can be queried usin
Azure requires that an application is added to Azure Active Directory to generate the `client_id`, `client_secret`, and `tenant_id` needed by Terraform (`subscription_id` can be recovered from your Azure account details). Please go [here](https://www.terraform.io/docs/providers/azurerm/) for full instructions on how to create this to populate your `provider.tf` file.

## terraform.tfvars
If a `terraform.tfvars` file is present in the current directory, Terraform automatically loads it to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use `-var-file` to load it.
If a `terraform.tfvars` or any `.auto.tfvars` files are present in the current directory, Terraform automatically loads them to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use the `-var-file` flag or the `.auto.tfvars` extension to load it.

If you are committing this template to source control, please insure that you add this file to your `.gitignore` file.

## variables.tf
The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template.

![graph](/examples/azure-cdn-with-storage-account/graph.png)
![graph](/examples/azure-cdn-with-storage-account/graph.png)
4 changes: 2 additions & 2 deletions examples/azure-encrypt-running-linux-vm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ You may leave the provider block in the `main.tf`, as it is in this template, or
Azure requires that an application is added to Azure Active Directory to generate the `client_id`, `client_secret`, and `tenant_id` needed by Terraform (`subscription_id` can be recovered from your Azure account details). Please go [here](https://www.terraform.io/docs/providers/azurerm/) for full instructions on how to create this to populate your `provider.tf` file.

## terraform.tfvars
If a `terraform.tfvars` file is present in the current directory, Terraform automatically loads it to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use `-var-file` to load it.
If a `terraform.tfvars` or any `.auto.tfvars` files are present in the current directory, Terraform automatically loads them to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use the `-var-file` flag or the `.auto.tfvars` extension to load it.

If you are committing this template to source control, please insure that you add this file to your .gitignore file.

## variables.tf
The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template.

![graph](/examples/azure-encrypt-running-linux-vm/graph.png)
![graph](/examples/azure-encrypt-running-linux-vm/graph.png)
4 changes: 2 additions & 2 deletions examples/azure-search-create/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ You may leave the provider block in the `main.tf`, as it is in this template, or
Azure requires that an application is added to Azure Active Directory to generate the `client_id`, `client_secret`, and `tenant_id` needed by Terraform (`subscription_id` can be recovered from your Azure account details). Please go [here](https://www.terraform.io/docs/providers/azurerm/) for full instructions on how to create this to populate your `provider.tf` file.

## terraform.tfvars
If a `terraform.tfvars` file is present in the current directory, Terraform automatically loads it to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use `-var-file` to load it.
If a `terraform.tfvars` or any `.auto.tfvars` files are present in the current directory, Terraform automatically loads them to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use the `-var-file` flag or the `.auto.tfvars` extension to load it.

If you are committing this template to source control, please insure that you add this file to your `.gitignore` file.

## variables.tf
The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template.

![graph](/examples/azure-search-create/graph.png)
![graph](/examples/azure-search-create/graph.png)
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ This data is outputted when `terraform apply` is called, and can be queried usin
Azure requires that an application is added to Azure Active Directory to generate the `client_id`, `client_secret`, and `tenant_id` needed by Terraform (`subscription_id` can be recovered from your Azure account details). Please go [here](https://www.terraform.io/docs/providers/azurerm/) for full instructions on how to create this to populate your `provider.tf` file.

## terraform.tfvars
If a `terraform.tfvars` file is present in the current directory, Terraform automatically loads it to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use `-var-file` to load it.
If a `terraform.tfvars` or any `.auto.tfvars` files are present in the current directory, Terraform automatically loads them to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use the `-var-file` flag or the `.auto.tfvars` extension to load it.

If you are committing this template to source control, please insure that you add this file to your `.gitignore` file.

## variables.tf
The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template.

![graph](/examples/azure-servicebus-create-topic-and-subscription/graph.png)
![graph](/examples/azure-servicebus-create-topic-and-subscription/graph.png)
2 changes: 1 addition & 1 deletion examples/azure-spark-and-cassandra-on-centos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ This data is outputted when `terraform apply` is called, and can be queried usin
Azure requires that an application is added to Azure Active Directory to generate the `client_id`, `client_secret`, and `tenant_id` needed by Terraform (`subscription_id` can be recovered from your Azure account details). Please go [here](https://www.terraform.io/docs/providers/azurerm/) for full instructions on how to create this to populate your `provider.tf` file.

## terraform.tfvars
If a `terraform.tfvars` file is present in the current directory, Terraform automatically loads it to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use `-var-file` to load it.
If a `terraform.tfvars` or any `.auto.tfvars` files are present in the current directory, Terraform automatically loads them to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use the `-var-file` flag or the `.auto.tfvars` extension to load it.

If you are committing this template to source control, please insure that you add this file to your `.gitignore` file.

Expand Down
4 changes: 2 additions & 2 deletions examples/azure-sql-database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ This data is outputted when `terraform apply` is called, and can be queried usin
Azure requires that an application is added to Azure Active Directory to generate the `client_id`, `client_secret`, and `tenant_id` needed by Terraform (`subscription_id` can be recovered from your Azure account details). Please go [here](https://www.terraform.io/docs/providers/azurerm/) for full instructions on how to create this to populate your `provider.tf` file.

## terraform.tfvars
If a `terraform.tfvars` file is present in the current directory, Terraform automatically loads it to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use `-var-file` to load it.
If a `terraform.tfvars` or any `.auto.tfvars` files are present in the current directory, Terraform automatically loads them to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use the `-var-file` flag or the `.auto.tfvars` extension to load it.

## variables.tf
The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template.

![graph](/examples/azure-sql-database/graph.png)
![graph](/examples/azure-sql-database/graph.png)
Loading

0 comments on commit 8d98fde

Please sign in to comment.