Skip to content

Commit 963d7f8

Browse files
feat: add debug command, print useful debug information
1 parent b85b12f commit 963d7f8

File tree

4 files changed

+200
-42
lines changed

4 files changed

+200
-42
lines changed

config/config.go

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ import (
1616
"github.com/conventionalcommit/commitlint/lint"
1717
)
1818

19-
const commitlintConfig = "COMMITLINT_CONFIG"
20-
21-
var configFiles = []string{
22-
".commitlint.yml",
23-
".commitlint.yaml",
24-
"commitlint.yml",
25-
"commitlint.yaml",
26-
}
27-
2819
// Parse parse given file in confPath, and return Config instance, error if any
2920
func Parse(confPath string) (*lint.Config, error) {
3021
confPath = filepath.Clean(confPath)
@@ -97,12 +88,12 @@ func Validate(conf *lint.Config) []error {
9788
// LookupAndParse gets the config path according to the precedence
9889
// if exists, parses the config file and returns config instance
9990
func LookupAndParse() (*lint.Config, error) {
100-
confFilePath, useDefault, err := lookupConfigPath()
91+
confFilePath, confType, err := internal.LookupConfigPath()
10192
if err != nil {
10293
return nil, err
10394
}
10495

105-
if useDefault {
96+
if confType == internal.DefaultConfig {
10697
return defConf, nil
10798
}
10899

@@ -113,39 +104,9 @@ func LookupAndParse() (*lint.Config, error) {
113104
return conf, nil
114105
}
115106

116-
// lookupConfigPath returns config file path following below order
117-
// 1. env path
118-
// 2. commitlint.yaml in current directory
119-
// 3. use default config
120-
func lookupConfigPath() (confPath string, isDefault bool, retErr error) {
121-
envConf := os.Getenv(commitlintConfig)
122-
if envConf != "" {
123-
envConf = filepath.Clean(envConf)
124-
if _, err1 := os.Stat(envConf); !os.IsNotExist(err1) {
125-
return envConf, false, nil
126-
}
127-
}
128-
129-
// get current directory
130-
currentDir, err := os.Getwd()
131-
if err != nil {
132-
return "", false, err
133-
}
134-
135-
// check if conf file exists in current directory
136-
for _, confFile := range configFiles {
137-
currentDirConf := filepath.Join(currentDir, confFile)
138-
if _, err1 := os.Stat(currentDirConf); !os.IsNotExist(err1) {
139-
return currentDirConf, false, nil
140-
}
141-
}
142-
143-
// default config
144-
return "", true, nil
145-
}
146-
147107
// WriteToFile util func to write config object to given file
148108
func WriteToFile(outFilePath string, conf *lint.Config) (retErr error) {
109+
outFilePath = filepath.Clean(outFilePath)
149110
f, err := os.Create(outFilePath)
150111
if err != nil {
151112
return err

internal/cmd/cli.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func newCmd() *cli.App {
1616
lintCmd(),
1717
configCmd(),
1818
hookCmd(),
19+
debugCmd(),
1920
}
2021

2122
app := &cli.App{
@@ -185,6 +186,16 @@ func hookCmd() *cli.Command {
185186
}
186187
}
187188

189+
func debugCmd() *cli.Command {
190+
return &cli.Command{
191+
Name: "debug",
192+
Usage: "prints usable information for debugging",
193+
Action: func(ctx *cli.Context) error {
194+
return printDebug()
195+
},
196+
}
197+
}
198+
188199
func formConfFlag() *cli.StringFlag {
189200
return &cli.StringFlag{
190201
Name: "config",

internal/cmd/debug.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"os"
7+
"os/exec"
8+
"strings"
9+
10+
"github.com/conventionalcommit/commitlint/internal"
11+
)
12+
13+
func printDebug() error {
14+
w := &strings.Builder{}
15+
w.WriteString("Commitlint Version: ")
16+
w.WriteString(internal.FullVersion())
17+
w.WriteByte('\n')
18+
19+
gitVer, err := getGitVersion()
20+
if err != nil {
21+
return err
22+
}
23+
24+
localConf, err := getGitHookConfig(false)
25+
if err != nil {
26+
return err
27+
}
28+
29+
globalConf, err := getGitHookConfig(true)
30+
if err != nil {
31+
return err
32+
}
33+
34+
confFile, confType, err := internal.LookupConfigPath()
35+
if err != nil {
36+
return err
37+
}
38+
39+
w.WriteString("Git Version: ")
40+
w.WriteString(gitVer)
41+
w.WriteByte('\n')
42+
43+
w.WriteString("Local Hook: ")
44+
w.WriteString(localConf)
45+
w.WriteByte('\n')
46+
47+
w.WriteString("Global Hook: ")
48+
w.WriteString(globalConf)
49+
50+
switch confType {
51+
case internal.DefaultConfig:
52+
fmt.Fprintf(w, "\nConfig: Default")
53+
case internal.FileConfig:
54+
fmt.Fprintf(w, "\nConfig: %s - %s", confType, confFile)
55+
case internal.EnvConfig:
56+
fmt.Fprintf(w, "\nConfig: %s:%s - %s", confType, internal.CommitlintConfigEnv, confFile)
57+
}
58+
59+
fmt.Println(w.String())
60+
return nil
61+
}
62+
63+
func getGitVersion() (string, error) {
64+
b := &bytes.Buffer{}
65+
66+
cmd := exec.Command("git", "version")
67+
cmd.Stdout = b
68+
cmd.Stderr = os.Stderr
69+
err := cmd.Run()
70+
if err != nil {
71+
return "", err
72+
}
73+
74+
ver := strings.ReplaceAll(b.String(), "git version ", "v")
75+
ver = strings.Trim(ver, "\n")
76+
return ver, nil
77+
}
78+
79+
func getGitHookConfig(isGlobal bool) (string, error) {
80+
b := &bytes.Buffer{}
81+
82+
var args = []string{"config"}
83+
if isGlobal {
84+
args = append(args, "--global")
85+
}
86+
args = append(args, "core.hooksPath")
87+
88+
cmd := exec.Command("git", args...)
89+
cmd.Stderr = os.Stderr
90+
cmd.Stdout = b
91+
92+
err := cmd.Run()
93+
if err != nil {
94+
return "", err
95+
}
96+
97+
s := strings.TrimSpace(b.String())
98+
s = strings.Trim(s, "\n")
99+
100+
return s, nil
101+
}

internal/config.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package internal
2+
3+
import (
4+
"errors"
5+
"os"
6+
"path/filepath"
7+
)
8+
9+
const CommitlintConfigEnv = "COMMITLINT_CONFIG"
10+
11+
const (
12+
UnknownConfig ConfigType = iota
13+
DefaultConfig
14+
EnvConfig
15+
FileConfig
16+
)
17+
18+
var configFiles = []string{
19+
".commitlint.yml",
20+
".commitlint.yaml",
21+
"commitlint.yml",
22+
"commitlint.yaml",
23+
}
24+
25+
type ConfigType byte
26+
27+
func (c ConfigType) String() string {
28+
switch c {
29+
case DefaultConfig:
30+
return "Default"
31+
case EnvConfig:
32+
return "Env"
33+
case FileConfig:
34+
return "File"
35+
default:
36+
return "Unknown"
37+
}
38+
}
39+
40+
// LookupConfigPath returns config file path following below order
41+
// 1. env path
42+
// 2. commitlint.yaml in current directory
43+
// 3. use default config
44+
func LookupConfigPath() (confPath string, typ ConfigType, err error) {
45+
envConf := os.Getenv(CommitlintConfigEnv)
46+
if envConf != "" {
47+
envConf = filepath.Clean(envConf)
48+
isExists, ferr := isFileExists(envConf)
49+
if ferr != nil {
50+
return "", UnknownConfig, err
51+
}
52+
if isExists {
53+
return envConf, EnvConfig, nil
54+
}
55+
}
56+
57+
// get current directory
58+
currentDir, err := os.Getwd()
59+
if err != nil {
60+
return "", UnknownConfig, err
61+
}
62+
63+
// check if conf file exists in current directory
64+
for _, confFile := range configFiles {
65+
currentDirConf := filepath.Join(currentDir, confFile)
66+
isExists, ferr := isFileExists(currentDirConf)
67+
if ferr != nil {
68+
return "", UnknownConfig, err
69+
}
70+
if isExists {
71+
return currentDirConf, FileConfig, nil
72+
}
73+
}
74+
75+
// default config
76+
return "", DefaultConfig, nil
77+
}
78+
79+
func isFileExists(fileName string) (bool, error) {
80+
_, err := os.Stat(fileName)
81+
if errors.Is(err, os.ErrNotExist) {
82+
return false, nil
83+
}
84+
return err == nil, err
85+
}

0 commit comments

Comments
 (0)