Skip to content

Commit

Permalink
cmd/shfmt: add -f0 flag
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Davids <[email protected]>
  • Loading branch information
sdavids committed Sep 20, 2024
1 parent a71f067 commit 4ccbf3a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
19 changes: 16 additions & 3 deletions cmd/shfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
simplify = &multiFlag[bool]{"s", "simplify", false}
minify = &multiFlag[bool]{"mn", "minify", false}
find = &multiFlag[bool]{"f", "find", false}
find0 = &multiFlag[bool]{"f0", "find0", false}
diff = &multiFlag[bool]{"d", "diff", false}
applyIgnore = &multiFlag[bool]{"", "apply-ignore", false}

Expand Down Expand Up @@ -71,7 +72,7 @@ var (
version = "(devel)" // to match the default from runtime/debug

allFlags = []any{
versionFlag, list, list0, write, simplify, minify, find, diff, applyIgnore,
versionFlag, list, list0, write, simplify, minify, find, find0, diff, applyIgnore,
lang, posix, filename,
indent, binNext, caseIndent, spaceRedirs, keepPadding, funcNext, toJSON, fromJSON,
}
Expand Down Expand Up @@ -159,7 +160,11 @@ Printer options:
Utilities:
-f, --find recursively find all shell files and print the paths
-f, --find recursively find all shell files and print the paths;
paths are separated by a newline
-f0, --find0 recursively find all shell files and print the paths;
paths are separated by a null character
corresponding to the -0 option of xargs
--to-json print syntax tree to stdout as a typed JSON
--from-json read syntax tree from stdin as a typed JSON
Expand Down Expand Up @@ -188,6 +193,10 @@ For more information, see 'man shfmt' and https://github.com/mvdan/sh.
fmt.Fprintf(os.Stderr, "-l and -l0 cannot coexist\n")
return 1
}
if find.val && find0.val {
fmt.Fprintf(os.Stderr, "-f and -f0 cannot coexist\n")
return 1
}
if minify.val {
simplify.val = true
}
Expand Down Expand Up @@ -255,7 +264,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 && !find0.val {
// When given paths to files directly, always format them,
// no matter their extension or shebang.
//
Expand Down Expand Up @@ -432,6 +441,10 @@ func formatPath(path string, checkShebang bool) error {
if find.val {
fmt.Println(path)
return nil
} else if find0.val {
fmt.Print(path)
fmt.Print("\000")
return nil
}
if _, err := io.CopyBuffer(&readBuf, f, copyBuf); err != nil {
return err
Expand Down
8 changes: 7 additions & 1 deletion cmd/shfmt/shfmt.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ 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.
Recursively find all shell files and print the paths;
paths are separated by a newline.

*-f0*, *--find0*
Recursively find all shell files and print the paths;
paths are separated by a null character
corresponding to the *-0* option of *xargs*.

*--to-json*
Print syntax tree to stdout as a typed JSON.
Expand Down
3 changes: 3 additions & 0 deletions cmd/shfmt/testdata/script/flags.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ stdout 'devel|v3'
! exec shfmt -l -l0
stderr 'cannot coexist'

! exec shfmt -f -f0
stderr 'cannot coexist'

! exec shfmt -ln=bash -p
stderr 'cannot coexist'

Expand Down

0 comments on commit 4ccbf3a

Please sign in to comment.