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