diff --git a/Taskfile.yaml b/Taskfile.yaml index 723ef53..61ede0b 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -5,7 +5,7 @@ tasks: run: desc: Run cmds: - - go run . + - go run . {{.CLI_ARGS}} lint: desc: Run lint with auto-fix where possible (dev workflow) @@ -29,4 +29,4 @@ tasks: desc: Build cmds: - mkdir -p build - - go build -o build/ + - go build -o build/ -ldflags="-s -w -X main.version=1.0.0-local -X main.commit=aabbcc -X main.date=2006-01-02T15:04:05Z07:00" diff --git a/cmd/root.go b/cmd/root.go index b6c6ec1..93e75f3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "context" "errors" + "fmt" "os" "github.com/fatih/color" @@ -10,25 +11,43 @@ import ( "github.com/urfave/cli/v3" ) -// Root is the entry point command for commitlint-scope. -var Root = &cli.Command{ - Name: "commitlint-scope", - Description: `commitlint-scope - a linter that checks if declared commit scopes match the changed files`, - Commands: []*cli.Command{ - commands.RunCMD, - commands.InitCMD, - }, - Suggest: true, - ExitErrHandler: func(ctx context.Context, command *cli.Command, err error) { - code := 1 - - if coder, ok := errors.AsType[cli.ExitCoder](err); ok { - code = coder.ExitCode() - } - - _, _ = color.New(color.FgRed, color.Bold).Fprintf(os.Stderr, "\n%s\n", err) - _, _ = color.New(color.FgRed, color.Bold).Fprintf(os.Stderr, "\nexit code %d\n", code) - - os.Exit(code) - }, +type RootCMDLDFlags struct { + Version string + Commit string + Date string +} + +type RootCMD struct { + cmd *cli.Command +} + +func NewRootCMD(ldflags RootCMDLDFlags) *RootCMD { + return &RootCMD{ + cmd: &cli.Command{ + Name: "commitlint-scope", + Usage: "linter that checks if declared commit scopes match the changed files", + Version: fmt.Sprintf("v%s %s %s", ldflags.Version, ldflags.Commit, ldflags.Date), + Commands: []*cli.Command{ + commands.RunCMD, + commands.InitCMD, + }, + Suggest: true, + ExitErrHandler: func(ctx context.Context, command *cli.Command, err error) { + code := 1 + + if coder, ok := errors.AsType[cli.ExitCoder](err); ok { + code = coder.ExitCode() + } + + _, _ = color.New(color.FgRed, color.Bold).Fprintf(os.Stderr, "\n%s\n", err) + _, _ = color.New(color.FgRed, color.Bold).Fprintf(os.Stderr, "\nexit code %d\n", code) + + os.Exit(code) + }, + }, + } +} + +func (c *RootCMD) Run(ctx context.Context, args []string) error { + return c.cmd.Run(ctx, args) } diff --git a/main.go b/main.go index 25a98ff..1796332 100644 --- a/main.go +++ b/main.go @@ -8,10 +8,21 @@ import ( "github.com/thumbrise/commitlint-scope/cmd" ) +var ( + version = "dev" + commit = "none" + date = "unknown" +) + func main() { ctx := context.Background() - if err := cmd.Root.Run(ctx, os.Args); err != nil { + root := cmd.NewRootCMD(cmd.RootCMDLDFlags{ + Version: version, + Commit: commit, + Date: date, + }) + if err := root.Run(ctx, os.Args); err != nil { _, _ = fmt.Fprintf(os.Stderr, "fatal error: %[1]v\n", err) os.Exit(1)