Skip to content

Commit a768760

Browse files
scopldez
andauthored
dev: clean commands (#3007)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent 9cb17e4 commit a768760

File tree

7 files changed

+80
-107
lines changed

7 files changed

+80
-107
lines changed

pkg/commands/cache.go

+18-28
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/spf13/cobra"
99

1010
"github.com/golangci/golangci-lint/internal/cache"
11-
"github.com/golangci/golangci-lint/pkg/exitcodes"
1211
"github.com/golangci/golangci-lint/pkg/fsutils"
1312
"github.com/golangci/golangci-lint/pkg/logutils"
1413
)
@@ -17,57 +16,48 @@ func (e *Executor) initCache() {
1716
cacheCmd := &cobra.Command{
1817
Use: "cache",
1918
Short: "Cache control and information",
20-
Run: func(cmd *cobra.Command, args []string) {
21-
if len(args) != 0 {
22-
e.log.Fatalf("Usage: golangci-lint cache")
23-
}
24-
if err := cmd.Help(); err != nil {
25-
e.log.Fatalf("Can't run cache: %s", err)
26-
}
19+
Args: cobra.NoArgs,
20+
RunE: func(cmd *cobra.Command, _ []string) error {
21+
return cmd.Help()
2722
},
2823
}
2924
e.rootCmd.AddCommand(cacheCmd)
3025

3126
cacheCmd.AddCommand(&cobra.Command{
32-
Use: "clean",
33-
Short: "Clean cache",
34-
Run: e.executeCleanCache,
27+
Use: "clean",
28+
Short: "Clean cache",
29+
Args: cobra.NoArgs,
30+
ValidArgsFunction: cobra.NoFileCompletions,
31+
RunE: e.executeCleanCache,
3532
})
3633
cacheCmd.AddCommand(&cobra.Command{
37-
Use: "status",
38-
Short: "Show cache status",
39-
Run: e.executeCacheStatus,
34+
Use: "status",
35+
Short: "Show cache status",
36+
Args: cobra.NoArgs,
37+
ValidArgsFunction: cobra.NoFileCompletions,
38+
Run: e.executeCacheStatus,
4039
})
4140

4241
// TODO: add trim command?
4342
}
4443

45-
func (e *Executor) executeCleanCache(_ *cobra.Command, args []string) {
46-
if len(args) != 0 {
47-
e.log.Fatalf("Usage: golangci-lint cache clean")
48-
}
49-
44+
func (e *Executor) executeCleanCache(_ *cobra.Command, _ []string) error {
5045
cacheDir := cache.DefaultDir()
5146
if err := os.RemoveAll(cacheDir); err != nil {
52-
e.log.Fatalf("Failed to remove dir %s: %s", cacheDir, err)
47+
return fmt.Errorf("failed to remove dir %s: %w", cacheDir, err)
5348
}
5449

55-
os.Exit(exitcodes.Success)
50+
return nil
5651
}
5752

58-
func (e *Executor) executeCacheStatus(_ *cobra.Command, args []string) {
59-
if len(args) != 0 {
60-
e.log.Fatalf("Usage: golangci-lint cache status")
61-
}
62-
53+
func (e *Executor) executeCacheStatus(_ *cobra.Command, _ []string) {
6354
cacheDir := cache.DefaultDir()
6455
fmt.Fprintf(logutils.StdOut, "Dir: %s\n", cacheDir)
56+
6557
cacheSizeBytes, err := dirSizeBytes(cacheDir)
6658
if err == nil {
6759
fmt.Fprintf(logutils.StdOut, "Size: %s\n", fsutils.PrettifyBytesCount(cacheSizeBytes))
6860
}
69-
70-
os.Exit(exitcodes.Success)
7161
}
7262

7363
func dirSizeBytes(path string) (int64, error) {

pkg/commands/config.go

+9-16
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,19 @@ func (e *Executor) initConfig() {
1515
cmd := &cobra.Command{
1616
Use: "config",
1717
Short: "Config",
18-
Run: func(cmd *cobra.Command, args []string) {
19-
if len(args) != 0 {
20-
e.log.Fatalf("Usage: golangci-lint config")
21-
}
22-
if err := cmd.Help(); err != nil {
23-
e.log.Fatalf("Can't run help: %s", err)
24-
}
18+
Args: cobra.NoArgs,
19+
RunE: func(cmd *cobra.Command, _ []string) error {
20+
return cmd.Help()
2521
},
2622
}
2723
e.rootCmd.AddCommand(cmd)
2824

2925
pathCmd := &cobra.Command{
30-
Use: "path",
31-
Short: "Print used config path",
32-
Run: e.executePathCmd,
26+
Use: "path",
27+
Short: "Print used config path",
28+
Args: cobra.NoArgs,
29+
ValidArgsFunction: cobra.NoFileCompletions,
30+
Run: e.executePathCmd,
3331
}
3432
e.initRunConfiguration(pathCmd) // allow --config
3533
cmd.AddCommand(pathCmd)
@@ -52,17 +50,12 @@ func (e *Executor) getUsedConfig() string {
5250
return prettyUsedConfigFile
5351
}
5452

55-
func (e *Executor) executePathCmd(_ *cobra.Command, args []string) {
56-
if len(args) != 0 {
57-
e.log.Fatalf("Usage: golangci-lint config path")
58-
}
59-
53+
func (e *Executor) executePathCmd(_ *cobra.Command, _ []string) {
6054
usedConfigFile := e.getUsedConfig()
6155
if usedConfigFile == "" {
6256
e.log.Warnf("No config file detected")
6357
os.Exit(exitcodes.NoConfigFileDetected)
6458
}
6559

6660
fmt.Println(usedConfigFile)
67-
os.Exit(exitcodes.Success)
6861
}

pkg/commands/help.go

+9-19
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package commands
22

33
import (
44
"fmt"
5-
"os"
65
"sort"
76
"strings"
87

98
"github.com/fatih/color"
109
"github.com/spf13/cobra"
1110

12-
"github.com/golangci/golangci-lint/pkg/exitcodes"
1311
"github.com/golangci/golangci-lint/pkg/lint/linter"
1412
"github.com/golangci/golangci-lint/pkg/logutils"
1513
)
@@ -18,21 +16,19 @@ func (e *Executor) initHelp() {
1816
helpCmd := &cobra.Command{
1917
Use: "help",
2018
Short: "Help",
21-
Run: func(cmd *cobra.Command, args []string) {
22-
if len(args) != 0 {
23-
e.log.Fatalf("Usage: golangci-lint help")
24-
}
25-
if err := cmd.Help(); err != nil {
26-
e.log.Fatalf("Can't run help: %s", err)
27-
}
19+
Args: cobra.NoArgs,
20+
RunE: func(cmd *cobra.Command, _ []string) error {
21+
return cmd.Help()
2822
},
2923
}
3024
e.rootCmd.SetHelpCommand(helpCmd)
3125

3226
lintersHelpCmd := &cobra.Command{
33-
Use: "linters",
34-
Short: "Help about linters",
35-
Run: e.executeLintersHelp,
27+
Use: "linters",
28+
Short: "Help about linters",
29+
Args: cobra.NoArgs,
30+
ValidArgsFunction: cobra.NoFileCompletions,
31+
Run: e.executeLintersHelp,
3632
}
3733
helpCmd.AddCommand(lintersHelpCmd)
3834
}
@@ -64,11 +60,7 @@ func printLinterConfigs(lcs []*linter.Config) {
6460
}
6561
}
6662

67-
func (e *Executor) executeLintersHelp(_ *cobra.Command, args []string) {
68-
if len(args) != 0 {
69-
e.log.Fatalf("Usage: golangci-lint help linters")
70-
}
71-
63+
func (e *Executor) executeLintersHelp(_ *cobra.Command, _ []string) {
7264
var enabledLCs, disabledLCs []*linter.Config
7365
for _, lc := range e.DBManager.GetAllSupportedLinterConfigs() {
7466
if lc.EnabledByDefault {
@@ -93,6 +85,4 @@ func (e *Executor) executeLintersHelp(_ *cobra.Command, args []string) {
9385
sort.Strings(linterNames)
9486
fmt.Fprintf(logutils.StdOut, "%s: %s\n", color.YellowString(p), strings.Join(linterNames, ", "))
9587
}
96-
97-
os.Exit(exitcodes.Success)
9888
}

pkg/commands/linters.go

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
11
package commands
22

33
import (
4-
"log"
5-
"os"
4+
"fmt"
65

76
"github.com/fatih/color"
87
"github.com/spf13/cobra"
98

10-
"github.com/golangci/golangci-lint/pkg/exitcodes"
119
"github.com/golangci/golangci-lint/pkg/lint/linter"
1210
)
1311

1412
func (e *Executor) initLinters() {
1513
e.lintersCmd = &cobra.Command{
16-
Use: "linters",
17-
Short: "List current linters configuration",
18-
Run: e.executeLinters,
14+
Use: "linters",
15+
Short: "List current linters configuration",
16+
Args: cobra.NoArgs,
17+
ValidArgsFunction: cobra.NoFileCompletions,
18+
RunE: e.executeLinters,
1919
}
2020
e.rootCmd.AddCommand(e.lintersCmd)
2121
e.initRunConfiguration(e.lintersCmd)
2222
}
2323

2424
// executeLinters runs the 'linters' CLI command, which displays the supported linters.
25-
func (e *Executor) executeLinters(_ *cobra.Command, args []string) {
26-
if len(args) != 0 {
27-
e.log.Fatalf("Usage: golangci-lint linters")
28-
}
29-
25+
func (e *Executor) executeLinters(_ *cobra.Command, _ []string) error {
3026
enabledLintersMap, err := e.EnabledLintersSet.GetEnabledLintersMap()
3127
if err != nil {
32-
log.Fatalf("Can't get enabled linters: %s", err)
28+
return fmt.Errorf("can't get enabled linters: %w", err)
3329
}
3430

3531
color.Green("Enabled by your configuration linters:\n")
@@ -49,5 +45,5 @@ func (e *Executor) executeLinters(_ *cobra.Command, args []string) {
4945
color.Red("\nDisabled by your configuration linters:\n")
5046
printLinterConfigs(disabledLCs)
5147

52-
os.Exit(exitcodes.Success)
48+
return nil
5349
}

pkg/commands/root.go

+22-21
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,24 @@ import (
1212
"github.com/spf13/pflag"
1313

1414
"github.com/golangci/golangci-lint/pkg/config"
15-
"github.com/golangci/golangci-lint/pkg/exitcodes"
1615
"github.com/golangci/golangci-lint/pkg/logutils"
1716
)
1817

19-
func (e *Executor) persistentPreRun(_ *cobra.Command, _ []string) {
18+
func (e *Executor) persistentPreRun(_ *cobra.Command, _ []string) error {
2019
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
2322
}
2423

2524
runtime.GOMAXPROCS(e.cfg.Run.Concurrency)
2625

2726
if e.cfg.Run.CPUProfilePath != "" {
2827
f, err := os.Create(e.cfg.Run.CPUProfilePath)
2928
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)
3130
}
3231
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)
3433
}
3534
}
3635

@@ -43,38 +42,44 @@ func (e *Executor) persistentPreRun(_ *cobra.Command, _ []string) {
4342
if e.cfg.Run.TracePath != "" {
4443
f, err := os.Create(e.cfg.Run.TracePath)
4544
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)
4746
}
4847
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)
5049
}
5150
}
51+
52+
return nil
5253
}
5354

54-
func (e *Executor) persistentPostRun(_ *cobra.Command, _ []string) {
55+
func (e *Executor) persistentPostRun(_ *cobra.Command, _ []string) error {
5556
if e.cfg.Run.CPUProfilePath != "" {
5657
pprof.StopCPUProfile()
5758
}
59+
5860
if e.cfg.Run.MemProfilePath != "" {
5961
f, err := os.Create(e.cfg.Run.MemProfilePath)
6062
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)
6264
}
6365

6466
var ms runtime.MemStats
6567
runtime.ReadMemStats(&ms)
6668
printMemStats(&ms, e.log)
6769

6870
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)
7072
}
71-
f.Close()
73+
_ = f.Close()
7274
}
75+
7376
if e.cfg.Run.TracePath != "" {
7477
trace.Stop()
7578
}
7679

7780
os.Exit(e.exitCode)
81+
82+
return nil
7883
}
7984

8085
func printMemStats(ms *runtime.MemStats, logger logutils.Log) {
@@ -120,16 +125,12 @@ func (e *Executor) initRoot() {
120125
Use: "golangci-lint",
121126
Short: "golangci-lint is a smart linters runner.",
122127
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()
130131
},
131-
PersistentPreRun: e.persistentPreRun,
132-
PersistentPostRun: e.persistentPostRun,
132+
PersistentPreRunE: e.persistentPreRun,
133+
PersistentPostRunE: e.persistentPostRun,
133134
}
134135

135136
initRootFlagSet(rootCmd.PersistentFlags(), e.cfg, e.needVersionOption())

pkg/commands/run.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,11 @@ func (e *Executor) initRun() {
279279
Use: "run",
280280
Short: "Run the linters",
281281
Run: e.executeRun,
282-
PreRun: func(_ *cobra.Command, _ []string) {
282+
PreRunE: func(_ *cobra.Command, _ []string) error {
283283
if ok := e.acquireFileLock(); !ok {
284-
e.log.Fatalf("Parallel golangci-lint is running")
284+
return fmt.Errorf("parallel golangci-lint is running")
285285
}
286+
return nil
286287
},
287288
PostRun: func(_ *cobra.Command, _ []string) {
288289
e.releaseFileLock()

0 commit comments

Comments
 (0)