Files
coder/provisioner/terraform/parameters.go
Marcin Tojek c7fb5f960c feat: preserve original order of rich parameters (#6747)
* WIP

* orderedParameters

* fix

* WIP

* TestS
2023-03-24 09:37:27 +01:00

56 lines
1.3 KiB
Go

package terraform
import (
"fmt"
"os"
"path"
"strings"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclparse"
)
var terraformWithCoderParametersSchema = &hcl.BodySchema{
Blocks: []hcl.BlockHeaderSchema{
{
Type: "data",
LabelNames: []string{"coder_parameter", "*"},
},
},
}
func rawRichParameterNames(workdir string) ([]string, error) {
entries, err := os.ReadDir(workdir)
if err != nil {
return nil, err
}
var coderParameterNames []string
for _, entry := range entries {
if !strings.HasSuffix(entry.Name(), ".tf") {
continue
}
hclFilepath := path.Join(workdir, entry.Name())
parser := hclparse.NewParser()
parsedHCL, diags := parser.ParseHCLFile(hclFilepath)
if diags.HasErrors() {
return nil, hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "Failed to parse HCL file",
Detail: fmt.Sprintf("parser.ParseHCLFile can't parse %q file", hclFilepath),
},
}
}
content, _, _ := parsedHCL.Body.PartialContent(terraformWithCoderParametersSchema)
for _, block := range content.Blocks {
if block.Type == "data" && block.Labels[0] == "coder_parameter" && len(block.Labels) == 2 {
coderParameterNames = append(coderParameterNames, block.Labels[1])
}
}
}
return coderParameterNames, nil
}