-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support setting variables on applications.
- Loading branch information
1 parent
2a17c0c
commit f8982d7
Showing
6 changed files
with
195 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:feature | ||
waypoint: Support setting variables when creating apps. | ||
``` |
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
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package internal | ||
|
||
import ( | ||
"os" | ||
"sort" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclsimple" | ||
"github.com/hashicorp/hcp-sdk-go/clients/cloud-waypoint-service/preview/2023-08-18/models" | ||
) | ||
|
||
func ParseInputVariablesFile(path string) ([]*models.HashicorpCloudWaypointInputVariable, error) { | ||
input, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return parseInputVariables(path, input) | ||
} | ||
|
||
func parseInputVariables(filename string, input []byte) ([]*models.HashicorpCloudWaypointInputVariable, error) { | ||
var hc hclInputVariablesFile | ||
var ctx hcl.EvalContext | ||
if err := hclsimple.Decode(filename, input, &ctx, &hc); err != nil { | ||
return nil, err | ||
} | ||
|
||
var variables []*models.HashicorpCloudWaypointInputVariable | ||
if len(hc.Variables) > 0 { | ||
for k, v := range hc.Variables { | ||
variables = append(variables, &models.HashicorpCloudWaypointInputVariable{ | ||
Name: k, | ||
Value: v, | ||
}) | ||
} | ||
} | ||
|
||
sort.Slice(variables, func(i, j int) bool { | ||
return variables[i].Name < variables[j].Name | ||
}) | ||
return variables, nil | ||
} | ||
|
||
type hclInputVariablesFile struct { | ||
Variables map[string]string `hcl:"variables,remain"` | ||
} |
74 changes: 74 additions & 0 deletions
74
internal/commands/waypoint/internal/input_variables_test.go
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package internal | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_InputVariablesFile(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("can parse variables", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
r := require.New(t) | ||
|
||
hcl := ` | ||
key="value" | ||
key2="value2" | ||
` | ||
|
||
inputVars, err := parseInputVariables("blah.hcl", []byte(hcl)) | ||
r.NoError(err) | ||
r.Equal(2, len(inputVars)) | ||
r.Equal("key", inputVars[0].Name) | ||
r.Equal("value", inputVars[0].Value) | ||
r.Equal("key2", inputVars[1].Name) | ||
r.Equal("value2", inputVars[1].Value) | ||
}) | ||
|
||
t.Run("handles empty file", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
r := require.New(t) | ||
|
||
hcl := `` | ||
|
||
inputVars, err := parseInputVariables("blah.hcl", []byte(hcl)) | ||
r.NoError(err) | ||
r.Equal(0, len(inputVars)) | ||
}) | ||
|
||
t.Run("handles empty values", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
r := require.New(t) | ||
|
||
hcl := ` | ||
key="" | ||
key2="" | ||
` | ||
|
||
inputVars, err := parseInputVariables("blah.hcl", []byte(hcl)) | ||
r.NoError(err) | ||
r.Equal(2, len(inputVars)) | ||
r.Equal("key", inputVars[0].Name) | ||
r.Equal("", inputVars[0].Value) | ||
r.Equal("key2", inputVars[1].Name) | ||
r.Equal("", inputVars[1].Value) | ||
}) | ||
|
||
t.Run("fail to parse", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
r := require.New(t) | ||
|
||
hcl := ` | ||
key====== | ||
` | ||
|
||
_, err := parseInputVariables("blah.hcl", []byte(hcl)) | ||
r.Error(err) | ||
}) | ||
} |