@@ -55,6 +55,10 @@ func (c *Runner) Run(paths []string) error {
5555 }()
5656 }
5757
58+ if c .opts .stdin {
59+ return c .process ("<standard input>" , savedStdout , os .Stdin )
60+ }
61+
5862 for _ , path := range paths {
5963 err := c .walk (path , savedStdout )
6064 if err != nil {
@@ -84,45 +88,65 @@ func (c *Runner) walk(root string, stdout *os.File) error {
8488 return err
8589 }
8690
87- input , err := os .ReadFile (path )
91+ in , err := os .Open (path )
8892 if err != nil {
8993 return err
9094 }
9195
92- match , err = c .matcher .IsGeneratedFile (path , input )
93- if err != nil || match {
94- return err
95- }
96+ defer func () { _ = in .Close () }()
9697
97- output := c .metaFormatter .Format (path , input )
98+ return c .process (path , stdout , in )
99+ })
100+ }
98101
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+ }
102107
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+ }
110112
111- c . exitCode = 1
113+ output := c . metaFormatter . Format ( path , input )
112114
113- return nil
115+ if c .opts .stdin {
116+ _ , err = stdout .Write (output )
117+ if err != nil {
118+ return err
114119 }
115120
116- c .log .Infof ("format: %s" , path )
121+ return nil
122+ }
123+
124+ if bytes .Equal (input , output ) {
125+ return nil
126+ }
117127
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
122134 }
123135
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 )
126150}
127151
128152func (c * Runner ) setOutputToDevNull () {
@@ -144,9 +168,10 @@ type RunnerOptions struct {
144168 patterns []* regexp.Regexp
145169 generated string
146170 diff bool
171+ stdin bool
147172}
148173
149- func NewRunnerOptions (cfg * config.Config , diff bool ) (RunnerOptions , error ) {
174+ func NewRunnerOptions (cfg * config.Config , diff , stdin bool ) (RunnerOptions , error ) {
150175 basePath , err := fsutils .GetBasePath (context .Background (), cfg .Run .RelativePathMode , cfg .GetConfigDir ())
151176 if err != nil {
152177 return RunnerOptions {}, fmt .Errorf ("get base path: %w" , err )
@@ -156,6 +181,7 @@ func NewRunnerOptions(cfg *config.Config, diff bool) (RunnerOptions, error) {
156181 basePath : basePath ,
157182 generated : cfg .Formatters .Exclusions .Generated ,
158183 diff : diff ,
184+ stdin : stdin ,
159185 }
160186
161187 for _ , pattern := range cfg .Formatters .Exclusions .Paths {
0 commit comments