Skip to content

feat: Use hclwriter in gotemplates #936

Open
@vincenthsh

Description

@vincenthsh

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 as zclconf/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:

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions