From 8c219166068cfa8de3af5ec6c88dfd21270ececb Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 18 Dec 2019 15:01:48 +0100 Subject: [PATCH] add guide on how to make ones component HCL2 enabled --- .../component-object-spec/index.html.mr.erb | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 website/source/guides/hcl/component-object-spec/index.html.mr.erb diff --git a/website/source/guides/hcl/component-object-spec/index.html.mr.erb b/website/source/guides/hcl/component-object-spec/index.html.mr.erb new file mode 100644 index 000000000..68ebe0178 --- /dev/null +++ b/website/source/guides/hcl/component-object-spec/index.html.mr.erb @@ -0,0 +1,37 @@ +--- +layout: guides +page_title: Generating code for congig spec. +sidebar_current: hcl +description: |- + Learn how to generate the HCL2 configuration of your component easily. +--- + +# Packer 1.5.0 can be + +From version 1.5.0 Packer can be configured using HCL2. Because Packer has so +many builders, provisioner & post-proessors, we relied on code generation to +iterate more easily. The good new is that you can benefit from this code +generator to get the HCL2 spec of your component simply. Is it a Go binary +package and is located in `cmd/mapstructure-to-hcl2`. Here are some simple +steps you can follow to have most of the code required : + +Say you want to configure the Config struct of a Builder in a package located +in `my/example-plugin/config.go`. + +* run `go install github.com/hashicorp/packer/cmd/mapstructure-to-hcl2` + +* Add `//go:generate mapstructure-to-hcl2 -type Config` at the top of +`config.go` + +* run `go generate my/example-plugin` + +This will generate a `my/example-plugin/config.hcl2spec.go` file containing the +configuration fields of Config. Now we only need to make Builder implement the +interface by adding the following snippet: + +```go +func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() } +``` + +From now one every time you add or change a field of Config you will need to +run he `go generate` command again.