mirror of
https://github.com/hashicorp/terraform.git
synced 2026-06-08 16:35:25 -04:00
command: Add vars to providers command
This commit is contained in:
parent
c9e1b3a47b
commit
1cc8345926
3 changed files with 89 additions and 5 deletions
|
|
@ -9,6 +9,9 @@ import "github.com/hashicorp/terraform/internal/tfdiags"
|
|||
type Providers struct {
|
||||
// TestsDirectory is the directory containing Terraform test files.
|
||||
TestsDirectory string
|
||||
|
||||
// Vars are the variable-related flags (-var, -var-file).
|
||||
Vars *Vars
|
||||
}
|
||||
|
||||
// ParseProviders processes CLI arguments, returning a Providers value and
|
||||
|
|
@ -16,9 +19,11 @@ type Providers struct {
|
|||
// representing the best effort interpretation of the arguments.
|
||||
func ParseProviders(args []string) (*Providers, tfdiags.Diagnostics) {
|
||||
var diags tfdiags.Diagnostics
|
||||
providers := &Providers{}
|
||||
providers := &Providers{
|
||||
Vars: &Vars{},
|
||||
}
|
||||
|
||||
cmdFlags := defaultFlagSet("providers")
|
||||
cmdFlags := extendedFlagSet("providers", nil, nil, providers.Vars)
|
||||
cmdFlags.StringVar(&providers.TestsDirectory, "test-directory", "tests", "test-directory")
|
||||
|
||||
if err := cmdFlags.Parse(args); err != nil {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"github.com/hashicorp/terraform/internal/tfdiags"
|
||||
)
|
||||
|
||||
|
|
@ -19,23 +20,27 @@ func TestParseProviders_valid(t *testing.T) {
|
|||
nil,
|
||||
&Providers{
|
||||
TestsDirectory: "tests",
|
||||
Vars: &Vars{},
|
||||
},
|
||||
},
|
||||
"test directory": {
|
||||
[]string{"-test-directory=integration-tests"},
|
||||
&Providers{
|
||||
TestsDirectory: "integration-tests",
|
||||
Vars: &Vars{},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
cmpOpts := cmpopts.IgnoreUnexported(Vars{})
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, diags := ParseProviders(tc.args)
|
||||
if len(diags) > 0 {
|
||||
t.Fatalf("unexpected diags: %v", diags)
|
||||
}
|
||||
if diff := cmp.Diff(tc.want, got); diff != "" {
|
||||
if diff := cmp.Diff(tc.want, got, cmpOpts); diff != "" {
|
||||
t.Fatalf("unexpected result\n%s", diff)
|
||||
}
|
||||
})
|
||||
|
|
@ -52,6 +57,7 @@ func TestParseProviders_invalid(t *testing.T) {
|
|||
[]string{"-wat"},
|
||||
&Providers{
|
||||
TestsDirectory: "tests",
|
||||
Vars: &Vars{},
|
||||
},
|
||||
tfdiags.Diagnostics{
|
||||
tfdiags.Sourceless(
|
||||
|
|
@ -65,6 +71,7 @@ func TestParseProviders_invalid(t *testing.T) {
|
|||
[]string{"foo"},
|
||||
&Providers{
|
||||
TestsDirectory: "tests",
|
||||
Vars: &Vars{},
|
||||
},
|
||||
tfdiags.Diagnostics{
|
||||
tfdiags.Sourceless(
|
||||
|
|
@ -76,13 +83,59 @@ func TestParseProviders_invalid(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
cmpOpts := cmpopts.IgnoreUnexported(Vars{})
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, gotDiags := ParseProviders(tc.args)
|
||||
if diff := cmp.Diff(tc.want, got); diff != "" {
|
||||
if diff := cmp.Diff(tc.want, got, cmpOpts); diff != "" {
|
||||
t.Fatalf("unexpected result\n%s", diff)
|
||||
}
|
||||
tfdiags.AssertDiagnosticsMatch(t, gotDiags, tc.wantDiags)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseProviders_vars(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
want []FlagNameValue
|
||||
}{
|
||||
"var": {
|
||||
args: []string{"-var", "foo=bar"},
|
||||
want: []FlagNameValue{
|
||||
{Name: "-var", Value: "foo=bar"},
|
||||
},
|
||||
},
|
||||
"var-file": {
|
||||
args: []string{"-var-file", "cool.tfvars"},
|
||||
want: []FlagNameValue{
|
||||
{Name: "-var-file", Value: "cool.tfvars"},
|
||||
},
|
||||
},
|
||||
"both": {
|
||||
args: []string{
|
||||
"-var", "foo=bar",
|
||||
"-var-file", "cool.tfvars",
|
||||
"-var", "boop=beep",
|
||||
},
|
||||
want: []FlagNameValue{
|
||||
{Name: "-var", Value: "foo=bar"},
|
||||
{Name: "-var-file", Value: "cool.tfvars"},
|
||||
{Name: "-var", Value: "boop=beep"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, diags := ParseProviders(tc.args)
|
||||
if len(diags) > 0 {
|
||||
t.Fatalf("unexpected diags: %v", diags)
|
||||
}
|
||||
if vars := got.Vars.All(); !cmp.Equal(vars, tc.want) {
|
||||
t.Fatalf("unexpected vars: %#v", vars)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,6 +43,23 @@ func (c *ProvidersCommand) Run(args []string) int {
|
|||
return 1
|
||||
}
|
||||
|
||||
loader, err := c.initConfigLoader()
|
||||
if err != nil {
|
||||
diags = diags.Append(err)
|
||||
c.showDiagnostics(diags)
|
||||
return 1
|
||||
}
|
||||
|
||||
var varDiags tfdiags.Diagnostics
|
||||
c.VariableValues, varDiags = parsedArgs.Vars.CollectValues(func(filename string, src []byte) {
|
||||
loader.Parser().ForceFileSource(filename, src)
|
||||
})
|
||||
diags = diags.Append(varDiags)
|
||||
if diags.HasErrors() {
|
||||
c.showDiagnostics(diags)
|
||||
return 1
|
||||
}
|
||||
|
||||
empty, err := configs.IsEmptyDir(configPath, parsedArgs.TestsDirectory)
|
||||
if err != nil {
|
||||
diags = diags.Append(tfdiags.Sourceless(
|
||||
|
|
@ -178,5 +195,14 @@ Usage: terraform [global options] providers [options] [DIR]
|
|||
|
||||
Options:
|
||||
|
||||
-test-directory=path Set the Terraform test directory, defaults to "tests".
|
||||
-test-directory=path Set the Terraform test directory, defaults to "tests".
|
||||
|
||||
-var 'foo=bar' Set a value for one of the input variables in the root
|
||||
module of the configuration. Use this option more than
|
||||
once to set more than one variable.
|
||||
|
||||
-var-file=filename Load variable values from the given file, in addition
|
||||
to the default files terraform.tfvars and *.auto.tfvars.
|
||||
Use this option more than once to include more than one
|
||||
variables file.
|
||||
`
|
||||
|
|
|
|||
Loading…
Reference in a new issue