Skip to content

Commit

Permalink
cli: fmt -check should return early on diff
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tgross committed Feb 14, 2023
1 parent 1154c05 commit efb6b28
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
41 changes: 23 additions & 18 deletions command/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -251,16 +254,18 @@ 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))
return
}
}

if f.check {
f.checkSuccess = false
}
}

if !f.list && !f.write {
Expand Down
19 changes: 11 additions & 8 deletions website/content/docs/commands/fmt.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -46,9 +49,9 @@ client {
}
$ nomad fmt
agent.hcl
$ cat agent.hcl
$ cat agent.hcl
server {
enabled = true
bootstrap_expect = 1
Expand Down

0 comments on commit efb6b28

Please sign in to comment.