@@ -12,25 +12,24 @@ import (
12
12
"github.com/spf13/pflag"
13
13
14
14
"github.com/golangci/golangci-lint/pkg/config"
15
- "github.com/golangci/golangci-lint/pkg/exitcodes"
16
15
"github.com/golangci/golangci-lint/pkg/logutils"
17
16
)
18
17
19
- func (e * Executor ) persistentPreRun (_ * cobra.Command , _ []string ) {
18
+ func (e * Executor ) persistentPreRun (_ * cobra.Command , _ []string ) error {
20
19
if e .cfg .Run .PrintVersion {
21
- fmt .Fprintf (logutils .StdOut , "golangci-lint has version %s built from %s on %s\n " , e .version , e .commit , e .date )
22
- os . Exit ( exitcodes . Success )
20
+ _ , _ = fmt .Fprintf (logutils .StdOut , "golangci-lint has version %s built from %s on %s\n " , e .version , e .commit , e .date )
21
+ return nil
23
22
}
24
23
25
24
runtime .GOMAXPROCS (e .cfg .Run .Concurrency )
26
25
27
26
if e .cfg .Run .CPUProfilePath != "" {
28
27
f , err := os .Create (e .cfg .Run .CPUProfilePath )
29
28
if err != nil {
30
- e . log . Fatalf ( "Can 't create file %s: %s " , e .cfg .Run .CPUProfilePath , err )
29
+ return fmt . Errorf ( "can 't create file %s: %w " , e .cfg .Run .CPUProfilePath , err )
31
30
}
32
31
if err := pprof .StartCPUProfile (f ); err != nil {
33
- e . log . Fatalf ( "Can 't start CPU profiling: %s " , err )
32
+ return fmt . Errorf ( "can 't start CPU profiling: %w " , err )
34
33
}
35
34
}
36
35
@@ -43,38 +42,44 @@ func (e *Executor) persistentPreRun(_ *cobra.Command, _ []string) {
43
42
if e .cfg .Run .TracePath != "" {
44
43
f , err := os .Create (e .cfg .Run .TracePath )
45
44
if err != nil {
46
- e . log . Fatalf ( "Can 't create file %s: %s " , e .cfg .Run .TracePath , err )
45
+ return fmt . Errorf ( "can 't create file %s: %w " , e .cfg .Run .TracePath , err )
47
46
}
48
47
if err = trace .Start (f ); err != nil {
49
- e . log . Fatalf ( "Can 't start tracing: %s " , err )
48
+ return fmt . Errorf ( "can 't start tracing: %w " , err )
50
49
}
51
50
}
51
+
52
+ return nil
52
53
}
53
54
54
- func (e * Executor ) persistentPostRun (_ * cobra.Command , _ []string ) {
55
+ func (e * Executor ) persistentPostRun (_ * cobra.Command , _ []string ) error {
55
56
if e .cfg .Run .CPUProfilePath != "" {
56
57
pprof .StopCPUProfile ()
57
58
}
59
+
58
60
if e .cfg .Run .MemProfilePath != "" {
59
61
f , err := os .Create (e .cfg .Run .MemProfilePath )
60
62
if err != nil {
61
- e . log . Fatalf ( "Can 't create file %s: %s " , e .cfg .Run .MemProfilePath , err )
63
+ return fmt . Errorf ( "can 't create file %s: %w " , e .cfg .Run .MemProfilePath , err )
62
64
}
63
65
64
66
var ms runtime.MemStats
65
67
runtime .ReadMemStats (& ms )
66
68
printMemStats (& ms , e .log )
67
69
68
70
if err := pprof .WriteHeapProfile (f ); err != nil {
69
- e . log . Fatalf ( "Can 't write heap profile: %s " , err )
71
+ return fmt . Errorf ( "cCan 't write heap profile: %w " , err )
70
72
}
71
- f .Close ()
73
+ _ = f .Close ()
72
74
}
75
+
73
76
if e .cfg .Run .TracePath != "" {
74
77
trace .Stop ()
75
78
}
76
79
77
80
os .Exit (e .exitCode )
81
+
82
+ return nil
78
83
}
79
84
80
85
func printMemStats (ms * runtime.MemStats , logger logutils.Log ) {
@@ -120,16 +125,12 @@ func (e *Executor) initRoot() {
120
125
Use : "golangci-lint" ,
121
126
Short : "golangci-lint is a smart linters runner." ,
122
127
Long : `Smart, fast linters runner. Run it in cloud for every GitHub pull request on https://golangci.com` ,
123
- Run : func (cmd * cobra.Command , args []string ) {
124
- if len (args ) != 0 {
125
- e .log .Fatalf ("Usage: golangci-lint" )
126
- }
127
- if err := cmd .Help (); err != nil {
128
- e .log .Fatalf ("Can't run help: %s" , err )
129
- }
128
+ Args : cobra .NoArgs ,
129
+ RunE : func (cmd * cobra.Command , _ []string ) error {
130
+ return cmd .Help ()
130
131
},
131
- PersistentPreRun : e .persistentPreRun ,
132
- PersistentPostRun : e .persistentPostRun ,
132
+ PersistentPreRunE : e .persistentPreRun ,
133
+ PersistentPostRunE : e .persistentPostRun ,
133
134
}
134
135
135
136
initRootFlagSet (rootCmd .PersistentFlags (), e .cfg , e .needVersionOption ())
0 commit comments