Skip to content

Commit 5cdf4dc

Browse files
authored
chore: Cleanup and format code. (#153)
* chore: Cleanup and format code. * chore: Cleanup and format code.
1 parent 52befb5 commit 5cdf4dc

File tree

9 files changed

+37
-35
lines changed

9 files changed

+37
-35
lines changed

cli/root.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ import (
55
gogit "github.com/go-git/go-git/v5"
66
"github.com/rs/zerolog/log"
77
"github.com/spf13/cobra"
8-
"golang.org/x/exp/slices"
9-
108
"github.com/thenativeweb/get-next-version/git"
119
"github.com/thenativeweb/get-next-version/target"
1210
"github.com/thenativeweb/get-next-version/util"
1311
"github.com/thenativeweb/get-next-version/versioning"
12+
"golang.org/x/exp/slices"
1413
)
1514

16-
var rootRepositoryFlag string
17-
var rootTargetFlag string
18-
var rootPrefixFlag string
15+
var (
16+
rootRepositoryFlag string
17+
rootTargetFlag string
18+
rootPrefixFlag string
19+
)
1920

2021
func init() {
2122
RootCommand.Flags().StringVarP(&rootRepositoryFlag, "repository", "r", ".", "sets the path to the repository")

conventionalcommits/commit_message.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import (
99
"github.com/thenativeweb/get-next-version/util"
1010
)
1111

12-
var bodyRegex *regexp.Regexp
13-
var breakingFooterTokens = []string{"BREAKING CHANGE", "BREAKING-CHANGE"}
14-
var footerTokenSeperators = []string{": ", " #"}
12+
var (
13+
bodyRegex *regexp.Regexp
14+
breakingFooterTokens = []string{"BREAKING CHANGE", "BREAKING-CHANGE"}
15+
footerTokenSeparators = []string{": ", " #"}
16+
)
1517

1618
func initCommitMessage() {
1719
typesRegexString := ""
@@ -60,8 +62,8 @@ func CommitMessageToType(message string) (Type, error) {
6062

6163
var breakingFooterPrefixes []string
6264
for _, token := range breakingFooterTokens {
63-
for _, seperator := range footerTokenSeperators {
64-
breakingFooterPrefixes = append(breakingFooterPrefixes, token+seperator)
65+
for _, separator := range footerTokenSeparators {
66+
breakingFooterPrefixes = append(breakingFooterPrefixes, token+separator)
6567
}
6668
}
6769
for _, footer := range footers {
@@ -70,18 +72,18 @@ func CommitMessageToType(message string) (Type, error) {
7072
}
7173
}
7274

73-
parsedMesageBody := bodyRegex.FindStringSubmatch(body)
74-
if parsedMesageBody == nil {
75+
parsedMessageBody := bodyRegex.FindStringSubmatch(body)
76+
if parsedMessageBody == nil {
7577
return Chore, errors.New("invalid message body for conventional commit message")
7678
}
7779

7880
breakingIndicatorIndex := util.MustFind(bodyRegex.SubexpNames(), "breaking")
79-
breakingIndicator := parsedMesageBody[breakingIndicatorIndex]
81+
breakingIndicator := parsedMessageBody[breakingIndicatorIndex]
8082
if breakingIndicator == "!" {
8183
return BreakingChange, nil
8284
}
8385

8486
typeIndex := util.MustFind(bodyRegex.SubexpNames(), "type")
8587

86-
return StringToType(parsedMesageBody[typeIndex])
88+
return StringToType(parsedMessageBody[typeIndex])
8789
}

conventionalcommits/commit_message_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestCommitMessageToType(t *testing.T) {
2828
{message: "chore:\n\nSome-Token: ", doExpectError: false, expectedCommitType: conventionalcommits.Chore},
2929
{message: "Chore:", doExpectError: false, expectedCommitType: conventionalcommits.Chore},
3030
{message: "", doExpectError: true, expectedCommitType: conventionalcommits.Chore},
31-
{message: "invaild:", doExpectError: true, expectedCommitType: conventionalcommits.Chore},
31+
{message: "invalid:", doExpectError: true, expectedCommitType: conventionalcommits.Chore},
3232
}
3333

3434
for _, test := range tests {

conventionalcommits/type.go

+13-16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package conventionalcommits
22

33
import (
44
"errors"
5+
"slices"
56
"strings"
67
)
78

@@ -14,10 +15,12 @@ const (
1415
BreakingChange
1516
)
1617

17-
var choreTypes = []string{"build", "chore", "ci", "docs", "style", "refactor", "perf", "test"}
18-
var fixTypes = []string{"fix"}
19-
var featureTypes = []string{"feat"}
20-
var allTypes []string
18+
var (
19+
choreTypes = []string{"build", "chore", "ci", "docs", "style", "refactor", "perf", "test"}
20+
fixTypes = []string{"fix"}
21+
featureTypes = []string{"feat"}
22+
allTypes []string
23+
)
2124

2225
func initType() {
2326
for _, types := range [][]string{choreTypes, fixTypes, featureTypes} {
@@ -26,22 +29,16 @@ func initType() {
2629
}
2730

2831
func StringToType(s string) (Type, error) {
29-
for _, choreType := range choreTypes {
30-
if strings.ToLower(s) == choreType {
31-
return Chore, nil
32-
}
32+
if slices.Contains(choreTypes, strings.ToLower(s)) {
33+
return Chore, nil
3334
}
3435

35-
for _, fixType := range fixTypes {
36-
if strings.ToLower(s) == fixType {
37-
return Fix, nil
38-
}
36+
if slices.Contains(fixTypes, strings.ToLower(s)) {
37+
return Fix, nil
3938
}
4039

41-
for _, featureType := range featureTypes {
42-
if strings.ToLower(s) == featureType {
43-
return Feature, nil
44-
}
40+
if slices.Contains(featureTypes, strings.ToLower(s)) {
41+
return Feature, nil
4542
}
4643

4744
return Chore, errors.New("invalid string for conventional commit type")

git/tags_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func TestGetAllSemVerTags(t *testing.T) {
4343
expectedTagNames: []string{"1.0.0", "2.0.0"},
4444
},
4545
{
46+
// The 'v' prefix in 'vsomething-else' is intentionally used here because it is the prefix for 'version'.
4647
tagsPerBranch: map[string][][]string{"main": {{"1.0.0"}, {"v2.0.0"}, {"vsomething-else"}}},
4748
doesExpectError: false,
4849
expectedTagNames: []string{"1.0.0", "2.0.0"},

target/format.go

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package target
22

33
import (
44
"fmt"
5+
56
"github.com/Masterminds/semver"
67
)
78

target/format_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/Masterminds/semver"
77
"github.com/stretchr/testify/assert"
8-
98
"github.com/thenativeweb/get-next-version/target"
109
)
1110

target/write_output_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/Masterminds/semver"
1010
"github.com/stretchr/testify/assert"
11-
1211
"github.com/thenativeweb/get-next-version/target"
1312
)
1413

version/version.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"runtime/debug"
66
)
77

8-
var Version = "(version unavailable)"
9-
var GitVersion = "(version unavailable)"
8+
var (
9+
Version = "(version unavailable)"
10+
GitVersion = "(version unavailable)"
11+
)
1012

1113
func init() {
1214
info, ok := debug.ReadBuildInfo()

0 commit comments

Comments
 (0)