Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(codersdk): export template variable parser #13984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cli/templatecreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (r *RootCmd) templateCreate() *serpent.Command {

var varsFiles []string
if !uploadFlags.stdin() {
varsFiles, err = DiscoverVarsFiles(uploadFlags.directory)
varsFiles, err = codersdk.DiscoverVarsFiles(uploadFlags.directory)
if err != nil {
return err
}
Expand All @@ -118,7 +118,7 @@ func (r *RootCmd) templateCreate() *serpent.Command {
return err
}

userVariableValues, err := ParseUserVariableValues(
userVariableValues, err := codersdk.ParseUserVariableValues(
varsFiles,
variablesFile,
commandLineVariables)
Expand Down
4 changes: 2 additions & 2 deletions cli/templatepush.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (r *RootCmd) templatePush() *serpent.Command {

var varsFiles []string
if !uploadFlags.stdin() {
varsFiles, err = DiscoverVarsFiles(uploadFlags.directory)
varsFiles, err = codersdk.DiscoverVarsFiles(uploadFlags.directory)
if err != nil {
return err
}
Expand Down Expand Up @@ -111,7 +111,7 @@ func (r *RootCmd) templatePush() *serpent.Command {
inv.Logger.Info(inv.Context(), "reusing existing provisioner tags", "tags", tags)
}

userVariableValues, err := ParseUserVariableValues(
userVariableValues, err := codersdk.ParseUserVariableValues(
varsFiles,
variablesFile,
commandLineVariables)
Expand Down
40 changes: 19 additions & 21 deletions cli/templatevariables.go → codersdk/templatevariables.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cli
package codersdk

import (
"encoding/json"
Expand All @@ -13,8 +13,6 @@ import (

"github.com/hashicorp/hcl/v2/hclparse"
"github.com/zclconf/go-cty/cty"

"github.com/coder/coder/v2/codersdk"
)

/**
Expand Down Expand Up @@ -54,7 +52,7 @@ func DiscoverVarsFiles(workDir string) ([]string, error) {
return found, nil
}

func ParseUserVariableValues(varsFiles []string, variablesFile string, commandLineVariables []string) ([]codersdk.VariableValue, error) {
func ParseUserVariableValues(varsFiles []string, variablesFile string, commandLineVariables []string) ([]VariableValue, error) {
fromVars, err := parseVariableValuesFromVarsFiles(varsFiles)
if err != nil {
return nil, err
Expand All @@ -73,15 +71,15 @@ func ParseUserVariableValues(varsFiles []string, variablesFile string, commandLi
return combineVariableValues(fromVars, fromFile, fromCommandLine), nil
}

func parseVariableValuesFromVarsFiles(varsFiles []string) ([]codersdk.VariableValue, error) {
var parsed []codersdk.VariableValue
func parseVariableValuesFromVarsFiles(varsFiles []string) ([]VariableValue, error) {
var parsed []VariableValue
for _, varsFile := range varsFiles {
content, err := os.ReadFile(varsFile)
if err != nil {
return nil, err
}

var t []codersdk.VariableValue
var t []VariableValue
ext := filepath.Ext(varsFile)
switch ext {
case ".tfvars":
Expand All @@ -103,7 +101,7 @@ func parseVariableValuesFromVarsFiles(varsFiles []string) ([]codersdk.VariableVa
return parsed, nil
}

func parseVariableValuesFromHCL(content []byte) ([]codersdk.VariableValue, error) {
func parseVariableValuesFromHCL(content []byte) ([]VariableValue, error) {
parser := hclparse.NewParser()
hclFile, diags := parser.ParseHCL(content, "file.hcl")
if diags.HasErrors() {
Expand Down Expand Up @@ -159,7 +157,7 @@ func parseVariableValuesFromHCL(content []byte) ([]codersdk.VariableValue, error
// parseVariableValuesFromJSON converts the .tfvars.json content into template variables.
// The function visits only root-level properties as template variables do not support nested
// structures.
func parseVariableValuesFromJSON(content []byte) ([]codersdk.VariableValue, error) {
func parseVariableValuesFromJSON(content []byte) ([]VariableValue, error) {
var data map[string]interface{}
err := json.Unmarshal(content, &data)
if err != nil {
Expand All @@ -183,10 +181,10 @@ func parseVariableValuesFromJSON(content []byte) ([]codersdk.VariableValue, erro
return convertMapIntoVariableValues(stringData), nil
}

func convertMapIntoVariableValues(m map[string]string) []codersdk.VariableValue {
var parsed []codersdk.VariableValue
func convertMapIntoVariableValues(m map[string]string) []VariableValue {
var parsed []VariableValue
for key, value := range m {
parsed = append(parsed, codersdk.VariableValue{
parsed = append(parsed, VariableValue{
Name: key,
Value: value,
})
Expand All @@ -197,8 +195,8 @@ func convertMapIntoVariableValues(m map[string]string) []codersdk.VariableValue
return parsed
}

func parseVariableValuesFromFile(variablesFile string) ([]codersdk.VariableValue, error) {
var values []codersdk.VariableValue
func parseVariableValuesFromFile(variablesFile string) ([]VariableValue, error) {
var values []VariableValue
if variablesFile == "" {
return values, nil
}
Expand All @@ -209,7 +207,7 @@ func parseVariableValuesFromFile(variablesFile string) ([]codersdk.VariableValue
}

for name, value := range variablesMap {
values = append(values, codersdk.VariableValue{
values = append(values, VariableValue{
Name: name,
Value: value,
})
Expand Down Expand Up @@ -237,23 +235,23 @@ func createVariablesMapFromFile(variablesFile string) (map[string]string, error)
return variablesMap, nil
}

func parseVariableValuesFromCommandLine(variables []string) ([]codersdk.VariableValue, error) {
var values []codersdk.VariableValue
func parseVariableValuesFromCommandLine(variables []string) ([]VariableValue, error) {
var values []VariableValue
for _, keyValue := range variables {
split := strings.SplitN(keyValue, "=", 2)
if len(split) < 2 {
return nil, xerrors.Errorf("format key=value expected, but got %s", keyValue)
}

values = append(values, codersdk.VariableValue{
values = append(values, VariableValue{
Name: split[0],
Value: split[1],
})
}
return values, nil
}

func combineVariableValues(valuesSets ...[]codersdk.VariableValue) []codersdk.VariableValue {
func combineVariableValues(valuesSets ...[]VariableValue) []VariableValue {
combinedValues := make(map[string]string)

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

var result []codersdk.VariableValue
var result []VariableValue
for name, value := range combinedValues {
result = append(result, codersdk.VariableValue{Name: name, Value: value})
result = append(result, VariableValue{Name: name, Value: value})
}

sort.Slice(result, func(i, j int) bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cli_test
package codersdk_test

import (
"os"
Expand All @@ -7,7 +7,6 @@ import (

"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/cli"
"github.com/coder/coder/v2/codersdk"
)

Expand Down Expand Up @@ -47,7 +46,7 @@ func TestDiscoverVarsFiles(t *testing.T) {
}

// When
found, err := cli.DiscoverVarsFiles(tempDir)
found, err := codersdk.DiscoverVarsFiles(tempDir)
require.NoError(t, err)

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

// When
actual, err := cli.ParseUserVariableValues([]string{
actual, err := codersdk.ParseUserVariableValues([]string{
filepath.Join(tempDir, hclFilename1),
filepath.Join(tempDir, hclFilename2),
filepath.Join(tempDir, jsonFilename3),
Expand Down Expand Up @@ -136,7 +135,7 @@ func TestParseVariableValuesFromVarsFiles_InvalidJSON(t *testing.T) {
require.NoError(t, err)

// When
actual, err := cli.ParseUserVariableValues([]string{
actual, err := codersdk.ParseUserVariableValues([]string{
filepath.Join(tempDir, jsonFilename),
}, "", nil)

Expand Down Expand Up @@ -167,7 +166,7 @@ cores: 2`
require.NoError(t, err)

// When
actual, err := cli.ParseUserVariableValues([]string{
actual, err := codersdk.ParseUserVariableValues([]string{
filepath.Join(tempDir, hclFilename),
}, "", nil)

Expand Down
Loading