Skip to content

Commit

Permalink
feat: add debug command, print useful debug information
Browse files Browse the repository at this point in the history
  • Loading branch information
muthukrishnan24 committed Feb 6, 2022
1 parent b85b12f commit 963d7f8
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 42 deletions.
45 changes: 3 additions & 42 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ import (
"github.com/conventionalcommit/commitlint/lint"
)

const commitlintConfig = "COMMITLINT_CONFIG"

var configFiles = []string{
".commitlint.yml",
".commitlint.yaml",
"commitlint.yml",
"commitlint.yaml",
}

// Parse parse given file in confPath, and return Config instance, error if any
func Parse(confPath string) (*lint.Config, error) {
confPath = filepath.Clean(confPath)
Expand Down Expand Up @@ -97,12 +88,12 @@ func Validate(conf *lint.Config) []error {
// LookupAndParse gets the config path according to the precedence
// if exists, parses the config file and returns config instance
func LookupAndParse() (*lint.Config, error) {
confFilePath, useDefault, err := lookupConfigPath()
confFilePath, confType, err := internal.LookupConfigPath()
if err != nil {
return nil, err
}

if useDefault {
if confType == internal.DefaultConfig {
return defConf, nil
}

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

// 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, isDefault bool, retErr error) {
envConf := os.Getenv(commitlintConfig)
if envConf != "" {
envConf = filepath.Clean(envConf)
if _, err1 := os.Stat(envConf); !os.IsNotExist(err1) {
return envConf, false, nil
}
}

// get current directory
currentDir, err := os.Getwd()
if err != nil {
return "", false, err
}

// check if conf file exists in current directory
for _, confFile := range configFiles {
currentDirConf := filepath.Join(currentDir, confFile)
if _, err1 := os.Stat(currentDirConf); !os.IsNotExist(err1) {
return currentDirConf, false, nil
}
}

// default config
return "", true, nil
}

// WriteToFile util func to write config object to given file
func WriteToFile(outFilePath string, conf *lint.Config) (retErr error) {
outFilePath = filepath.Clean(outFilePath)
f, err := os.Create(outFilePath)
if err != nil {
return err
Expand Down
11 changes: 11 additions & 0 deletions internal/cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func newCmd() *cli.App {
lintCmd(),
configCmd(),
hookCmd(),
debugCmd(),
}

app := &cli.App{
Expand Down Expand Up @@ -185,6 +186,16 @@ func hookCmd() *cli.Command {
}
}

func debugCmd() *cli.Command {
return &cli.Command{
Name: "debug",
Usage: "prints usable information for debugging",
Action: func(ctx *cli.Context) error {
return printDebug()
},
}
}

func formConfFlag() *cli.StringFlag {
return &cli.StringFlag{
Name: "config",
Expand Down
101 changes: 101 additions & 0 deletions internal/cmd/debug.go
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
}
85 changes: 85 additions & 0 deletions internal/config.go
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
}

0 comments on commit 963d7f8

Please sign in to comment.