Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ jobs:

- run: ./golangci-lint fmt
- run: ./golangci-lint fmt --diff
- run: cat cmd/golangci-lint/main.go | ./golangci-lint fmt --stdin

- run: ./golangci-lint cache
- run: ./golangci-lint cache status
Expand Down
2 changes: 1 addition & 1 deletion assets/cli-help.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions docs/src/docs/product/migration-guide.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2033,4 +2033,17 @@ The `version` property has been added to the configuration file.
]
```

You can also add:

```json
"go.formatTool": "custom",
"go.alternateTools": {
"customFormatter": "golangci-lint"
},
"go.formatFlags": [
"fmt",
"--stdin"
]
```

</details>
9 changes: 9 additions & 0 deletions docs/src/docs/welcome/integrations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ Recommended settings for VS Code are:
"go.lintTool": "golangci-lint",
"go.lintFlags": [
"--fast-only"
],

"go.formatTool": "custom",
"go.alternateTools": {
"customFormatter": "golangci-lint"
},
"go.formatFlags": [
"fmt",
"--stdin"
]
```

Expand Down
6 changes: 4 additions & 2 deletions pkg/commands/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (
type fmtOptions struct {
config.LoaderOptions

diff bool // Flag only.
diff bool // Flag only.
stdin bool // Flag only.
}

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

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

c.cmd = fmtCmd

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

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

opts, err := goformat.NewRunnerOptions(c.cfg, c.opts.diff)
opts, err := goformat.NewRunnerOptions(c.cfg, c.opts.diff, c.opts.stdin)
if err != nil {
return fmt.Errorf("build walk options: %w", err)
}
Expand Down
78 changes: 52 additions & 26 deletions pkg/goformat/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (c *Runner) Run(paths []string) error {
}()
}

if c.opts.stdin {
return c.process("<standard input>", savedStdout, os.Stdin)
}

for _, path := range paths {
err := c.walk(path, savedStdout)
if err != nil {
Expand Down Expand Up @@ -84,45 +88,65 @@ func (c *Runner) walk(root string, stdout *os.File) error {
return err
}

input, err := os.ReadFile(path)
in, err := os.Open(path)
if err != nil {
return err
}

match, err = c.matcher.IsGeneratedFile(path, input)
if err != nil || match {
return err
}
defer func() { _ = in.Close() }()

output := c.metaFormatter.Format(path, input)
return c.process(path, stdout, in)
})
}

if bytes.Equal(input, output) {
return nil
}
func (c *Runner) process(path string, stdout io.Writer, in io.Reader) error {
input, err := io.ReadAll(in)
if err != nil {
return err
}

if c.opts.diff {
newName := filepath.ToSlash(path)
oldName := newName + ".orig"
_, err = stdout.Write(diff.Diff(oldName, input, newName, output))
if err != nil {
return err
}
match, err := c.matcher.IsGeneratedFile(path, input)
if err != nil || match {
return err
}

c.exitCode = 1
output := c.metaFormatter.Format(path, input)

return nil
if c.opts.stdin {
_, err = stdout.Write(output)
if err != nil {
return err
}

c.log.Infof("format: %s", path)
return nil
}

if bytes.Equal(input, output) {
return nil
}

// On Windows, we need to re-set the permissions from the file. See golang/go#38225.
var perms os.FileMode
if fi, err := os.Stat(path); err == nil {
perms = fi.Mode() & os.ModePerm
if c.opts.diff {
newName := filepath.ToSlash(path)
oldName := newName + ".orig"
_, err = stdout.Write(diff.Diff(oldName, input, newName, output))
if err != nil {
return err
}

return os.WriteFile(path, output, perms)
})
c.exitCode = 1

return nil
}

c.log.Infof("format: %s", path)

// On Windows, we need to re-set the permissions from the file. See golang/go#38225.
var perms os.FileMode
if fi, err := os.Stat(path); err == nil {
perms = fi.Mode() & os.ModePerm
}

return os.WriteFile(path, output, perms)
}

func (c *Runner) setOutputToDevNull() {
Expand All @@ -144,9 +168,10 @@ type RunnerOptions struct {
patterns []*regexp.Regexp
generated string
diff bool
stdin bool
}

func NewRunnerOptions(cfg *config.Config, diff bool) (RunnerOptions, error) {
func NewRunnerOptions(cfg *config.Config, diff, stdin bool) (RunnerOptions, error) {
basePath, err := fsutils.GetBasePath(context.Background(), cfg.Run.RelativePathMode, cfg.GetConfigDir())
if err != nil {
return RunnerOptions{}, fmt.Errorf("get base path: %w", err)
Expand All @@ -156,6 +181,7 @@ func NewRunnerOptions(cfg *config.Config, diff bool) (RunnerOptions, error) {
basePath: basePath,
generated: cfg.Formatters.Exclusions.Generated,
diff: diff,
stdin: stdin,
}

for _, pattern := range cfg.Formatters.Exclusions.Paths {
Expand Down
Loading