diff --git a/README.md b/README.md index 47404e5..bf1b72a 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ v0.0.3 v0.0.4 v0.0.5 -❯ tagli increment -t v0.0.5 -v patch +❯ tagli increment -t v0.0.5 --type patch Tag v0.0.6 was created ❯ git tag -l diff --git a/cmd/increment.go b/cmd/increment.go index 9c9fbbf..45b8e5e 100644 --- a/cmd/increment.go +++ b/cmd/increment.go @@ -25,17 +25,17 @@ var incrementCmd = &cobra.Command{ os.Exit(1) } - version, err := cmd.Flags().GetString("version") + versionType, err := cmd.Flags().GetString("type") if err != nil { - fmt.Println("Failed to parse --version option") + fmt.Println("Failed to parse --type option") os.Exit(1) } - incrementGitTag(tagName, incrementValue, version) + incrementGitTag(tagName, incrementValue, versionType) }, Short: "Increment a tag of Git repository", } -func incrementGitTag(tagName string, incrementValue int, version string) { +func incrementGitTag(tagName string, incrementValue int, versionType string) { repo, err := git.PlainOpen(RepositoryPath) head, err := repo.Head() if err != nil { @@ -43,7 +43,12 @@ func incrementGitTag(tagName string, incrementValue int, version string) { os.Exit(1) } - incrementTag := increment(tagName, incrementValue, version) + incrementTag, err := incrementVersion(tagName, incrementValue, versionType) + if err != nil { + fmt.Printf("increment version error: %s", err) + os.Exit(1) + } + _, err = repo.CreateTag(incrementTag, head.Hash(), nil) if err != nil { fmt.Printf("create tag error: %s", err) @@ -53,25 +58,22 @@ func incrementGitTag(tagName string, incrementValue int, version string) { os.Exit(0) } -func increment(tagName string, incrementValue int, version string) string { +func incrementVersion(tagName string, incrementValue int, versionType string) (string, error) { versionObj, err := semver.NewVersion(tagName) if err != nil { - fmt.Printf("create tag error: %s", err) - os.Exit(1) + return "", fmt.Errorf("Invalid version: %s", tagName) } - switch version { + switch versionType { case "major": - return incrementMajor(versionObj, incrementValue) + return incrementMajor(versionObj, incrementValue), nil case "minor": - return incrementMinor(versionObj, incrementValue) + return incrementMinor(versionObj, incrementValue), nil case "patch": - return incrementPatch(versionObj, incrementValue) + return incrementPatch(versionObj, incrementValue), nil default: - fmt.Println("Invalid version") - os.Exit(1) + return "", fmt.Errorf("Invalid version type: %s", versionType) } - return "" } func containsVprefix(tagName string) bool { @@ -79,7 +81,10 @@ func containsVprefix(tagName string) bool { } func incrementMajor(version *semver.Version, incrementValue int) string { - m := version.IncMajor() + var m *semver.Version = version + for i := 0; i < incrementValue; i++ { + *m = version.IncMajor() + } if containsVprefix(version.Original()) { return "v" + m.String() } @@ -87,7 +92,10 @@ func incrementMajor(version *semver.Version, incrementValue int) string { } func incrementMinor(version *semver.Version, incrementValue int) string { - m := version.IncMinor() + var m *semver.Version = version + for i := 0; i < incrementValue; i++ { + *m = version.IncMinor() + } if containsVprefix(version.Original()) { return "v" + m.String() } @@ -95,7 +103,10 @@ func incrementMinor(version *semver.Version, incrementValue int) string { } func incrementPatch(version *semver.Version, incrementValue int) string { - m := version.IncPatch() + var m *semver.Version = version + for i := 0; i < incrementValue; i++ { + *m = version.IncPatch() + } if containsVprefix(version.Original()) { return "v" + m.String() } @@ -106,9 +117,9 @@ func init() { incrementCmd.Flags().StringP("tag", "t", "", "tag name") incrementCmd.Flags().IntP("increment", "i", 1, "increment value") incrementCmd.Flags().StringP("repository", "r", ".", "repository path") - incrementCmd.Flags().StringP("version", "v", "", "version (patch, minor, major))") + incrementCmd.Flags().StringP("type", "", "", "version type (patch, minor, major))") incrementCmd.MarkFlagRequired("tag") - incrementCmd.MarkFlagRequired("version") + incrementCmd.MarkFlagRequired("type") rootCmd.AddCommand(incrementCmd) } diff --git a/cmd/increment_test.go b/cmd/increment_test.go new file mode 100644 index 0000000..95e49b1 --- /dev/null +++ b/cmd/increment_test.go @@ -0,0 +1,82 @@ +package cmd + +import ( + "testing" +) + +func TestIncrementVersion(t *testing.T) { + testCases := []struct { + name string + currentVersion string + incrementValue int + versionType string + expectedVersion string + expectedError bool + }{ + { + name: "major delta", + currentVersion: "1.0.0", + incrementValue: 1, + versionType: "major", + expectedVersion: "2.0.0", + }, + { + name: "minor delta", + currentVersion: "1.1.0", + incrementValue: 1, + versionType: "minor", + expectedVersion: "1.2.0", + }, + { + name: "patch delta", + currentVersion: "1.0.1", + incrementValue: 1, + versionType: "patch", + expectedVersion: "1.0.2", + }, + { + name: "major 2 delta", + currentVersion: "1.2.0", + incrementValue: 2, + versionType: "major", + expectedVersion: "3.0.0", + }, + { + name: "minor 2 delta", + currentVersion: "1.2.0", + incrementValue: 2, + versionType: "minor", + expectedVersion: "1.4.0", + }, + { + name: "patch 2 delta", + currentVersion: "1.2.0", + incrementValue: 2, + versionType: "patch", + expectedVersion: "1.2.2", + }, + { + name: "invalid version type", + currentVersion: "1.2", + incrementValue: 2, + versionType: "dummy", + expectedVersion: "", + expectedError: true, + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + got, err := incrementVersion(tc.currentVersion, tc.incrementValue, tc.versionType) + if got != tc.expectedVersion { + t.Errorf("expected %v: got %v", tc.expectedVersion, got) + } + if (err != nil) != tc.expectedError { + if tc.expectedError { + t.Errorf("expected an error but got no error") + } else { + t.Errorf("expected no error but got an error: %v", err) + } + } + }) + } +}