@@ -55,6 +55,10 @@ func (c *Runner) Run(paths []string) error {
55
55
}()
56
56
}
57
57
58
+ if c .opts .stdin {
59
+ return c .process ("<standard input>" , savedStdout , os .Stdin )
60
+ }
61
+
58
62
for _ , path := range paths {
59
63
err := c .walk (path , savedStdout )
60
64
if err != nil {
@@ -84,45 +88,65 @@ func (c *Runner) walk(root string, stdout *os.File) error {
84
88
return err
85
89
}
86
90
87
- input , err := os .ReadFile (path )
91
+ in , err := os .Open (path )
88
92
if err != nil {
89
93
return err
90
94
}
91
95
92
- match , err = c .matcher .IsGeneratedFile (path , input )
93
- if err != nil || match {
94
- return err
95
- }
96
+ defer func () { _ = in .Close () }()
96
97
97
- output := c .metaFormatter .Format (path , input )
98
+ return c .process (path , stdout , in )
99
+ })
100
+ }
98
101
99
- if bytes .Equal (input , output ) {
100
- return nil
101
- }
102
+ func (c * Runner ) process (path string , stdout io.Writer , in io.Reader ) error {
103
+ input , err := io .ReadAll (in )
104
+ if err != nil {
105
+ return err
106
+ }
102
107
103
- if c .opts .diff {
104
- newName := filepath .ToSlash (path )
105
- oldName := newName + ".orig"
106
- _ , err = stdout .Write (diff .Diff (oldName , input , newName , output ))
107
- if err != nil {
108
- return err
109
- }
108
+ match , err := c .matcher .IsGeneratedFile (path , input )
109
+ if err != nil || match {
110
+ return err
111
+ }
110
112
111
- c . exitCode = 1
113
+ output := c . metaFormatter . Format ( path , input )
112
114
113
- return nil
115
+ if c .opts .stdin {
116
+ _ , err = stdout .Write (output )
117
+ if err != nil {
118
+ return err
114
119
}
115
120
116
- c .log .Infof ("format: %s" , path )
121
+ return nil
122
+ }
123
+
124
+ if bytes .Equal (input , output ) {
125
+ return nil
126
+ }
117
127
118
- // On Windows, we need to re-set the permissions from the file. See golang/go#38225.
119
- var perms os.FileMode
120
- if fi , err := os .Stat (path ); err == nil {
121
- perms = fi .Mode () & os .ModePerm
128
+ if c .opts .diff {
129
+ newName := filepath .ToSlash (path )
130
+ oldName := newName + ".orig"
131
+ _ , err = stdout .Write (diff .Diff (oldName , input , newName , output ))
132
+ if err != nil {
133
+ return err
122
134
}
123
135
124
- return os .WriteFile (path , output , perms )
125
- })
136
+ c .exitCode = 1
137
+
138
+ return nil
139
+ }
140
+
141
+ c .log .Infof ("format: %s" , path )
142
+
143
+ // On Windows, we need to re-set the permissions from the file. See golang/go#38225.
144
+ var perms os.FileMode
145
+ if fi , err := os .Stat (path ); err == nil {
146
+ perms = fi .Mode () & os .ModePerm
147
+ }
148
+
149
+ return os .WriteFile (path , output , perms )
126
150
}
127
151
128
152
func (c * Runner ) setOutputToDevNull () {
@@ -144,9 +168,10 @@ type RunnerOptions struct {
144
168
patterns []* regexp.Regexp
145
169
generated string
146
170
diff bool
171
+ stdin bool
147
172
}
148
173
149
- func NewRunnerOptions (cfg * config.Config , diff bool ) (RunnerOptions , error ) {
174
+ func NewRunnerOptions (cfg * config.Config , diff , stdin bool ) (RunnerOptions , error ) {
150
175
basePath , err := fsutils .GetBasePath (context .Background (), cfg .Run .RelativePathMode , cfg .GetConfigDir ())
151
176
if err != nil {
152
177
return RunnerOptions {}, fmt .Errorf ("get base path: %w" , err )
@@ -156,6 +181,7 @@ func NewRunnerOptions(cfg *config.Config, diff bool) (RunnerOptions, error) {
156
181
basePath : basePath ,
157
182
generated : cfg .Formatters .Exclusions .Generated ,
158
183
diff : diff ,
184
+ stdin : stdin ,
159
185
}
160
186
161
187
for _ , pattern := range cfg .Formatters .Exclusions .Paths {
0 commit comments