Skip to content

Commit 27752e8

Browse files
committed
add command 'golangci-lint config path'
1 parent cb5d1da commit 27752e8

File tree

4 files changed

+60
-7
lines changed

4 files changed

+60
-7
lines changed

pkg/commands/config.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/viper"
8+
9+
"github.com/golangci/golangci-lint/pkg/exitcodes"
10+
"github.com/golangci/golangci-lint/pkg/fsutils"
11+
12+
"github.com/spf13/cobra"
13+
)
14+
15+
func (e *Executor) initConfig() {
16+
cmd := &cobra.Command{
17+
Use: "config",
18+
Short: "Config",
19+
Run: func(cmd *cobra.Command, args []string) {
20+
if err := cmd.Help(); err != nil {
21+
e.log.Fatalf("Can't run help: %s", err)
22+
}
23+
},
24+
}
25+
e.rootCmd.AddCommand(cmd)
26+
27+
pathCmd := &cobra.Command{
28+
Use: "path",
29+
Short: "Print used config path",
30+
Run: e.executePathCmd,
31+
}
32+
e.initRunConfiguration(pathCmd) // allow --config
33+
cmd.AddCommand(pathCmd)
34+
35+
}
36+
37+
func (e Executor) executePathCmd(cmd *cobra.Command, args []string) {
38+
usedConfigFile := viper.ConfigFileUsed()
39+
if usedConfigFile == "" {
40+
e.log.Warnf("No config file detected")
41+
os.Exit(exitcodes.NoConfigFileDetected)
42+
}
43+
44+
usedConfigFile, err := fsutils.ShortestRelPath(usedConfigFile, "")
45+
if err != nil {
46+
e.log.Warnf("Can't pretty print config file path: %s", err)
47+
}
48+
49+
fmt.Println(usedConfigFile)
50+
os.Exit(0)
51+
}

pkg/commands/executor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func NewExecutor(version, commit, date string) *Executor {
5555
e.initRun()
5656
e.initHelp()
5757
e.initLinters()
58+
e.initConfig()
5859

5960
// init e.cfg by values from config: flags parse will see these values
6061
// like the default ones. It will overwrite them only if the same option

pkg/commands/help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (e *Executor) initHelp() {
2222
}
2323
},
2424
}
25-
e.rootCmd.AddCommand(helpCmd)
25+
e.rootCmd.SetHelpCommand(helpCmd)
2626

2727
lintersHelpCmd := &cobra.Command{
2828
Use: "linters",

pkg/exitcodes/exitcodes.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package exitcodes
22

33
const (
4-
Success = 0
5-
IssuesFound = 1
6-
WarningInTest = 2
7-
Failure = 3
8-
Timeout = 4
9-
NoGoFiles = 5
4+
Success = 0
5+
IssuesFound = 1
6+
WarningInTest = 2
7+
Failure = 3
8+
Timeout = 4
9+
NoGoFiles = 5
10+
NoConfigFileDetected = 6
1011
)
1112

1213
type ExitError struct {

0 commit comments

Comments
 (0)