|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/spf13/viper" |
| 13 | + |
| 14 | + "github.com/golangci/golangci-lint/pkg/config" |
| 15 | + "github.com/golangci/golangci-lint/pkg/goformat" |
| 16 | + "github.com/golangci/golangci-lint/pkg/goformatters" |
| 17 | + "github.com/golangci/golangci-lint/pkg/logutils" |
| 18 | + "github.com/golangci/golangci-lint/pkg/result/processors" |
| 19 | +) |
| 20 | + |
| 21 | +type fmtCommand struct { |
| 22 | + viper *viper.Viper |
| 23 | + cmd *cobra.Command |
| 24 | + |
| 25 | + opts config.LoaderOptions |
| 26 | + |
| 27 | + cfg *config.Config |
| 28 | + |
| 29 | + buildInfo BuildInfo |
| 30 | + |
| 31 | + runner *goformat.Runner |
| 32 | + |
| 33 | + log logutils.Log |
| 34 | + debugf logutils.DebugFunc |
| 35 | +} |
| 36 | + |
| 37 | +func newFmtCommand(logger logutils.Log, info BuildInfo) *fmtCommand { |
| 38 | + c := &fmtCommand{ |
| 39 | + viper: viper.New(), |
| 40 | + log: logger, |
| 41 | + debugf: logutils.Debug(logutils.DebugKeyExec), |
| 42 | + cfg: config.NewDefault(), |
| 43 | + buildInfo: info, |
| 44 | + } |
| 45 | + |
| 46 | + fmtCmd := &cobra.Command{ |
| 47 | + Use: "fmt", |
| 48 | + Short: "Format Go source files", |
| 49 | + RunE: c.execute, |
| 50 | + PreRunE: c.preRunE, |
| 51 | + PersistentPreRunE: c.persistentPreRunE, |
| 52 | + SilenceUsage: true, |
| 53 | + } |
| 54 | + |
| 55 | + fmtCmd.SetOut(logutils.StdOut) // use custom output to properly color it in Windows terminals |
| 56 | + fmtCmd.SetErr(logutils.StdErr) |
| 57 | + |
| 58 | + flagSet := fmtCmd.Flags() |
| 59 | + flagSet.SortFlags = false // sort them as they are defined here |
| 60 | + |
| 61 | + setupConfigFileFlagSet(flagSet, &c.opts) |
| 62 | + |
| 63 | + setupFormattersFlagSet(c.viper, flagSet) |
| 64 | + |
| 65 | + c.cmd = fmtCmd |
| 66 | + |
| 67 | + return c |
| 68 | +} |
| 69 | + |
| 70 | +func (c *fmtCommand) persistentPreRunE(cmd *cobra.Command, args []string) error { |
| 71 | + c.log.Infof("%s", c.buildInfo.String()) |
| 72 | + |
| 73 | + loader := config.NewLoader(c.log.Child(logutils.DebugKeyConfigReader), c.viper, cmd.Flags(), c.opts, c.cfg, args) |
| 74 | + |
| 75 | + err := loader.Load(config.LoadOptions{CheckDeprecation: true, Validation: true}) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("can't load config: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func (c *fmtCommand) preRunE(_ *cobra.Command, _ []string) error { |
| 84 | + metaFormatter, err := goformatters.NewMetaFormatter(c.log, &c.cfg.Formatters, &c.cfg.Run) |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("failed to create meta-formatter: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + matcher := processors.NewGeneratedFileMatcher(c.cfg.Formatters.Exclusions.Generated) |
| 90 | + |
| 91 | + opts, err := goformat.NewRunnerOptions(c.cfg) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("build walk options: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + c.runner = goformat.NewRunner(c.log, metaFormatter, matcher, opts) |
| 97 | + |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +func (c *fmtCommand) execute(_ *cobra.Command, args []string) error { |
| 102 | + if !logutils.HaveDebugTag(logutils.DebugKeyLintersOutput) { |
| 103 | + // Don't allow linters and loader to print anything |
| 104 | + log.SetOutput(io.Discard) |
| 105 | + savedStdout, savedStderr := c.setOutputToDevNull() |
| 106 | + defer func() { |
| 107 | + os.Stdout, os.Stderr = savedStdout, savedStderr |
| 108 | + }() |
| 109 | + } |
| 110 | + |
| 111 | + paths, err := cleanArgs(args) |
| 112 | + if err != nil { |
| 113 | + return fmt.Errorf("failed to clean arguments: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + c.log.Infof("Formatting Go files...") |
| 117 | + |
| 118 | + err = c.runner.Run(paths) |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("failed to process files: %w", err) |
| 121 | + } |
| 122 | + |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +func (c *fmtCommand) setOutputToDevNull() (savedStdout, savedStderr *os.File) { |
| 127 | + savedStdout, savedStderr = os.Stdout, os.Stderr |
| 128 | + devNull, err := os.Open(os.DevNull) |
| 129 | + if err != nil { |
| 130 | + c.log.Warnf("Can't open null device %q: %s", os.DevNull, err) |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + os.Stdout, os.Stderr = devNull, devNull |
| 135 | + return |
| 136 | +} |
| 137 | + |
| 138 | +func cleanArgs(args []string) ([]string, error) { |
| 139 | + if len(args) == 0 { |
| 140 | + abs, err := filepath.Abs(".") |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + |
| 145 | + return []string{abs}, nil |
| 146 | + } |
| 147 | + |
| 148 | + var expended []string |
| 149 | + for _, arg := range args { |
| 150 | + abs, err := filepath.Abs(strings.ReplaceAll(arg, "...", "")) |
| 151 | + if err != nil { |
| 152 | + return nil, err |
| 153 | + } |
| 154 | + |
| 155 | + expended = append(expended, abs) |
| 156 | + } |
| 157 | + |
| 158 | + return expended, nil |
| 159 | +} |
0 commit comments