acabc1c1aa
* Add packer fmt command This change adds a new command that allows users to format one or more HCL2 Packer configuration template files. Related to: #9174 * command/fmt: Add check flag Packer's fmt command now supports a check flag that will output the name of any file that would be changed by the HCL2 formatting engine. The check flag is mutually exclusive with the write flag and will only check if formatting is needed. The update write flag will now overwrite the source files with the newly formatted HCL2 source unless the `-write=false` or `-check` is passed at the command line. * Returns a diagnostic error if Format is unable to show a diff - equivalent to `terraform fmt` * Updates testing to run against #Format and not the private methods of the HCL2Formatter; fixes ShowDiff test failure on Windows * Updates comments for exported functions * Add docs for fmt command
110 lines
2.3 KiB
Go
110 lines
2.3 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"strings"
|
|
|
|
hclutils "github.com/hashicorp/packer/hcl2template"
|
|
"github.com/posener/complete"
|
|
)
|
|
|
|
type FormatCommand struct {
|
|
Meta
|
|
}
|
|
|
|
func (c *FormatCommand) Run(args []string) int {
|
|
ctx := context.Background()
|
|
cfg, ret := c.ParseArgs(args)
|
|
if ret != 0 {
|
|
return ret
|
|
}
|
|
|
|
return c.RunContext(ctx, cfg)
|
|
}
|
|
|
|
func (c *FormatCommand) ParseArgs(args []string) (*FormatArgs, int) {
|
|
var cfg FormatArgs
|
|
flags := c.Meta.FlagSet("format", FlagSetNone)
|
|
flags.Usage = func() { c.Ui.Say(c.Help()) }
|
|
cfg.AddFlagSets(flags)
|
|
if err := flags.Parse(args); err != nil {
|
|
return &cfg, 1
|
|
}
|
|
|
|
args = flags.Args()
|
|
if len(args) != 1 {
|
|
flags.Usage()
|
|
return &cfg, 1
|
|
}
|
|
|
|
cfg.Path = args[0]
|
|
return &cfg, 0
|
|
}
|
|
|
|
func (c *FormatCommand) RunContext(ctx context.Context, cla *FormatArgs) int {
|
|
if cla.Check {
|
|
cla.Write = false
|
|
}
|
|
|
|
formatter := hclutils.HCL2Formatter{
|
|
ShowDiff: cla.Diff,
|
|
Write: cla.Write,
|
|
Output: os.Stdout,
|
|
}
|
|
|
|
bytesModified, diags := formatter.Format(cla.Path)
|
|
ret := writeDiags(c.Ui, nil, diags)
|
|
if ret != 0 {
|
|
return ret
|
|
}
|
|
|
|
if cla.Check && bytesModified > 0 {
|
|
// exit code taken from `terraform fmt` command
|
|
return 3
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
func (*FormatCommand) Help() string {
|
|
helpText := `
|
|
Usage: packer fmt [options] [TEMPLATE]
|
|
|
|
Rewrites all Packer configuration files to a canonical format. Both
|
|
configuration files (.pkr.hcl) and variable files (.pkrvars) are updated.
|
|
JSON files (.json) are not modified.
|
|
|
|
If TEMPATE is "." the current directory will be used. The given content must
|
|
be in Packer's HCL2 configuration language; JSON is not supported.
|
|
|
|
Options:
|
|
-check Check if the input is formatted. Exit status will be 0 if all
|
|
input is properly formatted and non-zero otherwise.
|
|
|
|
-diff Display diffs of formatting change
|
|
|
|
-write=false Don't write to source files
|
|
(always disabled if using -check)
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
}
|
|
|
|
func (*FormatCommand) Synopsis() string {
|
|
return "Rewrites HCL2 config files to canonical format"
|
|
}
|
|
|
|
func (*FormatCommand) AutocompleteArgs() complete.Predictor {
|
|
return complete.PredictNothing
|
|
}
|
|
|
|
func (*FormatCommand) AutocompleteFlags() complete.Flags {
|
|
return complete.Flags{
|
|
"-check": complete.PredictNothing,
|
|
"-diff": complete.PredictNothing,
|
|
"-write": complete.PredictNothing,
|
|
}
|
|
}
|