Wilken Rivera acabc1c1aa
Add packer fmt command (#10225)
* 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
2020-11-11 11:49:39 -05:00

47 lines
1.2 KiB
Plaintext

---
description: |
The `packer fmt` Packer command is used to format HCL2
configuration files to a canonical format and style.
layout: docs
page_title: packer fmt - Commands
sidebar_title: <tt>fmt</tt>
---
# `fmt` Command
The `packer fmt` Packer command is used to format HCL2 configuration files to
a canonical format and style. JSON files (.json) are not modified. This command
applies a subset of HCL language style conventions, along with other minor
adjustments for readability.
`packer fmt` will display the name of the configuration file(s) that need formatting,
and write any formatted changes back to the original configuration file(s).
Example usage:
Check if configuration file(s) need to be formatted, but don't write the changes.
```shell-session
$ packer fmt -check .
my-template.json
```
Format a configuration file, writing the changes back to the original file.
```shell-session
$ packer fmt my-template.json
my-template.json
```
## Options
- `-check` - Checks if the input is formatted. Exit status will be 0 if all
input is properly formatted and non-zero otherwise.
- `-diff` - Display diffs of any formatting change
- `-write=false` - Don't write formatting changes to source files
(always disabled if using -check)