Skip to content

Commit

Permalink
cmd/shfmt: support -f[=0] flag
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Davids <[email protected]>
  • Loading branch information
sdavids committed Oct 1, 2024
1 parent 4183cbc commit dde54b1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
22 changes: 16 additions & 6 deletions cmd/shfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var (
write = &multiFlag[bool]{"w", "write", false}
simplify = &multiFlag[bool]{"s", "simplify", false}
minify = &multiFlag[bool]{"mn", "minify", false}
find = &multiFlag[bool]{"f", "find", false}
find = &multiFlag[boolString]{"f", "find", "false"}
diff = &multiFlag[bool]{"d", "diff", false}
applyIgnore = &multiFlag[bool]{"", "apply-ignore", false}

Expand Down Expand Up @@ -172,9 +172,10 @@ Printer options:
Utilities:
-f, --find recursively find all shell files and print the paths
--to-json print syntax tree to stdout as a typed JSON
--from-json read syntax tree from stdin as a typed JSON
-f[=0], --find[=0] recursively find all shell files and print the paths;
paths are separated by a newline or a null character if -f=0
--to-json print syntax tree to stdout as a typed JSON
--from-json read syntax tree from stdin as a typed JSON
For more information, see 'man shfmt' and https://github.com/mvdan/sh.
`)
Expand All @@ -201,6 +202,10 @@ For more information, see 'man shfmt' and https://github.com/mvdan/sh.
fmt.Fprintf(os.Stderr, "only -l and -l=0 allowed\n")
return 1
}
if find.val != "true" && find.val != "false" && find.val != "0" {
fmt.Fprintf(os.Stderr, "only -f and -f=0 allowed\n")
return 1
}
if minify.val {
simplify.val = true
}
Expand Down Expand Up @@ -268,7 +273,7 @@ For more information, see 'man shfmt' and https://github.com/mvdan/sh.
}
status := 0
for _, path := range flag.Args() {
if info, err := os.Stat(path); err == nil && !info.IsDir() && !applyIgnore.val && !find.val {
if info, err := os.Stat(path); err == nil && !info.IsDir() && !applyIgnore.val && find.val == "false" {
// When given paths to files directly, always format them,
// no matter their extension or shebang.
//
Expand Down Expand Up @@ -442,9 +447,14 @@ func formatPath(path string, checkShebang bool) error {
}
readBuf.Write(copyBuf[:n])
}
if find.val {
switch find.val {
case "true":
fmt.Println(path)
return nil
case "0":
fmt.Print(path)
fmt.Print("\000")
return nil
}
if _, err := io.CopyBuffer(&readBuf, f, copyBuf); err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions cmd/shfmt/shfmt.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ predictable. Some aspects of the format can be configured via printer flags.

## Utility flags

*-f*, *--find*
Recursively find all shell files and print the paths.
*-f[=0]*, *--find[=0]*
Recursively find all shell files and print the paths;
paths are separated by a newline or a null character if -f=0.

*--to-json*
Print syntax tree to stdout as a typed JSON.
Expand Down

0 comments on commit dde54b1

Please sign in to comment.