66 "fmt"
77 "go/build"
88 "go/token"
9+ "io/ioutil"
910 "log"
1011 "os"
1112 "runtime"
@@ -245,7 +246,26 @@ func (e *Executor) runAnalysis(ctx context.Context, args []string) (<-chan resul
245246 return runner .Run (ctx , linters , lintCtx ), nil
246247}
247248
249+ func setOutputToDevNull () (savedStdout , savedStderr * os.File ) {
250+ savedStdout , savedStderr = os .Stdout , os .Stderr
251+ devNull , err := os .Open (os .DevNull )
252+ if err != nil {
253+ logrus .Warnf ("can't open null device %q: %s" , os .DevNull , err )
254+ return
255+ }
256+
257+ os .Stdout , os .Stderr = devNull , devNull
258+ return
259+ }
260+
248261func (e * Executor ) runAndPrint (ctx context.Context , args []string ) error {
262+ // Don't allow linters and loader to print anything
263+ log .SetOutput (ioutil .Discard )
264+ savedStdout , savedStderr := setOutputToDevNull ()
265+ defer func () {
266+ os .Stdout , os .Stderr = savedStdout , savedStderr
267+ }()
268+
249269 issues , err := e .runAnalysis (ctx , args )
250270 if err != nil {
251271 return err
@@ -288,11 +308,11 @@ func (e *Executor) executeRun(cmd *cobra.Command, args []string) {
288308 }
289309
290310 if e .cfg .Output .PrintWelcomeMessage {
291- fmt .Println ( "Run this tool in cloud on every github pull request in https://golangci.com for free (public repos)" )
311+ fmt .Fprintln ( printers . StdOut , "Run this tool in cloud on every github pull request in https://golangci.com for free (public repos)" )
292312 }
293313
294314 if err := e .runAndPrint (ctx , args ); err != nil {
295- log . Print ( err )
315+ logrus . Warnf ( "running error: %s" , err )
296316 if e .exitCode == 0 {
297317 e .exitCode = exitCodeIfFailure
298318 }
@@ -305,11 +325,11 @@ func (e *Executor) parseConfig(cmd *cobra.Command) {
305325 if err == pflag .ErrHelp {
306326 return
307327 }
308- log .Fatalf ("Can't parse args: %s" , err )
328+ logrus .Fatalf ("Can't parse args: %s" , err )
309329 }
310330
311331 if err := viper .BindPFlags (cmd .Flags ()); err != nil {
312- log .Fatalf ("Can't bind cobra's flags to viper: %s" , err )
332+ logrus .Fatalf ("Can't bind cobra's flags to viper: %s" , err )
313333 }
314334
315335 viper .SetEnvPrefix ("GOLANGCI" )
@@ -318,7 +338,7 @@ func (e *Executor) parseConfig(cmd *cobra.Command) {
318338
319339 configFile := e .cfg .Run .Config
320340 if e .cfg .Run .NoConfig && configFile != "" {
321- log .Fatal ("can't combine option --config and --no-config" )
341+ logrus .Fatal ("can't combine option --config and --no-config" )
322342 }
323343
324344 if e .cfg .Run .NoConfig {
@@ -342,15 +362,15 @@ func (e *Executor) parseConfigImpl() {
342362 if _ , ok := err .(viper.ConfigFileNotFoundError ); ok {
343363 return
344364 }
345- log .Fatalf ("Can't read viper config: %s" , err )
365+ logrus .Fatalf ("Can't read viper config: %s" , err )
346366 }
347367
348368 if err := viper .Unmarshal (& e .cfg ); err != nil {
349- log .Fatalf ("Can't unmarshal config by viper: %s" , err )
369+ logrus .Fatalf ("Can't unmarshal config by viper: %s" , err )
350370 }
351371
352372 if err := e .validateConfig (& commandLineConfig ); err != nil {
353- log .Fatal (err )
373+ logrus .Fatal (err )
354374 }
355375}
356376
0 commit comments