@@ -57,7 +57,7 @@ func (c *Runner) Run(paths []string) error {
57
57
}
58
58
59
59
if c .opts .stdin {
60
- return c .process ("<standard input>" , savedStdout , os .Stdin )
60
+ return c .formatStdIn ("<standard input>" , savedStdout , os .Stdin )
61
61
}
62
62
63
63
for _ , path := range paths {
@@ -121,15 +121,6 @@ func (c *Runner) process(path string, stdout io.Writer, in io.Reader) error {
121
121
122
122
output := c .metaFormatter .Format (path , input )
123
123
124
- if c .opts .stdin {
125
- _ , err = stdout .Write (output )
126
- if err != nil {
127
- return err
128
- }
129
-
130
- return nil
131
- }
132
-
133
124
if bytes .Equal (input , output ) {
134
125
return nil
135
126
}
@@ -168,6 +159,38 @@ func (c *Runner) process(path string, stdout io.Writer, in io.Reader) error {
168
159
return os .WriteFile (path , output , perms )
169
160
}
170
161
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
+ // If the file is generated,
175
+ // the input should be written to the stdout to avoid emptied the file.
176
+ _ , err = stdout .Write (input )
177
+ if err != nil {
178
+ return err
179
+ }
180
+
181
+ return nil
182
+ }
183
+
184
+ output := c .metaFormatter .Format (path , input )
185
+
186
+ _ , err = stdout .Write (output )
187
+ if err != nil {
188
+ return err
189
+ }
190
+
191
+ return nil
192
+ }
193
+
171
194
func (c * Runner ) setOutputToDevNull () {
172
195
devNull , err := os .Open (os .DevNull )
173
196
if err != nil {
0 commit comments