Skip to content

Commit fa7035e

Browse files
committed
cmd/shfmt: add -filename to name standard input
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.
1 parent 9d929f2 commit fa7035e

4 files changed

Lines changed: 42 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
- **cmd/shfmt**
6+
- Add `-filename` to give a name to standard input
7+
- **syntax**
8+
- Rewrite arithmetic parsing to fix operator precedence
9+
510
## [3.1.1] - 2020-05-04
611

712
- **cmd/shfmt**

cmd/shfmt/main.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ var (
3838
// useEditorConfig will be false if any parser or printer flags were used.
3939
useEditorConfig = true
4040

41-
langStr = flag.String("ln", "", "")
42-
posix = flag.Bool("p", false, "")
41+
langStr = flag.String("ln", "", "")
42+
posix = flag.Bool("p", false, "")
43+
filename = flag.String("filename", "", "")
4344

4445
indent = flag.Uint("i", 0, "")
4546
binNext = flag.Bool("bn", false, "")
@@ -85,8 +86,9 @@ for shell files - both by filename extension and by shebang.
8586
8687
Parser options:
8788
88-
-ln str language variant to parse (bash/posix/mksh, default "bash")
89-
-p shorthand for -ln=posix
89+
-ln str language variant to parse (bash/posix/mksh, default "bash")
90+
-p shorthand for -ln=posix
91+
-filename str provide a name for the standard input file
9092
9193
Printer options:
9294
@@ -170,16 +172,24 @@ Utilities:
170172
color = true
171173
}
172174
if flag.NArg() == 0 || (flag.NArg() == 1 && flag.Arg(0) == "-") {
173-
if err := formatStdin(); err != nil {
175+
name := "<standard input>"
176+
if *filename != "" {
177+
name = *filename
178+
}
179+
if err := formatStdin(name); err != nil {
174180
if err != errChangedWithDiff {
175181
fmt.Fprintln(os.Stderr, err)
176182
}
177183
return 1
178184
}
179185
return 0
180186
}
187+
if *filename != "" {
188+
fmt.Fprintln(os.Stderr, "-filename can only be used with stdin")
189+
return 1
190+
}
181191
if *toJSON {
182-
fmt.Fprintln(os.Stderr, "-tojson can only be used with stdin/out")
192+
fmt.Fprintln(os.Stderr, "-tojson can only be used with stdin")
183193
return 1
184194
}
185195
status := 0
@@ -196,15 +206,15 @@ Utilities:
196206

197207
var errChangedWithDiff = fmt.Errorf("")
198208

199-
func formatStdin() error {
209+
func formatStdin(name string) error {
200210
if *write {
201211
return fmt.Errorf("-w cannot be used on standard input")
202212
}
203213
src, err := ioutil.ReadAll(in)
204214
if err != nil {
205215
return err
206216
}
207-
return formatBytes(src, "<standard input>")
217+
return formatBytes(src, name)
208218
}
209219

210220
var vcsDir = regexp.MustCompile(`^\.(git|svn|hg)$`)

cmd/shfmt/testdata/scripts/editorconfig.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ shfmt
66
cmp stdout input.sh.golden
77
! stderr .
88

9+
# Verify that -filename works well with EditorConfig.
10+
stdin stdin-filename-bash
11+
shfmt
12+
13+
stdin stdin-filename-bash
14+
! shfmt -filename=foo-posix.sh
15+
stderr '^foo-posix.sh:.* arrays are a bash'
16+
917
# Using a file path should use EditorConfig, including with the use of flags
1018
# like -l.
1119
shfmt input.sh
@@ -51,6 +59,9 @@ root = true
5159
[*]
5260
indent_style = space
5361
indent_size = 3
62+
63+
[*-posix.sh]
64+
shell_variant = posix
5465
-- input.sh --
5566
{
5667
indented
@@ -59,6 +70,10 @@ indent_size = 3
5970
{
6071
indented
6172
}
73+
-- stdin-filename-bash --
74+
array=(
75+
element
76+
)
6277
-- morespaces/.editorconfig --
6378
[*.sh]
6479
indent_size = 6

cmd/shfmt/testdata/scripts/flags.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ stderr 'cannot coexist'
1616
stderr 'unknown shell language'
1717

1818
! shfmt -tojson file
19-
stderr 'can only be used with stdin'
19+
stderr '-tojson can only be used with stdin'
20+
21+
! shfmt -filename=foo file
22+
stderr '-filename can only be used with stdin'
2023

2124
# Check all the -ln variations.
2225
stdin notbash.sh

0 commit comments

Comments
 (0)