@@ -2,12 +2,11 @@ package commands
2
2
3
3
import (
4
4
"fmt"
5
- "io"
6
- "log"
7
5
"os"
8
6
"path/filepath"
9
7
"strings"
10
8
9
+ "github.com/fatih/color"
11
10
"github.com/spf13/cobra"
12
11
"github.com/spf13/viper"
13
12
@@ -18,11 +17,17 @@ import (
18
17
"github.com/golangci/golangci-lint/pkg/result/processors"
19
18
)
20
19
20
+ type fmtOptions struct {
21
+ config.LoaderOptions
22
+
23
+ diff bool // Flag only.
24
+ }
25
+
21
26
type fmtCommand struct {
22
27
viper * viper.Viper
23
28
cmd * cobra.Command
24
29
25
- opts config. LoaderOptions
30
+ opts fmtOptions
26
31
27
32
cfg * config.Config
28
33
@@ -49,18 +54,21 @@ func newFmtCommand(logger logutils.Log, info BuildInfo) *fmtCommand {
49
54
RunE : c .execute ,
50
55
PreRunE : c .preRunE ,
51
56
PersistentPreRunE : c .persistentPreRunE ,
57
+ PersistentPostRun : c .persistentPostRun ,
52
58
SilenceUsage : true ,
53
59
}
54
60
55
61
fmtCmd .SetOut (logutils .StdOut ) // use custom output to properly color it in Windows terminals
56
62
fmtCmd .SetErr (logutils .StdErr )
57
63
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 )
60
68
61
- setupConfigFileFlagSet ( flagSet , & c . opts )
69
+ setupFormattersFlagSet ( c . viper , fs )
62
70
63
- setupFormattersFlagSet ( c . viper , flagSet )
71
+ fs . BoolVarP ( & c . opts . diff , "diff" , "d" , false , color . GreenString ( "Display diffs instead of rewriting files" ) )
64
72
65
73
c .cmd = fmtCmd
66
74
@@ -70,7 +78,7 @@ func newFmtCommand(logger logutils.Log, info BuildInfo) *fmtCommand {
70
78
func (c * fmtCommand ) persistentPreRunE (cmd * cobra.Command , args []string ) error {
71
79
c .log .Infof ("%s" , c .buildInfo .String ())
72
80
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 )
74
82
75
83
err := loader .Load (config.LoadOptions {CheckDeprecation : true , Validation : true })
76
84
if err != nil {
@@ -88,7 +96,7 @@ func (c *fmtCommand) preRunE(_ *cobra.Command, _ []string) error {
88
96
89
97
matcher := processors .NewGeneratedFileMatcher (c .cfg .Formatters .Exclusions .Generated )
90
98
91
- opts , err := goformat .NewRunnerOptions (c .cfg )
99
+ opts , err := goformat .NewRunnerOptions (c .cfg , c . opts . diff )
92
100
if err != nil {
93
101
return fmt .Errorf ("build walk options: %w" , err )
94
102
}
@@ -99,15 +107,6 @@ func (c *fmtCommand) preRunE(_ *cobra.Command, _ []string) error {
99
107
}
100
108
101
109
func (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
-
111
110
paths , err := cleanArgs (args )
112
111
if err != nil {
113
112
return fmt .Errorf ("failed to clean arguments: %w" , err )
@@ -123,16 +122,10 @@ func (c *fmtCommand) execute(_ *cobra.Command, args []string) error {
123
122
return nil
124
123
}
125
124
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 ())
132
128
}
133
-
134
- os .Stdout , os .Stderr = devNull , devNull
135
- return
136
129
}
137
130
138
131
func cleanArgs (args []string ) ([]string , error ) {
0 commit comments