Skip to content

Commit 3f18d74

Browse files
committed
fix: write the input to stdout when using stdin and there are no changes
1 parent b6459b0 commit 3f18d74

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

.golangci.next.reference.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4088,6 +4088,7 @@ formatters:
40884088
# Default: lax
40894089
generated: strict
40904090
# Which file paths to exclude.
4091+
# This option is ignored when using `--stdin` as the path is unknown.
40914092
# Default: []
40924093
paths:
40934094
- ".*\\.my\\.go$"

pkg/goformat/runner.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (c *Runner) Run(paths []string) error {
5757
}
5858

5959
if c.opts.stdin {
60-
return c.process("<standard input>", savedStdout, os.Stdin)
60+
return c.formatStdIn("<standard input>", savedStdout, os.Stdin)
6161
}
6262

6363
for _, path := range paths {
@@ -121,15 +121,6 @@ func (c *Runner) process(path string, stdout io.Writer, in io.Reader) error {
121121

122122
output := c.metaFormatter.Format(path, input)
123123

124-
if c.opts.stdin {
125-
_, err = stdout.Write(output)
126-
if err != nil {
127-
return err
128-
}
129-
130-
return nil
131-
}
132-
133124
if bytes.Equal(input, output) {
134125
return nil
135126
}
@@ -168,6 +159,36 @@ func (c *Runner) process(path string, stdout io.Writer, in io.Reader) error {
168159
return os.WriteFile(path, output, perms)
169160
}
170161

162+
func (c *Runner) formatStdIn(path string, stdout io.Writer, in io.Reader) error {
163+
input, err := io.ReadAll(in)
164+
if err != nil {
165+
return err
166+
}
167+
168+
match, err := c.matcher.IsGeneratedFile(path, input)
169+
if err != nil {
170+
return err
171+
}
172+
173+
if match {
174+
_, err = stdout.Write(input)
175+
if err != nil {
176+
return err
177+
}
178+
179+
return nil
180+
}
181+
182+
output := c.metaFormatter.Format(path, input)
183+
184+
_, err = stdout.Write(output)
185+
if err != nil {
186+
return err
187+
}
188+
189+
return nil
190+
}
191+
171192
func (c *Runner) setOutputToDevNull() {
172193
devNull, err := os.Open(os.DevNull)
173194
if err != nil {

0 commit comments

Comments
 (0)