Skip to content

Commit

Permalink
feat: separate cli commands and callbacks
Browse files Browse the repository at this point in the history
* rename New to NewWith
* add New which calls NewWith with empty version info
* remove version command, set version info to cli.App
  • Loading branch information
muthukrishnan24 committed Sep 22, 2021
1 parent 020f9a7 commit 28325db
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 99 deletions.
43 changes: 17 additions & 26 deletions cmd/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ const (
HookDir = ".commitlint/hooks"
)

func initCallback(ctx *cli.Context) (retErr error) {
isGlobal := ctx.Bool("global")

// Init is the callback function for init command
func Init(isGlobal bool) error {
hookDir, err := getHookDir(isGlobal)
if err != nil {
return err
Expand All @@ -46,11 +45,10 @@ func initCallback(ctx *cli.Context) (retErr error) {
return nil
}

func lintCallback(ctx *cli.Context) error {
confFilePath := ctx.String("config")
fileInput := ctx.String("message")

resStr, hasError, err := runLint(confFilePath, fileInput)
// Lint is the callback function for lint command
func Lint(confPath, msgPath string) error {
// NOTE: lint should return with exit code for error case
resStr, hasError, err := runLint(confPath, msgPath)
if err != nil {
return cli.Exit(err, ErrExitCode)
}
Expand All @@ -59,33 +57,26 @@ func lintCallback(ctx *cli.Context) error {
return cli.Exit(resStr, ErrExitCode)
}

// print success message
fmt.Println(resStr)
return nil
}

func hookCreateCallback(ctx *cli.Context) (retErr error) {
err := hook.WriteToFile(".")
if err != nil {
return cli.Exit(err, ErrExitCode)
}
return nil
// CreateHook is the callback function for create hook command
func CreateHook() (retErr error) {
return hook.WriteToFile(".")
}

func configCreateCallback(ctx *cli.Context) error {
isOnlyEnabled := ctx.Bool("enabled")
err := config.DefaultConfToFile(isOnlyEnabled)
if err != nil {
return cli.Exit(err, ErrExitCode)
}
return nil
// CreateConfig is the callback function for create config command
func CreateConfig(onlyEnabled bool) error {
return config.DefaultConfToFile(onlyEnabled)
}

func verifyCallback(ctx *cli.Context) error {
confFlag := ctx.String("config")

// VerifyConfig is the callback function for verify command
func VerifyConfig(confFlag string) error {
confPath, useDefault, err := config.GetConfigPath(confFlag)
if err != nil {
return cli.Exit(err, ErrExitCode)
return err
}

if useDefault {
Expand All @@ -95,7 +86,7 @@ func verifyCallback(ctx *cli.Context) error {

_, _, err = getLinter(confPath)
if err != nil {
return cli.Exit(err, ErrExitCode)
return err
}

fmt.Printf("%s config is valid\n", confPath)
Expand Down
124 changes: 52 additions & 72 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,91 +2,92 @@
package cmd

import (
"fmt"
"runtime/debug"

"github.com/urfave/cli/v2"
)

// New returns commitlint cli.App
func New(versionNo, commitHash, builtTime string) *cli.App {
func New() *cli.App {
return NewWith("", "", "")
}

// NewWith returns commitlint cli.App with version info
func NewWith(versionNo, commitHash, builtTime string) *cli.App {
versionInfo := formVersionInfo(versionNo, commitHash, builtTime)

cmds := []*cli.Command{
createCmd(),
initCmd(),
lintCmd(),
createCmd(),
verifyCmd(),
versionCmd(versionInfo),
}

app := &cli.App{
Name: "commitlint",
Usage: "linter for conventional commits",
Commands: cmds,
Action: nil,
Version: versionInfo,
}
return app
}

func versionCmd(versionInfo string) *cli.Command {
return &cli.Command{
Name: "version",
Usage: "prints commitlint version",
Action: func(c *cli.Context) error {
fmt.Printf(versionInfo)
return nil
},
}
}

func initCmd() *cli.Command {
return &cli.Command{
Name: "init",
Usage: "setup commitlint for git repos",
Action: initCallback,
Name: "init",
Usage: "setup commitlint for git repos",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "global",
Aliases: []string{"g"},
Usage: "sets git hook in global config",
},
},
Action: func(ctx *cli.Context) error {
isGlobal := ctx.Bool("global")
return Init(isGlobal)
},
}
}

func createCmd() *cli.Command {
configCmd := &cli.Command{
Name: "config",
Usage: "creates commitlint.yaml in current directory",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "enabled",
Aliases: []string{"e"},
Usage: "writes only default enabled rules to file",
Value: false,
},
},
Action: func(ctx *cli.Context) error {
isOnlyEnabled := ctx.Bool("enabled")
return CreateConfig(isOnlyEnabled)
},
}

hookCmd := &cli.Command{
Name: "hook",
Usage: "creates commit-msg file in current directory",
Action: func(ctx *cli.Context) error {
return CreateHook()
},
}

return &cli.Command{
Name: "create",
Usage: "create commitlint config, hooks files",
Subcommands: []*cli.Command{
{
Name: "config",
Usage: "creates commitlint.yaml in current directory",
Action: configCreateCallback,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "enabled",
Aliases: []string{"e"},
Usage: "writes only default enabled rules to file",
Value: false,
},
},
},
{
Name: "hook",
Usage: "creates commit-msg file in current directory",
Action: hookCreateCallback,
},
configCmd,
hookCmd,
},
}
}

func lintCmd() *cli.Command {
return &cli.Command{
Name: "lint",
Usage: "lints commit message",
Action: lintCallback,
Name: "lint",
Usage: "lints commit message",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Expand All @@ -101,6 +102,11 @@ func lintCmd() *cli.Command {
Usage: "path to git commit message `FILE`",
},
},
Action: func(ctx *cli.Context) error {
confFilePath := ctx.String("config")
fileInput := ctx.String("message")
return Lint(confFilePath, fileInput)
},
}
}

Expand All @@ -116,35 +122,9 @@ func verifyCmd() *cli.Command {
Usage: "optional config file `conf.yaml`",
},
},
Action: verifyCallback,
}
}

func formVersionInfo(versionInfo, commitInfo, buildTime string) string {
versionTmpl := `commitlint version %s - built from %s on %s
`
versionInfo, commitInfo, buildTime = getVersionInfo(versionInfo, commitInfo, buildTime)
return fmt.Sprintf(versionTmpl, versionInfo, commitInfo, buildTime)
}

func getVersionInfo(version, commit, build string) (versionInfo, commitInfo, buildTime string) {
if build != "" {
return version, commit, build
}

info, ok := debug.ReadBuildInfo()
if !ok {
return "master", "unknown", "unknown"
}

checkSum := "unknown"
if info.Main.Sum != "" {
checkSum = info.Main.Sum
Action: func(ctx *cli.Context) error {
confFilePath := ctx.String("config")
return VerifyConfig(confFilePath)
},
}

versionInfo = info.Main.Version
commitInfo = "(" + "checksum: " + checkSum + ")"
buildTime = "unknown"

return versionInfo, commitInfo, buildTime
}
24 changes: 24 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package cmd

import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime/debug"
"strings"

"github.com/conventionalcommit/commitlint/config"
Expand Down Expand Up @@ -150,3 +152,25 @@ func getRepoRootDir() (string, error) {

return gitDir, nil
}

func formVersionInfo(version, commit, build string) string {
versionTmpl := "%s - built from %s on %s"

if build != "" {
return fmt.Sprintf(versionTmpl, version, commit, build)
}

info, ok := debug.ReadBuildInfo()
if !ok {
return fmt.Sprintf(versionTmpl, "master", "unknown", "unknown")
}

var commitInfo string
if info.Main.Sum == "" {
commitInfo = "(" + "checksum: unknown)"
} else {
commitInfo = "(" + "checksum: " + info.Main.Sum + ")"
}

return fmt.Sprintf(versionTmpl, info.Main.Version, commitInfo, "unknown")
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
)

func main() {
app := cmd.New(Version, Commit, BuildTime)
app := cmd.NewWith(Version, Commit, BuildTime)
err := app.Run(os.Args)
if err != nil {
fmt.Println("Error:", err)
Expand Down

0 comments on commit 28325db

Please sign in to comment.