@@ -2,12 +2,11 @@ package commands
22
33import (
44 "fmt"
5- "io"
6- "log"
75 "os"
86 "path/filepath"
97 "strings"
108
9+ "github.com/fatih/color"
1110 "github.com/spf13/cobra"
1211 "github.com/spf13/viper"
1312
@@ -18,11 +17,17 @@ import (
1817 "github.com/golangci/golangci-lint/pkg/result/processors"
1918)
2019
20+ type fmtOptions struct {
21+ config.LoaderOptions
22+
23+ diff bool // Flag only.
24+ }
25+
2126type fmtCommand struct {
2227 viper * viper.Viper
2328 cmd * cobra.Command
2429
25- opts config. LoaderOptions
30+ opts fmtOptions
2631
2732 cfg * config.Config
2833
@@ -49,18 +54,21 @@ func newFmtCommand(logger logutils.Log, info BuildInfo) *fmtCommand {
4954 RunE : c .execute ,
5055 PreRunE : c .preRunE ,
5156 PersistentPreRunE : c .persistentPreRunE ,
57+ PersistentPostRun : c .persistentPostRun ,
5258 SilenceUsage : true ,
5359 }
5460
5561 fmtCmd .SetOut (logutils .StdOut ) // use custom output to properly color it in Windows terminals
5662 fmtCmd .SetErr (logutils .StdErr )
5763
58- flagSet := fmtCmd .Flags ()
59- flagSet .SortFlags = false // sort them as they are defined here
64+ fs := fmtCmd .Flags ()
65+ fs .SortFlags = false // sort them as they are defined here
66+
67+ setupConfigFileFlagSet (fs , & c .opts .LoaderOptions )
6068
61- setupConfigFileFlagSet ( flagSet , & c . opts )
69+ setupFormattersFlagSet ( c . viper , fs )
6270
63- setupFormattersFlagSet ( c . viper , flagSet )
71+ fs . BoolVarP ( & c . opts . diff , "diff" , "d" , false , color . GreenString ( "Display diffs instead of rewriting files" ) )
6472
6573 c .cmd = fmtCmd
6674
@@ -70,7 +78,7 @@ func newFmtCommand(logger logutils.Log, info BuildInfo) *fmtCommand {
7078func (c * fmtCommand ) persistentPreRunE (cmd * cobra.Command , args []string ) error {
7179 c .log .Infof ("%s" , c .buildInfo .String ())
7280
73- loader := config .NewLoader (c .log .Child (logutils .DebugKeyConfigReader ), c .viper , cmd .Flags (), c .opts , c .cfg , args )
81+ loader := config .NewLoader (c .log .Child (logutils .DebugKeyConfigReader ), c .viper , cmd .Flags (), c .opts . LoaderOptions , c .cfg , args )
7482
7583 err := loader .Load (config.LoadOptions {CheckDeprecation : true , Validation : true })
7684 if err != nil {
@@ -88,7 +96,7 @@ func (c *fmtCommand) preRunE(_ *cobra.Command, _ []string) error {
8896
8997 matcher := processors .NewGeneratedFileMatcher (c .cfg .Formatters .Exclusions .Generated )
9098
91- opts , err := goformat .NewRunnerOptions (c .cfg )
99+ opts , err := goformat .NewRunnerOptions (c .cfg , c . opts . diff )
92100 if err != nil {
93101 return fmt .Errorf ("build walk options: %w" , err )
94102 }
@@ -99,15 +107,6 @@ func (c *fmtCommand) preRunE(_ *cobra.Command, _ []string) error {
99107}
100108
101109func (c * fmtCommand ) execute (_ * cobra.Command , args []string ) error {
102- if ! logutils .HaveDebugTag (logutils .DebugKeyFormattersOutput ) {
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-
111110 paths , err := cleanArgs (args )
112111 if err != nil {
113112 return fmt .Errorf ("failed to clean arguments: %w" , err )
@@ -123,16 +122,10 @@ func (c *fmtCommand) execute(_ *cobra.Command, args []string) error {
123122 return nil
124123}
125124
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
125+ func (c * fmtCommand ) persistentPostRun (_ * cobra.Command , _ []string ) {
126+ if c .runner .ExitCode () != 0 {
127+ os .Exit (c .runner .ExitCode ())
132128 }
133-
134- os .Stdout , os .Stderr = devNull , devNull
135- return
136129}
137130
138131func cleanArgs (args []string ) ([]string , error ) {
0 commit comments