Skip to content

Commit 76edcc8

Browse files
committed
refactor(commit): redo commit message builder
Change footer for issue and breaking in data structure, rework message generation, add more params to flags
1 parent 2b1e3d7 commit 76edcc8

File tree

4 files changed

+128
-67
lines changed

4 files changed

+128
-67
lines changed

cmd/commit.go

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"log"
87

98
"github.com/urfave/cli/v3"
109

@@ -14,44 +13,71 @@ import (
1413
var ErrInvalidFlag = errors.New("invalid flag")
1514

1615
func commit() *cli.Command {
17-
var commitType string
18-
var scope string
19-
var title string
20-
var body string
21-
var footer string
16+
commit := conventional.Commit{}
2217

2318
return &cli.Command{
2419
Name: "commit",
2520
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-
},
21+
Flags: flags(&commit),
3322
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-
})
23+
commit, err := conventional.BuildCommitMessage(&commit)
4824
if err != nil {
4925
return fmt.Errorf("error building commit: %w", err)
5026
}
5127

52-
log.Println(commit)
28+
_, err = fmt.Println(commit)
29+
if err != nil {
30+
return fmt.Errorf("error printing built commit message: %w", err)
31+
}
5332

5433
return nil
5534
},
5635
}
5736
}
37+
38+
func flags(commit *conventional.Commit) []cli.Flag {
39+
return []cli.Flag{
40+
&cli.StringFlag{
41+
Name: "type",
42+
OnlyOnce: true,
43+
Destination: &commit.Type,
44+
Required: true,
45+
Usage: "Type of change (e.g., feat, fix, docs)",
46+
},
47+
&cli.StringFlag{
48+
Name: "scope",
49+
OnlyOnce: true,
50+
Destination: &commit.Scope,
51+
Required: false,
52+
Usage: "Optional context for the change (e.g., api, cli)",
53+
},
54+
&cli.StringFlag{
55+
Name: "title",
56+
OnlyOnce: true,
57+
Destination: &commit.Title,
58+
Required: true,
59+
Usage: "Short description of changes",
60+
},
61+
&cli.StringFlag{
62+
Name: "body",
63+
OnlyOnce: true,
64+
Destination: &commit.Body,
65+
Required: false,
66+
Usage: "Optional longer description of the change",
67+
},
68+
&cli.StringFlag{
69+
Name: "breaking",
70+
OnlyOnce: true,
71+
Destination: &commit.BreakingChange,
72+
Required: false,
73+
Usage: "Optional description of breaking changes introduced with commit",
74+
},
75+
&cli.StringFlag{
76+
Name: "issue",
77+
OnlyOnce: true,
78+
Destination: &commit.Issue,
79+
Required: false,
80+
Usage: "Optional issue number",
81+
},
82+
}
83+
}

internal/conventional/build_commit.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

internal/conventional/commit.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ type (
66
Commit struct {
77
// Type describes the kind of change (e.g., "feat", "fix", "docs").
88
Type string
9-
// Type describes the kind of change (e.g., "feat", "fix", "docs").
9+
// Scope is an optional context for the change (e.g., "api", "cli").
1010
Scope string
1111
// Title is a short description of the change.
1212
Title string
1313
// Body is an optional longer description.
1414
Body string
15-
// Footer is optional metadata (e.g., "BREAKING CHANGE:", issue refs).
16-
Footer string
15+
// Breaking change description.
16+
BreakingChange string
17+
// Issue number.
18+
Issue string
1719
}
1820
)

internal/conventional/message.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package conventional
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
const (
9+
newLine = "\n"
10+
)
11+
12+
// ErrRequiredPartNotPreset - error used for missing required conventional commit fields.
13+
var ErrRequiredPartNotPreset = errors.New("required part not present")
14+
15+
// BuildCommitMessage - builds conventional commit message.
16+
func BuildCommitMessage(commit *Commit) (string, error) {
17+
if commit.Type == "" {
18+
return "", fmt.Errorf("%w: type", ErrRequiredPartNotPreset)
19+
}
20+
21+
if commit.Title == "" {
22+
return "", fmt.Errorf("%w: title", ErrRequiredPartNotPreset)
23+
}
24+
25+
header := buildHeader(commit)
26+
footer := buildFooter(commit)
27+
28+
return header + newLine + footer, nil
29+
}
30+
31+
func buildHeader(commit *Commit) string {
32+
header := commit.Type
33+
34+
if commit.Scope != "" {
35+
header = fmt.Sprintf("%s(%s)", header, commit.Scope)
36+
}
37+
38+
if commit.BreakingChange != "" {
39+
header += "!"
40+
}
41+
42+
header += ": " + commit.Title
43+
44+
return header
45+
}
46+
47+
func buildFooter(commit *Commit) string {
48+
footer := ""
49+
50+
if commit.BreakingChange != "" {
51+
footer += newLine + "BREAKING CHANGE: " + commit.BreakingChange
52+
}
53+
54+
if commit.Issue != "" {
55+
footer += newLine + "Issue: " + commit.Issue
56+
}
57+
58+
if commit.Body != "" {
59+
if footer != "" {
60+
footer = newLine + commit.Body + newLine + footer + newLine
61+
} else {
62+
footer = newLine + commit.Body + newLine
63+
}
64+
} else if footer != "" {
65+
footer += newLine
66+
}
67+
68+
return footer
69+
}

0 commit comments

Comments
 (0)