@@ -12,6 +12,50 @@ import (
1212 "github.com/spf13/cobra"
1313)
1414
15+ func (e * Executor ) persistentPostRun (cmd * cobra.Command , args []string ) {
16+ if e .cfg .Run .PrintVersion {
17+ fmt .Fprintf (printers .StdOut , "golangci-lint has version %s built from %s on %s\n " , e .version , e .commit , e .date )
18+ os .Exit (0 )
19+ }
20+
21+ runtime .GOMAXPROCS (e .cfg .Run .Concurrency )
22+
23+ log .SetFlags (0 ) // don't print time
24+ if e .cfg .Run .IsVerbose {
25+ logrus .SetLevel (logrus .InfoLevel )
26+ } else {
27+ logrus .SetLevel (logrus .WarnLevel )
28+ }
29+
30+ if e .cfg .Run .CPUProfilePath != "" {
31+ f , err := os .Create (e .cfg .Run .CPUProfilePath )
32+ if err != nil {
33+ logrus .Fatal (err )
34+ }
35+ if err := pprof .StartCPUProfile (f ); err != nil {
36+ logrus .Fatal (err )
37+ }
38+ }
39+ }
40+
41+ func (e * Executor ) persistentPreRun (cmd * cobra.Command , args []string ) {
42+ if e .cfg .Run .CPUProfilePath != "" {
43+ pprof .StopCPUProfile ()
44+ }
45+ if e .cfg .Run .MemProfilePath != "" {
46+ f , err := os .Create (e .cfg .Run .MemProfilePath )
47+ if err != nil {
48+ logrus .Fatal (err )
49+ }
50+ runtime .GC () // get up-to-date statistics
51+ if err := pprof .WriteHeapProfile (f ); err != nil {
52+ logrus .Fatal ("could not write memory profile: " , err )
53+ }
54+ }
55+
56+ os .Exit (e .exitCode )
57+ }
58+
1559func (e * Executor ) initRoot () {
1660 rootCmd := & cobra.Command {
1761 Use : "golangci-lint" ,
@@ -22,48 +66,8 @@ func (e *Executor) initRoot() {
2266 logrus .Fatal (err )
2367 }
2468 },
25- PersistentPreRun : func (cmd * cobra.Command , args []string ) {
26- if e .cfg .Run .PrintVersion {
27- fmt .Fprintf (printers .StdOut , "golangci-lint has version %s built from %s on %s\n " , e .version , e .commit , e .date )
28- os .Exit (0 )
29- }
30-
31- runtime .GOMAXPROCS (e .cfg .Run .Concurrency )
32-
33- log .SetFlags (0 ) // don't print time
34- if e .cfg .Run .IsVerbose {
35- logrus .SetLevel (logrus .InfoLevel )
36- } else {
37- logrus .SetLevel (logrus .WarnLevel )
38- }
39-
40- if e .cfg .Run .CPUProfilePath != "" {
41- f , err := os .Create (e .cfg .Run .CPUProfilePath )
42- if err != nil {
43- logrus .Fatal (err )
44- }
45- if err := pprof .StartCPUProfile (f ); err != nil {
46- logrus .Fatal (err )
47- }
48- }
49- },
50- PersistentPostRun : func (cmd * cobra.Command , args []string ) {
51- if e .cfg .Run .CPUProfilePath != "" {
52- pprof .StopCPUProfile ()
53- }
54- if e .cfg .Run .MemProfilePath != "" {
55- f , err := os .Create (e .cfg .Run .MemProfilePath )
56- if err != nil {
57- logrus .Fatal (err )
58- }
59- runtime .GC () // get up-to-date statistics
60- if err := pprof .WriteHeapProfile (f ); err != nil {
61- logrus .Fatal ("could not write memory profile: " , err )
62- }
63- }
64-
65- os .Exit (e .exitCode )
66- },
69+ PersistentPreRun : e .persistentPostRun ,
70+ PersistentPostRun : e .persistentPreRun ,
6771 }
6872 rootCmd .PersistentFlags ().BoolVarP (& e .cfg .Run .IsVerbose , "verbose" , "v" , false , "verbose output" )
6973 rootCmd .PersistentFlags ().StringVar (& e .cfg .Run .CPUProfilePath , "cpu-profile-path" , "" , "Path to CPU profile output file" )
0 commit comments