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