Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
61 changes: 40 additions & 21 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,51 @@ package cmd
import (
"context"
"errors"
"fmt"
"os"

"github.com/fatih/color"
"github.com/thumbrise/commitlint-scope/cmd/commands"
"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)
}
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading