Skip to content

Commit 88b3e63

Browse files
committed
feat(commit): basic conventional commit builder
1 parent 5761ea9 commit 88b3e63

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

cmd/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ import (
66

77
var commands = []*cli.Command{
88
version(),
9+
commit(),
910
}

cmd/commit.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"log"
8+
9+
"github.com/urfave/cli/v3"
10+
11+
"github.com/isokolovskii/commitizen/internal/conventional"
12+
)
13+
14+
var ErrInvalidFlag = errors.New("invalid flag")
15+
16+
func commit() *cli.Command {
17+
var commitType string
18+
var scope string
19+
var title string
20+
var body string
21+
var footer string
22+
23+
return &cli.Command{
24+
Name: "commit",
25+
Usage: "Create Conventional Commit",
26+
Flags: []cli.Flag{
27+
&cli.StringFlag{Name: "type", OnlyOnce: true, Destination: &commitType},
28+
&cli.StringFlag{Name: "scope", OnlyOnce: true, Destination: &scope},
29+
&cli.StringFlag{Name: "title", OnlyOnce: true, Destination: &title},
30+
&cli.StringFlag{Name: "body", OnlyOnce: true, Destination: &body},
31+
&cli.StringFlag{Name: "footer", OnlyOnce: true, Destination: &footer},
32+
},
33+
Action: func(_ context.Context, _ *cli.Command) error {
34+
if commitType == "" {
35+
return fmt.Errorf("%w: commitType must be provided", ErrInvalidFlag)
36+
}
37+
if title == "" {
38+
return fmt.Errorf("%w: title mus be provided", ErrInvalidFlag)
39+
}
40+
41+
commit, err := conventional.BuildCommit(&conventional.Commit{
42+
Type: commitType,
43+
Scope: scope,
44+
Title: title,
45+
Body: body,
46+
Footer: footer,
47+
})
48+
if err != nil {
49+
return fmt.Errorf("error building commit: %w", err)
50+
}
51+
52+
log.Println(commit)
53+
54+
return nil
55+
},
56+
}
57+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package conventional
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
var ErrRequiredPartNotPreset = errors.New("required part not present")
9+
10+
func BuildCommit(commit *Commit) (string, error) {
11+
result := commit.Type
12+
13+
if commit.Type == "" {
14+
return "", fmt.Errorf("%w: type", ErrRequiredPartNotPreset)
15+
}
16+
17+
if commit.Title == "" {
18+
return "", fmt.Errorf("%w: title", ErrRequiredPartNotPreset)
19+
}
20+
21+
if commit.Scope != "" {
22+
result = fmt.Sprintf("%s(%s)", result, commit.Scope)
23+
}
24+
25+
result = fmt.Sprintf("%s: %s", result, commit.Title)
26+
27+
if commit.Body != "" {
28+
result = fmt.Sprintf("%s\n\n%s", result, commit.Body)
29+
}
30+
31+
if commit.Footer != "" {
32+
result = fmt.Sprintf("%s\n\n%s\n", result, commit.Footer)
33+
}
34+
35+
return result, nil
36+
}

internal/conventional/commit.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package conventional
2+
3+
type (
4+
Commit struct {
5+
Type string
6+
Scope string
7+
Title string
8+
Body string
9+
Footer string
10+
}
11+
)

0 commit comments

Comments
 (0)