Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7028ff7

Browse files
feat(codersdk): export template variable parser (#13984)
1 parent177c7d3 commit7028ff7

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
varvarsFiles []string
9999
if!uploadFlags.stdin() {
100-
varsFiles,err=DiscoverVarsFiles(uploadFlags.directory)
100+
varsFiles,err=codersdk.DiscoverVarsFiles(uploadFlags.directory)
101101
iferr!=nil {
102102
returnerr
103103
}
@@ -118,7 +118,7 @@ func (r *RootCmd) templateCreate() *serpent.Command {
118118
returnerr
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
varvarsFiles []string
8383
if!uploadFlags.stdin() {
84-
varsFiles,err=DiscoverVarsFiles(uploadFlags.directory)
84+
varsFiles,err=codersdk.DiscoverVarsFiles(uploadFlags.directory)
8585
iferr!=nil {
8686
returnerr
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.gorenamed to‎codersdk/templatevariables.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
packagecli
1+
packagecodersdk
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
returnfound,nil
5553
}
5654

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

76-
funcparseVariableValuesFromVarsFiles(varsFiles []string) ([]codersdk.VariableValue,error) {
77-
varparsed []codersdk.VariableValue
74+
funcparseVariableValuesFromVarsFiles(varsFiles []string) ([]VariableValue,error) {
75+
varparsed []VariableValue
7876
for_,varsFile:=rangevarsFiles {
7977
content,err:=os.ReadFile(varsFile)
8078
iferr!=nil {
8179
returnnil,err
8280
}
8381

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

106-
funcparseVariableValuesFromHCL(content []byte) ([]codersdk.VariableValue,error) {
104+
funcparseVariableValuesFromHCL(content []byte) ([]VariableValue,error) {
107105
parser:=hclparse.NewParser()
108106
hclFile,diags:=parser.ParseHCL(content,"file.hcl")
109107
ifdiags.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-
funcparseVariableValuesFromJSON(content []byte) ([]codersdk.VariableValue,error) {
160+
funcparseVariableValuesFromJSON(content []byte) ([]VariableValue,error) {
163161
vardatamap[string]interface{}
164162
err:=json.Unmarshal(content,&data)
165163
iferr!=nil {
@@ -183,10 +181,10 @@ func parseVariableValuesFromJSON(content []byte) ([]codersdk.VariableValue, erro
183181
returnconvertMapIntoVariableValues(stringData),nil
184182
}
185183

186-
funcconvertMapIntoVariableValues(mmap[string]string) []codersdk.VariableValue {
187-
varparsed []codersdk.VariableValue
184+
funcconvertMapIntoVariableValues(mmap[string]string) []VariableValue {
185+
varparsed []VariableValue
188186
forkey,value:=rangem {
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
returnparsed
198196
}
199197

200-
funcparseVariableValuesFromFile(variablesFilestring) ([]codersdk.VariableValue,error) {
201-
varvalues []codersdk.VariableValue
198+
funcparseVariableValuesFromFile(variablesFilestring) ([]VariableValue,error) {
199+
varvalues []VariableValue
202200
ifvariablesFile=="" {
203201
returnvalues,nil
204202
}
@@ -209,7 +207,7 @@ func parseVariableValuesFromFile(variablesFile string) ([]codersdk.VariableValue
209207
}
210208

211209
forname,value:=rangevariablesMap {
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
returnvariablesMap,nil
238236
}
239237

240-
funcparseVariableValuesFromCommandLine(variables []string) ([]codersdk.VariableValue,error) {
241-
varvalues []codersdk.VariableValue
238+
funcparseVariableValuesFromCommandLine(variables []string) ([]VariableValue,error) {
239+
varvalues []VariableValue
242240
for_,keyValue:=rangevariables {
243241
split:=strings.SplitN(keyValue,"=",2)
244242
iflen(split)<2 {
245243
returnnil,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
returnvalues,nil
254252
}
255253

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

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

265-
varresult []codersdk.VariableValue
263+
varresult []VariableValue
266264
forname,value:=rangecombinedValues {
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,jint)bool {

‎cli/templatevariables_test.gorenamed to‎codersdk/templatevariables_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
packagecli_test
1+
packagecodersdk_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)

[8]ページ先頭

©2009-2025 Movatter.jp