Skip to content

Commit 7028ff7

Browse files
feat(codersdk): export template variable parser (coder#13984)
1 parent 177c7d3 commit 7028ff7

File tree

4 files changed

+28
-31
lines changed

4 files changed

+28
-31
lines changed

cli/templatecreate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (r *RootCmd) templateCreate() *serpent.Command {
9797

9898
var varsFiles []string
9999
if !uploadFlags.stdin() {
100-
varsFiles, err = DiscoverVarsFiles(uploadFlags.directory)
100+
varsFiles, err = codersdk.DiscoverVarsFiles(uploadFlags.directory)
101101
if err != nil {
102102
return err
103103
}
@@ -118,7 +118,7 @@ func (r *RootCmd) templateCreate() *serpent.Command {
118118
return err
119119
}
120120

121-
userVariableValues, err := ParseUserVariableValues(
121+
userVariableValues, err := codersdk.ParseUserVariableValues(
122122
varsFiles,
123123
variablesFile,
124124
commandLineVariables)

cli/templatepush.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (r *RootCmd) templatePush() *serpent.Command {
8181

8282
var varsFiles []string
8383
if !uploadFlags.stdin() {
84-
varsFiles, err = DiscoverVarsFiles(uploadFlags.directory)
84+
varsFiles, err = codersdk.DiscoverVarsFiles(uploadFlags.directory)
8585
if err != nil {
8686
return err
8787
}
@@ -111,7 +111,7 @@ func (r *RootCmd) templatePush() *serpent.Command {
111111
inv.Logger.Info(inv.Context(), "reusing existing provisioner tags", "tags", tags)
112112
}
113113

114-
userVariableValues, err := ParseUserVariableValues(
114+
userVariableValues, err := codersdk.ParseUserVariableValues(
115115
varsFiles,
116116
variablesFile,
117117
commandLineVariables)

cli/templatevariables.go renamed to codersdk/templatevariables.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cli
1+
package codersdk
22

33
import (
44
"encoding/json"
@@ -13,8 +13,6 @@ import (
1313

1414
"github.com/hashicorp/hcl/v2/hclparse"
1515
"github.com/zclconf/go-cty/cty"
16-
17-
"github.com/coder/coder/v2/codersdk"
1816
)
1917

2018
/**
@@ -54,7 +52,7 @@ func DiscoverVarsFiles(workDir string) ([]string, error) {
5452
return found, nil
5553
}
5654

57-
func ParseUserVariableValues(varsFiles []string, variablesFile string, commandLineVariables []string) ([]codersdk.VariableValue, error) {
55+
func ParseUserVariableValues(varsFiles []string, variablesFile string, commandLineVariables []string) ([]VariableValue, error) {
5856
fromVars, err := parseVariableValuesFromVarsFiles(varsFiles)
5957
if err != nil {
6058
return nil, err
@@ -73,15 +71,15 @@ func ParseUserVariableValues(varsFiles []string, variablesFile string, commandLi
7371
return combineVariableValues(fromVars, fromFile, fromCommandLine), nil
7472
}
7573

76-
func parseVariableValuesFromVarsFiles(varsFiles []string) ([]codersdk.VariableValue, error) {
77-
var parsed []codersdk.VariableValue
74+
func parseVariableValuesFromVarsFiles(varsFiles []string) ([]VariableValue, error) {
75+
var parsed []VariableValue
7876
for _, varsFile := range varsFiles {
7977
content, err := os.ReadFile(varsFile)
8078
if err != nil {
8179
return nil, err
8280
}
8381

84-
var t []codersdk.VariableValue
82+
var t []VariableValue
8583
ext := filepath.Ext(varsFile)
8684
switch ext {
8785
case ".tfvars":
@@ -103,7 +101,7 @@ func parseVariableValuesFromVarsFiles(varsFiles []string) ([]codersdk.VariableVa
103101
return parsed, nil
104102
}
105103

106-
func parseVariableValuesFromHCL(content []byte) ([]codersdk.VariableValue, error) {
104+
func parseVariableValuesFromHCL(content []byte) ([]VariableValue, error) {
107105
parser := hclparse.NewParser()
108106
hclFile, diags := parser.ParseHCL(content, "file.hcl")
109107
if diags.HasErrors() {
@@ -159,7 +157,7 @@ func parseVariableValuesFromHCL(content []byte) ([]codersdk.VariableValue, error
159157
// parseVariableValuesFromJSON converts the .tfvars.json content into template variables.
160158
// The function visits only root-level properties as template variables do not support nested
161159
// structures.
162-
func parseVariableValuesFromJSON(content []byte) ([]codersdk.VariableValue, error) {
160+
func parseVariableValuesFromJSON(content []byte) ([]VariableValue, error) {
163161
var data map[string]interface{}
164162
err := json.Unmarshal(content, &data)
165163
if err != nil {
@@ -183,10 +181,10 @@ func parseVariableValuesFromJSON(content []byte) ([]codersdk.VariableValue, erro
183181
return convertMapIntoVariableValues(stringData), nil
184182
}
185183

186-
func convertMapIntoVariableValues(m map[string]string) []codersdk.VariableValue {
187-
var parsed []codersdk.VariableValue
184+
func convertMapIntoVariableValues(m map[string]string) []VariableValue {
185+
var parsed []VariableValue
188186
for key, value := range m {
189-
parsed = append(parsed, codersdk.VariableValue{
187+
parsed = append(parsed, VariableValue{
190188
Name: key,
191189
Value: value,
192190
})
@@ -197,8 +195,8 @@ func convertMapIntoVariableValues(m map[string]string) []codersdk.VariableValue
197195
return parsed
198196
}
199197

200-
func parseVariableValuesFromFile(variablesFile string) ([]codersdk.VariableValue, error) {
201-
var values []codersdk.VariableValue
198+
func parseVariableValuesFromFile(variablesFile string) ([]VariableValue, error) {
199+
var values []VariableValue
202200
if variablesFile == "" {
203201
return values, nil
204202
}
@@ -209,7 +207,7 @@ func parseVariableValuesFromFile(variablesFile string) ([]codersdk.VariableValue
209207
}
210208

211209
for name, value := range variablesMap {
212-
values = append(values, codersdk.VariableValue{
210+
values = append(values, VariableValue{
213211
Name: name,
214212
Value: value,
215213
})
@@ -237,23 +235,23 @@ func createVariablesMapFromFile(variablesFile string) (map[string]string, error)
237235
return variablesMap, nil
238236
}
239237

240-
func parseVariableValuesFromCommandLine(variables []string) ([]codersdk.VariableValue, error) {
241-
var values []codersdk.VariableValue
238+
func parseVariableValuesFromCommandLine(variables []string) ([]VariableValue, error) {
239+
var values []VariableValue
242240
for _, keyValue := range variables {
243241
split := strings.SplitN(keyValue, "=", 2)
244242
if len(split) < 2 {
245243
return nil, xerrors.Errorf("format key=value expected, but got %s", keyValue)
246244
}
247245

248-
values = append(values, codersdk.VariableValue{
246+
values = append(values, VariableValue{
249247
Name: split[0],
250248
Value: split[1],
251249
})
252250
}
253251
return values, nil
254252
}
255253

256-
func combineVariableValues(valuesSets ...[]codersdk.VariableValue) []codersdk.VariableValue {
254+
func combineVariableValues(valuesSets ...[]VariableValue) []VariableValue {
257255
combinedValues := make(map[string]string)
258256

259257
for _, values := range valuesSets {
@@ -262,9 +260,9 @@ func combineVariableValues(valuesSets ...[]codersdk.VariableValue) []codersdk.Va
262260
}
263261
}
264262

265-
var result []codersdk.VariableValue
263+
var result []VariableValue
266264
for name, value := range combinedValues {
267-
result = append(result, codersdk.VariableValue{Name: name, Value: value})
265+
result = append(result, VariableValue{Name: name, Value: value})
268266
}
269267

270268
sort.Slice(result, func(i, j int) bool {

cli/templatevariables_test.go renamed to codersdk/templatevariables_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cli_test
1+
package codersdk_test
22

33
import (
44
"os"
@@ -7,7 +7,6 @@ import (
77

88
"github.com/stretchr/testify/require"
99

10-
"github.com/coder/coder/v2/cli"
1110
"github.com/coder/coder/v2/codersdk"
1211
)
1312

@@ -47,7 +46,7 @@ func TestDiscoverVarsFiles(t *testing.T) {
4746
}
4847

4948
// When
50-
found, err := cli.DiscoverVarsFiles(tempDir)
49+
found, err := codersdk.DiscoverVarsFiles(tempDir)
5150
require.NoError(t, err)
5251

5352
// Then
@@ -97,7 +96,7 @@ go_image = ["1.19","1.20","1.21"]`
9796
require.NoError(t, err)
9897

9998
// When
100-
actual, err := cli.ParseUserVariableValues([]string{
99+
actual, err := codersdk.ParseUserVariableValues([]string{
101100
filepath.Join(tempDir, hclFilename1),
102101
filepath.Join(tempDir, hclFilename2),
103102
filepath.Join(tempDir, jsonFilename3),
@@ -136,7 +135,7 @@ func TestParseVariableValuesFromVarsFiles_InvalidJSON(t *testing.T) {
136135
require.NoError(t, err)
137136

138137
// When
139-
actual, err := cli.ParseUserVariableValues([]string{
138+
actual, err := codersdk.ParseUserVariableValues([]string{
140139
filepath.Join(tempDir, jsonFilename),
141140
}, "", nil)
142141

@@ -167,7 +166,7 @@ cores: 2`
167166
require.NoError(t, err)
168167

169168
// When
170-
actual, err := cli.ParseUserVariableValues([]string{
169+
actual, err := codersdk.ParseUserVariableValues([]string{
171170
filepath.Join(tempDir, hclFilename),
172171
}, "", nil)
173172

0 commit comments

Comments
 (0)