Skip to content

Commit

Permalink
cmd/shfmt: add -filename to name standard input
Browse files Browse the repository at this point in the history
Otherwise, EditorConfig support is pretty useless with stdin, as we
would always use "<standard input>" as the filename.

This feature is particularly useful for editor integrations, which want
to format a real file on disk but with the in-memory contents that the
user is editing.

Start writing the changelog for 3.2.0 too.

Fixes #577.
  • Loading branch information
mvdan committed Jun 26, 2020
1 parent 9d929f2 commit fa7035e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

- **cmd/shfmt**
- Add `-filename` to give a name to standard input
- **syntax**
- Rewrite arithmetic parsing to fix operator precedence

## [3.1.1] - 2020-05-04

- **cmd/shfmt**
Expand Down
26 changes: 18 additions & 8 deletions cmd/shfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ var (
// useEditorConfig will be false if any parser or printer flags were used.
useEditorConfig = true

langStr = flag.String("ln", "", "")
posix = flag.Bool("p", false, "")
langStr = flag.String("ln", "", "")
posix = flag.Bool("p", false, "")
filename = flag.String("filename", "", "")

indent = flag.Uint("i", 0, "")
binNext = flag.Bool("bn", false, "")
Expand Down Expand Up @@ -85,8 +86,9 @@ for shell files - both by filename extension and by shebang.
Parser options:
-ln str language variant to parse (bash/posix/mksh, default "bash")
-p shorthand for -ln=posix
-ln str language variant to parse (bash/posix/mksh, default "bash")
-p shorthand for -ln=posix
-filename str provide a name for the standard input file
Printer options:
Expand Down Expand Up @@ -170,16 +172,24 @@ Utilities:
color = true
}
if flag.NArg() == 0 || (flag.NArg() == 1 && flag.Arg(0) == "-") {
if err := formatStdin(); err != nil {
name := "<standard input>"
if *filename != "" {
name = *filename
}
if err := formatStdin(name); err != nil {
if err != errChangedWithDiff {
fmt.Fprintln(os.Stderr, err)
}
return 1
}
return 0
}
if *filename != "" {
fmt.Fprintln(os.Stderr, "-filename can only be used with stdin")
return 1
}
if *toJSON {
fmt.Fprintln(os.Stderr, "-tojson can only be used with stdin/out")
fmt.Fprintln(os.Stderr, "-tojson can only be used with stdin")
return 1
}
status := 0
Expand All @@ -196,15 +206,15 @@ Utilities:

var errChangedWithDiff = fmt.Errorf("")

func formatStdin() error {
func formatStdin(name string) error {
if *write {
return fmt.Errorf("-w cannot be used on standard input")
}
src, err := ioutil.ReadAll(in)
if err != nil {
return err
}
return formatBytes(src, "<standard input>")
return formatBytes(src, name)
}

var vcsDir = regexp.MustCompile(`^\.(git|svn|hg)$`)
Expand Down
15 changes: 15 additions & 0 deletions cmd/shfmt/testdata/scripts/editorconfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ shfmt
cmp stdout input.sh.golden
! stderr .

# Verify that -filename works well with EditorConfig.
stdin stdin-filename-bash
shfmt

stdin stdin-filename-bash
! shfmt -filename=foo-posix.sh
stderr '^foo-posix.sh:.* arrays are a bash'

# Using a file path should use EditorConfig, including with the use of flags
# like -l.
shfmt input.sh
Expand Down Expand Up @@ -51,6 +59,9 @@ root = true
[*]
indent_style = space
indent_size = 3

[*-posix.sh]
shell_variant = posix
-- input.sh --
{
indented
Expand All @@ -59,6 +70,10 @@ indent_size = 3
{
indented
}
-- stdin-filename-bash --
array=(
element
)
-- morespaces/.editorconfig --
[*.sh]
indent_size = 6
Expand Down
5 changes: 4 additions & 1 deletion cmd/shfmt/testdata/scripts/flags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ stderr 'cannot coexist'
stderr 'unknown shell language'

! shfmt -tojson file
stderr 'can only be used with stdin'
stderr '-tojson can only be used with stdin'

! shfmt -filename=foo file
stderr '-filename can only be used with stdin'

# Check all the -ln variations.
stdin notbash.sh
Expand Down

0 comments on commit fa7035e

Please sign in to comment.