From 79a760db9f3f99b44c071f75b02c02bf0420043b Mon Sep 17 00:00:00 2001 From: kvanzuijlen <8818390+kvanzuijlen@users.noreply.github.com> Date: Tue, 6 Feb 2024 17:28:18 +0100 Subject: [PATCH] chore: Some minor refactoring + miscellaneous fixes --- cmd/bump.go | 2 +- cmd/latest.go | 2 +- cmd/root.go | 8 +++++--- main.go | 5 +---- pkg/versioning/semver/bump.go | 4 ++-- pkg/versioning/semver/select.go | 4 ++-- 6 files changed, 12 insertions(+), 13 deletions(-) diff --git a/cmd/bump.go b/cmd/bump.go index 8af6f35..7cae322 100644 --- a/cmd/bump.go +++ b/cmd/bump.go @@ -18,7 +18,7 @@ var bumpCmd = &cobra.Command{ Long: `Bumps the specified version number with the specified version level. Can also be used to bump multiple levels with the --number flag`, RunE: func(cmd *cobra.Command, args []string) (err error) { - version, err := semver.Bump(args[0], versionLevel, numberOfVersions) + version, err := semver.Bump(args[0], versionLevel, count) if err != nil { zap.L().Error(err.Error()) return err diff --git a/cmd/latest.go b/cmd/latest.go index c29c7f9..b96b667 100644 --- a/cmd/latest.go +++ b/cmd/latest.go @@ -28,7 +28,7 @@ If a different version level is specified, this command will return the latest n if err != nil { return err } - versions := semver.Select(tags, numberOfVersions, versionLevel) + versions := semver.Select(tags, count, versionLevel) for _, version := range versions { fmt.Println(version) } diff --git a/cmd/root.go b/cmd/root.go index 094c721..1a1faea 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,7 +12,7 @@ import ( ) var ( - numberOfVersions int + count int major, minor, patch bool versionLevel string useSemver bool @@ -34,12 +34,14 @@ func Execute() { } func init() { - rootCmd.PersistentFlags().IntVarP(&numberOfVersions, "count", "c", 1, "Number of versions") + rootCmd.PersistentFlags().IntVarP(&count, "count", "c", 1, "Number of versions") rootCmd.PersistentFlags().BoolVarP(&major, "major", "M", false, "Set version level to major") rootCmd.PersistentFlags().BoolVarP(&minor, "minor", "m", false, "Set version level to minor") rootCmd.PersistentFlags().BoolVarP(&patch, "patch", "p", true, "Set version level to patch") rootCmd.MarkFlagsMutuallyExclusive("major", "minor", "patch") + rootCmd.MarkFlagsOneRequired("major", "minor", "patch") rootCmd.PersistentFlags().BoolVarP(&useSemver, "semver", "S", true, "Use semver as the versioning type") + rootCmd.MarkFlagsOneRequired("semver") } func setVersionLevel(_ *cobra.Command, _ []string) { @@ -56,6 +58,6 @@ func setVersionLevel(_ *cobra.Command, _ []string) { zap.L().Debug("Starting...", zap.String("version level", versionLevel), - zap.Int("number of versions", numberOfVersions), + zap.Int("number of versions", count), ) } diff --git a/main.go b/main.go index cdc7b66..a6b78fc 100644 --- a/main.go +++ b/main.go @@ -25,10 +25,7 @@ func init() { logger := createLogger() defer func(logger *zap.Logger) { - err := logger.Sync() - if err != nil { - panic(err) - } + _ = logger.Sync() }(logger) zap.ReplaceGlobals(logger) diff --git a/pkg/versioning/semver/bump.go b/pkg/versioning/semver/bump.go index 99afc11..dac20f5 100644 --- a/pkg/versioning/semver/bump.go +++ b/pkg/versioning/semver/bump.go @@ -8,13 +8,13 @@ import ( "go.uber.org/zap" ) -func Bump(versionNumber string, versionLevel string, numberOfVersions int) (version *semver.Version, err error) { +func Bump(versionNumber string, versionLevel string, count int) (version *semver.Version, err error) { version, err = parse(versionNumber) if err != nil { zap.L().Error("Error while parsing version", zap.String("versionNumber", versionNumber)) return nil, err } - for i := 0; i < numberOfVersions; i++ { + for i := 0; i < count; i++ { version = bumpVersion(versionLevel, version) } return version, nil diff --git a/pkg/versioning/semver/select.go b/pkg/versioning/semver/select.go index d90d8ea..0fac2e8 100644 --- a/pkg/versioning/semver/select.go +++ b/pkg/versioning/semver/select.go @@ -11,7 +11,7 @@ import ( "go.uber.org/zap" ) -func Select(tags []string, numberOfVersions int, versionLevel string) (selectedVersions []*semver.Version) { +func Select(tags []string, count int, versionLevel string) (selectedVersions []*semver.Version) { versions := semverSort(tags) versionMap := make(map[string]*semver.Version) @@ -24,7 +24,7 @@ func Select(tags []string, numberOfVersions int, versionLevel string) (selectedV if _, exists := versionMap[key]; !exists { versionMap[key] = version selectedVersions = append(selectedVersions, version) - if len(selectedVersions) == numberOfVersions { + if len(selectedVersions) == count { break } }