2016-02-02 23:10:22 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-06-23 18:10:28 +02:00
|
|
|
"bytes"
|
2016-02-02 23:10:22 +01:00
|
|
|
"fmt"
|
2016-02-02 23:10:22 +01:00
|
|
|
"io"
|
2018-05-23 06:32:43 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2016-02-02 23:10:22 +01:00
|
|
|
"os"
|
2018-05-23 06:32:43 +02:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2016-02-02 23:10:22 +01:00
|
|
|
"strings"
|
|
|
|
|
2019-01-17 19:44:37 +01:00
|
|
|
"github.com/hashicorp/hcl2/hcl"
|
|
|
|
"github.com/hashicorp/hcl2/hcl/hclsyntax"
|
|
|
|
"github.com/hashicorp/hcl2/hclwrite"
|
2016-02-02 23:10:22 +01:00
|
|
|
"github.com/mitchellh/cli"
|
2018-05-23 06:32:43 +02:00
|
|
|
|
2019-01-17 19:44:37 +01:00
|
|
|
"github.com/hashicorp/terraform/configs"
|
2018-05-23 06:32:43 +02:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2016-02-02 23:10:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-05-23 06:32:43 +02:00
|
|
|
stdinArg = "-"
|
2016-02-02 23:10:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// FmtCommand is a Command implementation that rewrites Terraform config
|
|
|
|
// files to a canonical format and style.
|
|
|
|
type FmtCommand struct {
|
|
|
|
Meta
|
2018-05-23 06:32:43 +02:00
|
|
|
list bool
|
|
|
|
write bool
|
|
|
|
diff bool
|
|
|
|
check bool
|
|
|
|
recursive bool
|
|
|
|
input io.Reader // STDIN if nil
|
2016-02-02 23:10:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FmtCommand) Run(args []string) int {
|
2016-02-02 23:10:22 +01:00
|
|
|
if c.input == nil {
|
|
|
|
c.input = os.Stdin
|
|
|
|
}
|
|
|
|
|
2017-03-08 05:09:48 +01:00
|
|
|
args, err := c.Meta.process(args, false)
|
|
|
|
if err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
2016-02-02 23:10:22 +01:00
|
|
|
|
2018-11-21 15:35:27 +01:00
|
|
|
cmdFlags := c.Meta.defaultFlagSet("fmt")
|
2018-05-23 06:32:43 +02:00
|
|
|
cmdFlags.BoolVar(&c.list, "list", true, "list")
|
|
|
|
cmdFlags.BoolVar(&c.write, "write", true, "write")
|
|
|
|
cmdFlags.BoolVar(&c.diff, "diff", false, "diff")
|
2017-06-23 18:10:28 +02:00
|
|
|
cmdFlags.BoolVar(&c.check, "check", false, "check")
|
2018-05-23 06:32:43 +02:00
|
|
|
cmdFlags.BoolVar(&c.recursive, "recursive", false, "recursive")
|
2016-02-02 23:10:22 +01:00
|
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
2019-08-16 14:31:21 +02:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
|
2016-02-02 23:10:22 +01:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = cmdFlags.Args()
|
2016-02-02 23:10:22 +01:00
|
|
|
if len(args) > 1 {
|
|
|
|
c.Ui.Error("The fmt command expects at most one argument.")
|
2016-02-02 23:10:22 +01:00
|
|
|
cmdFlags.Usage()
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2019-01-17 19:58:59 +01:00
|
|
|
var paths []string
|
2016-02-02 23:10:22 +01:00
|
|
|
if len(args) == 0 {
|
2019-01-17 19:58:59 +01:00
|
|
|
paths = []string{"."}
|
2016-02-02 23:10:23 +01:00
|
|
|
} else if args[0] == stdinArg {
|
2018-05-23 06:32:43 +02:00
|
|
|
c.list = false
|
|
|
|
c.write = false
|
2016-02-02 23:10:23 +01:00
|
|
|
} else {
|
2019-01-17 19:58:59 +01:00
|
|
|
paths = []string{args[0]}
|
2016-02-02 23:10:22 +01:00
|
|
|
}
|
|
|
|
|
2017-06-23 18:10:28 +02:00
|
|
|
var output io.Writer
|
2018-05-23 06:32:43 +02:00
|
|
|
list := c.list // preserve the original value of -list
|
2017-06-23 18:10:28 +02:00
|
|
|
if c.check {
|
|
|
|
// set to true so we can use the list output to check
|
|
|
|
// if the input needs formatting
|
2018-05-23 06:32:43 +02:00
|
|
|
c.list = true
|
|
|
|
c.write = false
|
2017-06-23 18:10:28 +02:00
|
|
|
output = &bytes.Buffer{}
|
|
|
|
} else {
|
|
|
|
output = &cli.UiWriter{Ui: c.Ui}
|
|
|
|
}
|
|
|
|
|
2019-01-17 19:58:59 +01:00
|
|
|
diags := c.fmt(paths, c.input, output)
|
2018-05-23 06:32:43 +02:00
|
|
|
c.showDiagnostics(diags)
|
|
|
|
if diags.HasErrors() {
|
2016-02-02 23:10:22 +01:00
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
2017-06-23 18:10:28 +02:00
|
|
|
if c.check {
|
|
|
|
buf := output.(*bytes.Buffer)
|
|
|
|
ok := buf.Len() == 0
|
|
|
|
if list {
|
|
|
|
io.Copy(&cli.UiWriter{Ui: c.Ui}, buf)
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
return 3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:10:22 +01:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-05-23 06:32:43 +02:00
|
|
|
func (c *FmtCommand) fmt(paths []string, stdin io.Reader, stdout io.Writer) tfdiags.Diagnostics {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
|
|
|
if len(paths) == 0 { // Assuming stdin, then.
|
|
|
|
if c.write {
|
|
|
|
diags = diags.Append(fmt.Errorf("Option -write cannot be used when reading from stdin"))
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
fileDiags := c.processFile("<stdin>", stdin, stdout, true)
|
|
|
|
diags = diags.Append(fileDiags)
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, path := range paths {
|
|
|
|
path = c.normalizePath(path)
|
2019-01-17 19:58:59 +01:00
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
diags = diags.Append(fmt.Errorf("No file or directory at %s", path))
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
dirDiags := c.processDir(path, stdout)
|
|
|
|
diags = diags.Append(dirDiags)
|
|
|
|
} else {
|
|
|
|
switch filepath.Ext(path) {
|
|
|
|
case ".tf", ".tfvars":
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
// Open does not produce error messages that are end-user-appropriate,
|
|
|
|
// so we'll need to simplify here.
|
|
|
|
diags = diags.Append(fmt.Errorf("Failed to read file %s", path))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fileDiags := c.processFile(c.normalizePath(path), f, stdout, false)
|
|
|
|
diags = diags.Append(fileDiags)
|
|
|
|
f.Close()
|
|
|
|
default:
|
|
|
|
diags = diags.Append(fmt.Errorf("Only .tf and .tfvars files can be processed with terraform fmt"))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 06:32:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FmtCommand) processFile(path string, r io.Reader, w io.Writer, isStdout bool) tfdiags.Diagnostics {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
|
|
|
log.Printf("[TRACE] terraform fmt: Formatting %s", path)
|
|
|
|
|
|
|
|
src, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
diags = diags.Append(fmt.Errorf("Failed to read %s", path))
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
2019-01-17 19:44:37 +01:00
|
|
|
// File must be parseable as HCL native syntax before we'll try to format
|
|
|
|
// it. If not, the formatter is likely to make drastic changes that would
|
|
|
|
// be hard for the user to undo.
|
|
|
|
_, syntaxDiags := hclsyntax.ParseConfig(src, path, hcl.Pos{Line: 1, Column: 1})
|
|
|
|
if syntaxDiags.HasErrors() {
|
|
|
|
diags = diags.Append(syntaxDiags)
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
2018-05-23 06:32:43 +02:00
|
|
|
result := hclwrite.Format(src)
|
|
|
|
|
|
|
|
if !bytes.Equal(src, result) {
|
|
|
|
// Something was changed
|
|
|
|
if c.list {
|
|
|
|
fmt.Fprintln(w, path)
|
|
|
|
}
|
|
|
|
if c.write {
|
|
|
|
err := ioutil.WriteFile(path, result, 0644)
|
|
|
|
if err != nil {
|
|
|
|
diags = diags.Append(fmt.Errorf("Failed to write %s", path))
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.diff {
|
|
|
|
diff, err := bytesDiff(src, result, path)
|
|
|
|
if err != nil {
|
|
|
|
diags = diags.Append(fmt.Errorf("Failed to generate diff for %s: %s", path, err))
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
w.Write(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.list && !c.write && !c.diff {
|
|
|
|
_, err = w.Write(result)
|
|
|
|
if err != nil {
|
|
|
|
diags = diags.Append(fmt.Errorf("Failed to write result"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FmtCommand) processDir(path string, stdout io.Writer) tfdiags.Diagnostics {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
|
|
|
log.Printf("[TRACE] terraform fmt: looking for files in %s", path)
|
|
|
|
|
|
|
|
entries, err := ioutil.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case os.IsNotExist(err):
|
|
|
|
diags = diags.Append(fmt.Errorf("There is no configuration directory at %s", path))
|
|
|
|
default:
|
|
|
|
// ReadDir does not produce error messages that are end-user-appropriate,
|
|
|
|
// so we'll need to simplify here.
|
|
|
|
diags = diags.Append(fmt.Errorf("Cannot read directory %s", path))
|
|
|
|
}
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range entries {
|
|
|
|
name := info.Name()
|
|
|
|
if configs.IsIgnoredFile(name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
subPath := filepath.Join(path, name)
|
|
|
|
if info.IsDir() {
|
|
|
|
if c.recursive {
|
|
|
|
subDiags := c.processDir(subPath, stdout)
|
|
|
|
diags = diags.Append(subDiags)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do not recurse into child directories by default because we
|
|
|
|
// want to mimic the file-reading behavior of "terraform plan", etc,
|
|
|
|
// operating on one module at a time.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ext := filepath.Ext(name)
|
|
|
|
switch ext {
|
|
|
|
case ".tf", ".tfvars":
|
|
|
|
f, err := os.Open(subPath)
|
|
|
|
if err != nil {
|
|
|
|
// Open does not produce error messages that are end-user-appropriate,
|
|
|
|
// so we'll need to simplify here.
|
|
|
|
diags = diags.Append(fmt.Errorf("Failed to read file %s", subPath))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fileDiags := c.processFile(c.normalizePath(subPath), f, stdout, false)
|
|
|
|
diags = diags.Append(fileDiags)
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:10:22 +01:00
|
|
|
func (c *FmtCommand) Help() string {
|
|
|
|
helpText := `
|
2016-02-02 23:10:22 +01:00
|
|
|
Usage: terraform fmt [options] [DIR]
|
|
|
|
|
2018-05-23 06:32:43 +02:00
|
|
|
Rewrites all Terraform configuration files to a canonical format. Both
|
|
|
|
configuration files (.tf) and variables files (.tfvars) are updated.
|
|
|
|
JSON files (.tf.json or .tfvars.json) are not modified.
|
2016-02-02 23:10:22 +01:00
|
|
|
|
2016-02-02 23:10:22 +01:00
|
|
|
If DIR is not specified then the current working directory will be used.
|
2018-05-23 06:32:43 +02:00
|
|
|
If DIR is "-" then content will be read from STDIN. The given content must
|
|
|
|
be in the Terraform language native syntax; JSON is not supported.
|
2016-02-02 23:10:22 +01:00
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2018-05-23 06:32:43 +02:00
|
|
|
-list=false Don't list files whose formatting differs
|
|
|
|
(always disabled if using STDIN)
|
2016-02-02 23:10:22 +01:00
|
|
|
|
2018-05-23 06:32:43 +02:00
|
|
|
-write=false Don't write to source files
|
|
|
|
(always disabled if using STDIN or -check)
|
2016-02-02 23:10:22 +01:00
|
|
|
|
2018-05-23 06:32:43 +02:00
|
|
|
-diff Display diffs of formatting changes
|
2016-02-02 23:10:22 +01:00
|
|
|
|
2018-05-23 06:32:43 +02:00
|
|
|
-check Check if the input is formatted. Exit status will be 0 if all
|
|
|
|
input is properly formatted and non-zero otherwise.
|
2017-06-23 18:10:28 +02:00
|
|
|
|
2019-08-04 10:18:09 +02:00
|
|
|
-no-color If specified, output won't contain any color.
|
|
|
|
|
2018-05-23 06:32:43 +02:00
|
|
|
-recursive Also process files in subdirectories. By default, only the
|
|
|
|
given directory (or current directory) is processed.
|
2016-02-02 23:10:22 +01:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FmtCommand) Synopsis() string {
|
|
|
|
return "Rewrites config files to canonical format"
|
|
|
|
}
|
2018-05-23 06:32:43 +02:00
|
|
|
|
|
|
|
func bytesDiff(b1, b2 []byte, path string) (data []byte, err error) {
|
|
|
|
f1, err := ioutil.TempFile("", "")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove(f1.Name())
|
|
|
|
defer f1.Close()
|
|
|
|
|
|
|
|
f2, err := ioutil.TempFile("", "")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove(f2.Name())
|
|
|
|
defer f2.Close()
|
|
|
|
|
|
|
|
f1.Write(b1)
|
|
|
|
f2.Write(b2)
|
|
|
|
|
|
|
|
data, err = exec.Command("diff", "--label=old/"+path, "--label=new/"+path, "-u", f1.Name(), f2.Name()).CombinedOutput()
|
|
|
|
if len(data) > 0 {
|
|
|
|
// diff exits with a non-zero status when the files don't match.
|
|
|
|
// Ignore that failure as long as we get output.
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|