Skip to content

Commit 5bffd77

Browse files
committed
feat: add option stdin for fmt command
1 parent 92bcb9a commit 5bffd77

File tree

3 files changed

+65
-28
lines changed

3 files changed

+65
-28
lines changed

Diff for: docs/src/docs/welcome/integrations.mdx

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ Recommended settings for VS Code are:
1212
"go.lintTool": "golangci-lint",
1313
"go.lintFlags": [
1414
"--fast-only"
15+
],
16+
17+
"go.formatTool": "custom",
18+
"go.alternateTools": {
19+
"customFormatter": "golangci-lint"
20+
},
21+
"go.formatFlags": [
22+
"fmt",
23+
"--stdin"
1524
]
1625
```
1726

Diff for: pkg/commands/fmt.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import (
2020
type fmtOptions struct {
2121
config.LoaderOptions
2222

23-
diff bool // Flag only.
23+
diff bool // Flag only.
24+
stdin bool // Flag only.
2425
}
2526

2627
type fmtCommand struct {
@@ -69,6 +70,7 @@ func newFmtCommand(logger logutils.Log, info BuildInfo) *fmtCommand {
6970
setupFormattersFlagSet(c.viper, fs)
7071

7172
fs.BoolVarP(&c.opts.diff, "diff", "d", false, color.GreenString("Display diffs instead of rewriting files"))
73+
fs.BoolVar(&c.opts.stdin, "stdin", false, color.GreenString("Use standard input"))
7274

7375
c.cmd = fmtCmd
7476

@@ -100,7 +102,7 @@ func (c *fmtCommand) preRunE(_ *cobra.Command, _ []string) error {
100102

101103
matcher := processors.NewGeneratedFileMatcher(c.cfg.Formatters.Exclusions.Generated)
102104

103-
opts, err := goformat.NewRunnerOptions(c.cfg, c.opts.diff)
105+
opts, err := goformat.NewRunnerOptions(c.cfg, c.opts.diff, c.opts.stdin)
104106
if err != nil {
105107
return fmt.Errorf("build walk options: %w", err)
106108
}

Diff for: pkg/goformat/runner.go

+52-26
Original file line numberDiff line numberDiff line change
@@ -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

128152
func (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

Comments
 (0)