fogg
fogg copied to clipboard
feat: Use hclwriter in gotemplates
Example Implementation writing a foo = {}
assignment
Example Implementation writing a provider "foo" {}
block
Usage in go templates:
terraform {
required_version = "={{ .TerraformVersion }}"
{{ template "backend" .Backend }}
required_providers {
{{- range $k, $v := .ProviderVersions }}
{{ toHclAssignment $k $v }}
{{- end }}
}
}
Result:
terraform {
required_version = "=1.5.7"
backend "s3" {
# snip
...
}
required_providers {
archive = {
source = "hashicorp/archive"
version = "~> 2.0"
}
assert = {
source = "bwoznicki/assert"
version = "0.0.1"
}
...
Caveat: requires
json
tags on the golang structs aszclconf/go-cty
only supports UnmarshalJSON// json tags required for toHCLAssignment function // in util/template.go type ProviderVersion struct { Source string `yaml:"source" json:"source"` Version *string `yaml:"version" json:"version"` }
Inspiration from:
Just noticed github.com/zclconf/go-cty-yaml - may not need json tags 🤦♂️