From efb6b28fde28014a999f080aebd87e487ceb700c Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Tue, 14 Feb 2023 10:06:42 -0500 Subject: [PATCH] cli: `fmt -check` should return early on diff The `nomad fmt -check` command incorrectly writes to file because we didn't return before writing the file on a diff. The docstring for the `-list` and `-write` flags is also unclear and can be easily misread to be the opposite of the actual behavior. Clarify this and fix up the docs to match. --- command/fmt.go | 41 +++++++++++++++------------ website/content/docs/commands/fmt.mdx | 19 +++++++------ 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/command/fmt.go b/command/fmt.go index 0156c295a3b8..07cf8a037a4a 100644 --- a/command/fmt.go +++ b/command/fmt.go @@ -47,27 +47,30 @@ func (*FormatCommand) Help() string { helpText := ` Usage: nomad fmt [flags] paths ... - Formats Nomad agent configuration and job file to a canonical format. - If a path is a directory, it will recursively format all files - with .nomad and .hcl extensions in the directory. - - If you provide a single dash (-) as argument, fmt will read from standard - input (STDIN) and output the processed output to standard output (STDOUT). + Formats Nomad agent configuration and job file to a canonical format. + If a path is a directory, it will recursively format all files + with .nomad and .hcl extensions in the directory. + + If you provide a single dash (-) as argument, fmt will read from standard + input (STDIN) and output the processed output to standard output (STDOUT). Format Options: - -list=false - Don't list the files, which contain formatting inconsistencies. + -check + Check if the files are valid HCL files. If not, exit status of the + command will be 1 and the incorrect files will not be formatted. This + flag overrides any -write flag value. - -check - Check if the files are valid HCL files. If not, exit status of the command - will be 1 and the incorrect files will not be formatted. + -list + List the files which contain formatting inconsistencies. Defaults + to -list=true. - -write=false - Don't overwrite the input files. + -recursive + Process files in subdirectories. By default only the given (or current) + directory is processed. - -recursive - Process also files in subdirectories. By default only the given (or current) directory is processed. + -write + Overwrite the input files. Defaults to -write=true. ` return strings.TrimSpace(helpText) @@ -251,6 +254,11 @@ func (f *FormatCommand) processFile(path string, r io.Reader) { f.Ui.Output(path) } + if f.check { + f.checkSuccess = false + return + } + if f.write { if err := os.WriteFile(path, out, 0644); err != nil { f.appendError(fmt.Errorf("Failed to write file %s: %w", path, err)) @@ -258,9 +266,6 @@ func (f *FormatCommand) processFile(path string, r io.Reader) { } } - if f.check { - f.checkSuccess = false - } } if !f.list && !f.write { diff --git a/website/content/docs/commands/fmt.mdx b/website/content/docs/commands/fmt.mdx index 2788805d89a9..f102e4f4c7b3 100644 --- a/website/content/docs/commands/fmt.mdx +++ b/website/content/docs/commands/fmt.mdx @@ -26,16 +26,19 @@ If you provide a single dash (-) as argument, fmt will read from standard input ## Format Options: -- `-list=false` : Don't list the files, which contain formatting inconsistencies. -- `-check` : Check if the files are valid HCL files. If not, exit status of the command - will be 1 and the incorrect files will not be formatted. -- `-write=false` : Don't overwrite the input files. -- `-recursive` : Process also files in subdirectories. By default only the given (or current) directory is processed. +- `-check`: Check if the files are valid HCL files. If not, exit status of the + command will be 1 and the incorrect files will not be formatted. This flag + overrides any `-write` flag value. +- `-list`: List the files which contain formatting inconsistencies. Defaults to + `-list=true`. +- `-recursive`: Process files in subdirectories. By default only the given (or + current) directory is processed. +- `-write`: Overwrite the input files. Defaults to `-write=true`. ## Examples ```shell-session -$ cat agent.hcl +$ cat agent.hcl server { enabled = true bootstrap_expect = 1 @@ -46,9 +49,9 @@ client { } $ nomad fmt - agent.hcl -$ cat agent.hcl + +$ cat agent.hcl server { enabled = true bootstrap_expect = 1