Skip to content

Commit 8737317

Browse files
committed
fix: resolve golangci-lint configuration and critical issues
- Update golangci-lint action from v3 to v6 to support v2 config - Fix err113 issues by using static errors with proper wrapping - Fix errcheck issues by checking Close() return values - Fix godot issues by adding periods to comments - Fix prealloc issue by pre-allocating slice capacity
1 parent 9a875b3 commit 8737317

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
cache: true
4848

4949
- name: Run golangci-lint
50-
uses: golangci/golangci-lint-action@v3
50+
uses: golangci/golangci-lint-action@v6
5151
with:
5252
version: latest
5353
args: --timeout=5m

main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,21 @@ func installBinary(c *cli.Context) error {
444444
if err != nil {
445445
return err
446446
}
447-
defer source.Close()
447+
defer func() {
448+
if closeErr := source.Close(); closeErr != nil {
449+
fmt.Printf("Warning: failed to close source file: %v\n", closeErr)
450+
}
451+
}()
448452

449453
destination, err := os.Create(installPath)
450454
if err != nil {
451455
return err
452456
}
453-
defer destination.Close()
457+
defer func() {
458+
if closeErr := destination.Close(); closeErr != nil {
459+
fmt.Printf("Warning: failed to close destination file: %v\n", closeErr)
460+
}
461+
}()
454462

455463
_, err = io.Copy(destination, source)
456464
if err != nil {

pkg/commitrules/rules.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
package commitrules
22

33
import (
4+
"errors"
45
"fmt"
56
"regexp"
67
"strings"
78
)
89

9-
// CommitRule defines the structure for commit message rules
10+
// Define static errors for commit message validation.
11+
var (
12+
ErrInvalidFormat = errors.New("invalid commit message format")
13+
ErrMissingType = errors.New("missing commit type")
14+
ErrInvalidType = errors.New("invalid commit type")
15+
ErrTooLong = errors.New("commit message too long")
16+
)
17+
18+
// CommitRule defines the structure for commit message rules.
1019
type CommitRule struct {
1120
Type string
1221
Description string
1322
Examples []string
1423
}
1524

16-
// CommitRules holds all available commit types and their rules
25+
// CommitRules holds all available commit types and their rules.
1726
var CommitRules = map[string]CommitRule{
1827
"feat": {
1928
Type: "feat",
@@ -52,16 +61,16 @@ var CommitRules = map[string]CommitRule{
5261
},
5362
}
5463

55-
// GetCommitTypes returns all available commit types
64+
// GetCommitTypes returns all available commit types.
5665
func GetCommitTypes() []string {
57-
var types []string
66+
types := make([]string, 0, len(CommitRules))
5867
for commitType := range CommitRules {
5968
types = append(types, commitType)
6069
}
6170
return types
6271
}
6372

64-
// GetPrompt generates the commit message prompt based on analysis input
73+
// GetPrompt generates the commit message prompt based on analysis input.
6574
func GetPrompt(analysisInput string) string {
6675
commitTypesList := strings.Join(GetCommitTypes(), ", ")
6776

@@ -92,7 +101,7 @@ Git diff to analyze:
92101
return prompt
93102
}
94103

95-
// CleanCommitMessage cleans and formats the generated commit message
104+
// CleanCommitMessage cleans and formats the generated commit message.
96105
func CleanCommitMessage(message string) string {
97106
// Remove quotes and extra whitespace
98107
message = strings.Trim(message, `"'`)
@@ -144,31 +153,31 @@ func CleanCommitMessage(message string) string {
144153
return firstLine
145154
}
146155

147-
// ValidateCommitMessage validates if a commit message follows the conventional format
156+
// ValidateCommitMessage validates if a commit message follows the conventional format.
148157
func ValidateCommitMessage(message string) error {
149158
message = strings.TrimSpace(message)
150159

151160
// Check basic format type(scope): description
152161
parts := strings.SplitN(message, ":", 2)
153162
if len(parts) != 2 {
154-
return fmt.Errorf("commit message must follow format: type(scope): description")
163+
return fmt.Errorf("commit message must follow format: type(scope): description: %w", ErrInvalidFormat)
155164
}
156165

157166
// Check if type is valid
158167
typeAndScope := strings.TrimSpace(parts[0])
159168
scopeParts := strings.SplitN(typeAndScope, "(", 2)
160169
if len(scopeParts) == 0 {
161-
return fmt.Errorf("commit message must have a type")
170+
return fmt.Errorf("commit message must have a type: %w", ErrMissingType)
162171
}
163172

164173
commitType := scopeParts[0]
165174
if _, exists := CommitRules[commitType]; !exists {
166-
return fmt.Errorf("invalid commit type: %s. Valid types: %s", commitType, strings.Join(GetCommitTypes(), ", "))
175+
return fmt.Errorf("invalid commit type: %s. Valid types: %s: %w", commitType, strings.Join(GetCommitTypes(), ", "), ErrInvalidType)
167176
}
168177

169178
// Check length
170179
if len(message) > 72 {
171-
return fmt.Errorf("commit message is too long: %d characters (maximum: 72)", len(message))
180+
return fmt.Errorf("commit message is too long: %d characters (maximum: 72): %w", len(message), ErrTooLong)
172181
}
173182

174183
if len(message) > 50 {

0 commit comments

Comments
 (0)