packer-cn/website/content/guides/hcl/component-object-spec.mdx
Adrien Delorme ed091163be
HCL2 Parse packer.required_plugins block + packer init (#10304)
This adds the new `required_plugins` block to be nested under the packer block.

Example:
```hcl
packer {
  required_plugins {
    aws = {
      version = ">= 2.7.0"
      source = "azr/aws"
    }
    azure = ">= 2.7.0"
  }
}
```

For example on darwin_amd64 Packer will install those under :
* "${PACKER_HOME_DIR}/plugin/github.com/azr/amazon/packer-plugin-amazon_2.7.0_x5.0_darwin_amd64"
* "${PACKER_HOME_DIR}/plugin/github.com/hashicorp/azure/packer-plugin-azure_2.7.0_x5.0_darwin_amd64_x5"

+ docs
+ tests
2021-02-02 18:05:04 +01:00

43 lines
1.7 KiB
Plaintext

---
page_title: Generating code for config spec.
sidebar_title: Making a plugin HCL2 enabled
description: Learn how to generate the HCL2 configuration of your component easily.
---
# Auto Generate the HCL2 code of a plugin
From v1.5, Packer can be configured using HCL2. Because Packer has so many
builders, provisioner & post-processors, we relied on code generation to iterate
more easily. The good news is that you can benefit from this code generator to
get the HCL2 spec code of your component simply. It's a Go binary package and is
located in [`cmd/mapstructure-to-hcl2`](https://github.com/hashicorp/packer/tree/master/cmd/mapstructure-to-hcl2).
Say you want to configure the `Config` struct of a `Builder` in a package
located in `my/example-plugin/config.go`. Here are some simple steps you can
follow to make it HCL2 enabled:
- 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`.
- Make sure that all the nested structs of `Config` are also auto generated the
same way.
- 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 on every time you add or change a field of Config you will need to
run the `go generate` command again.
A good example of this is the [Config struct of the amazon-ebs builder](https://github.com/hashicorp/packer/blob/master/builder/amazon/ebs/builder.go)