command/fmt: Factor out the actual formatting

Previously formatting was just a simple wrapper around hclwrite.Format.
That remains true here, but the call is factored out into a separate
method in preparation for making it also do some Terraform-specific
cleanups in a future commit.
This commit is contained in:
Martin Atkins 2020-09-25 16:07:41 -07:00
parent f636a90e67
commit 05f6a62399
1 changed files with 7 additions and 1 deletions

View File

@ -176,7 +176,7 @@ func (c *FmtCommand) processFile(path string, r io.Reader, w io.Writer, isStdout
return diags return diags
} }
result := hclwrite.Format(src) result := c.formatSourceCode(src)
if !bytes.Equal(src, result) { if !bytes.Equal(src, result) {
// Something was changed // Something was changed
@ -266,6 +266,12 @@ func (c *FmtCommand) processDir(path string, stdout io.Writer) tfdiags.Diagnosti
return diags return diags
} }
// formatSourceCode is the formatting logic itself, applied to each file that
// is selected (directly or indirectly) on the command line.
func (c *FmtCommand) formatSourceCode(src []byte) []byte {
return hclwrite.Format(src)
}
func (c *FmtCommand) Help() string { func (c *FmtCommand) Help() string {
helpText := ` helpText := `
Usage: terraform fmt [options] [DIR] Usage: terraform fmt [options] [DIR]