Skip to content

Commit

Permalink
expose CliFlagAssignedVariables builder functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lonegunmanb committed May 17, 2024
1 parent d9afff3 commit 182f297
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
25 changes: 6 additions & 19 deletions base_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,7 @@ func (s *baseConfigSuite) TestReadCliAssignedVariables() {
{
desc: "CliFlagAssignedVariableFile-json",
cliFlags: []CliFlagAssignedVariables{
CliFlagAssignedVariableFile{
varFileName: "/test.tfvars.json",
},
NewCliFlagAssignedVariableFile("/test.tfvars.json"),
},
files: map[string]string{
"/test.tfvars.json": `{
Expand All @@ -384,9 +382,7 @@ func (s *baseConfigSuite) TestReadCliAssignedVariables() {
{
desc: "CliFlagAssignedVariableFile-json",
cliFlags: []CliFlagAssignedVariables{
CliFlagAssignedVariableFile{
varFileName: "/test.tfvars.json",
},
NewCliFlagAssignedVariableFile("/test.tfvars.json"),
},
files: map[string]string{
"/test.tfvars.json": `{
Expand All @@ -400,13 +396,8 @@ func (s *baseConfigSuite) TestReadCliAssignedVariables() {
{
desc: "CliFlagAssignedVariableFile-precedence-0",
cliFlags: []CliFlagAssignedVariables{
CliFlagAssignedVariable{
varName: "string_value",
rawValue: "world",
},
CliFlagAssignedVariableFile{
varFileName: "/test.tfvars.json",
},
NewCliFlagAssignedVariable("string_value", "world"),
NewCliFlagAssignedVariableFile("/test.tfvars.json"),
},
files: map[string]string{
"/test.tfvars.json": `{
Expand All @@ -420,12 +411,8 @@ func (s *baseConfigSuite) TestReadCliAssignedVariables() {
{
desc: "CliFlagAssignedVariableFile-precedence-1",
cliFlags: []CliFlagAssignedVariables{
CliFlagAssignedVariableFile{
varFileName: "/test.tfvars.json",
},
CliFlagAssignedVariableFile{
varFileName: "/test.tfvars",
},
NewCliFlagAssignedVariableFile("/test.tfvars.json"),
NewCliFlagAssignedVariableFile("/test.tfvars"),
},
files: map[string]string{
"/test.tfvars.json": `{
Expand Down
13 changes: 13 additions & 0 deletions cli_flag_assigned_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ type CliFlagAssignedVariable struct {
rawValue string
}

func NewCliFlagAssignedVariable(varName, rawValue string) CliFlagAssignedVariable {
return CliFlagAssignedVariable{
varName: varName,
rawValue: rawValue,
}
}

func (v CliFlagAssignedVariable) Variables(c *BaseConfig) (map[string]VariableValueRead, error) {
variableBlocks := Blocks[*VariableBlock](c)
variables := make(map[string]*VariableBlock)
Expand All @@ -40,6 +47,12 @@ type CliFlagAssignedVariableFile struct {
varFileName string
}

func NewCliFlagAssignedVariableFile(varFileName string) CliFlagAssignedVariableFile {
return CliFlagAssignedVariableFile{
varFileName: varFileName,
}
}

func (v CliFlagAssignedVariableFile) Variables(c *BaseConfig) (map[string]VariableValueRead, error) {
exist, err := afero.Exists(configFs, v.varFileName)
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ func (s *variableSuite) TestReadVariableValue_ReadDefaultIfNotSet() {
{
desc: "CliFlagAssignedVariableFile-hcl",
cliFlags: []CliFlagAssignedVariables{
CliFlagAssignedVariableFile{
varFileName: "/test.tfvars",
},
NewCliFlagAssignedVariableFile("/test.tfvars"),
},
files: map[string]string{
"/test.tfvars": `string_value = "hello"`,
Expand Down

0 comments on commit 182f297

Please sign in to comment.