-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add debug command, print useful debug information
- Loading branch information
1 parent
b85b12f
commit 963d7f8
Showing
4 changed files
with
200 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 err != nil { | ||
return err | ||
} | ||
|
||
localConf, err := getGitHookConfig(false) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
globalConf, err := getGitHookConfig(true) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
confFile, confType, err := internal.LookupConfigPath() | ||
if err != 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 err != 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 err != nil { | ||
return "", err | ||
} | ||
|
||
s := strings.TrimSpace(b.String()) | ||
s = strings.Trim(s, "\n") | ||
|
||
return s, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package internal | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
const CommitlintConfigEnv = "COMMITLINT_CONFIG" | ||
|
||
const ( | ||
UnknownConfig ConfigType = iota | ||
DefaultConfig | ||
EnvConfig | ||
FileConfig | ||
) | ||
|
||
var configFiles = []string{ | ||
".commitlint.yml", | ||
".commitlint.yaml", | ||
"commitlint.yml", | ||
"commitlint.yaml", | ||
} | ||
|
||
type ConfigType byte | ||
|
||
func (c ConfigType) String() string { | ||
switch c { | ||
case DefaultConfig: | ||
return "Default" | ||
case EnvConfig: | ||
return "Env" | ||
case FileConfig: | ||
return "File" | ||
default: | ||
return "Unknown" | ||
} | ||
} | ||
|
||
// LookupConfigPath returns config file path following below order | ||
// 1. env path | ||
// 2. commitlint.yaml in current directory | ||
// 3. use default config | ||
func LookupConfigPath() (confPath string, typ ConfigType, err error) { | ||
envConf := os.Getenv(CommitlintConfigEnv) | ||
if envConf != "" { | ||
envConf = filepath.Clean(envConf) | ||
isExists, ferr := isFileExists(envConf) | ||
if ferr != nil { | ||
return "", UnknownConfig, err | ||
} | ||
if isExists { | ||
return envConf, EnvConfig, nil | ||
} | ||
} | ||
|
||
// get current directory | ||
currentDir, err := os.Getwd() | ||
if err != nil { | ||
return "", UnknownConfig, err | ||
} | ||
|
||
// check if conf file exists in current directory | ||
for _, confFile := range configFiles { | ||
currentDirConf := filepath.Join(currentDir, confFile) | ||
isExists, ferr := isFileExists(currentDirConf) | ||
if ferr != nil { | ||
return "", UnknownConfig, err | ||
} | ||
if isExists { | ||
return currentDirConf, FileConfig, nil | ||
} | ||
} | ||
|
||
// default config | ||
return "", DefaultConfig, nil | ||
} | ||
|
||
func isFileExists(fileName string) (bool, error) { | ||
_, err := os.Stat(fileName) | ||
if errors.Is(err, os.ErrNotExist) { | ||
return false, nil | ||
} | ||
return err == nil, err | ||
} |