Skip to content

Commit

Permalink
Make semver packager usable as a golang library (#8)
Browse files Browse the repository at this point in the history
* move sever code from 'internal' to 'pkg'

* refactor semver API visibility

* WIP on making 'semver' API for public use as a library

* WIP on making 'semver' API for public use as a library

* WIP on making 'semver' API for public use as a library

* api documentation

* add package doc

* clean up tests and godoc

* more cleanup

* remove redundant code

* fix init template

* fix 'go vet' command
  • Loading branch information
ptgoetz authored Nov 11, 2024
1 parent c4e2c41 commit 6c9abf5
Show file tree
Hide file tree
Showing 17 changed files with 762 additions and 699 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ tidy: ## Tidy up go.mod and remove unused dependencies
lint: ## Lint the Go source files
@echo "Linting..."
golangci-lint run
go vet ./...


format: ## Format all Go source files
@echo "Formatting Go source files..."
Expand Down
30 changes: 15 additions & 15 deletions cmd/versionbump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"github.com/ptgoetz/go-versionbump/internal"
vbc "github.com/ptgoetz/go-versionbump/internal/config"
"github.com/ptgoetz/go-versionbump/internal/semver"
"github.com/ptgoetz/go-versionbump/pkg/semver"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"os"
Expand All @@ -27,22 +27,22 @@ var rootCmd = &cobra.Command{

var majorCmd = &cobra.Command{
Use: "major",
Short: `Bump the major version number (e.g. 1.2.3 -> 2.0.0).`,
Long: `Bump the major version number (e.g. 1.2.3 -> 2.0.0).`,
Short: `bump the major version number (e.g. 1.2.3 -> 2.0.0).`,
Long: `bump the major version number (e.g. 1.2.3 -> 2.0.0).`,
RunE: bumpMajor, // Use RunE for better error handling
}

var minorCmd = &cobra.Command{
Use: "minor",
Short: `Bump the minor version number (e.g. 1.2.3 -> 1.3.0).`,
Long: `Bump the minor version number (e.g. 1.2.3 -> 1.3.0).`,
Short: `bump the minor version number (e.g. 1.2.3 -> 1.3.0).`,
Long: `bump the minor version number (e.g. 1.2.3 -> 1.3.0).`,
RunE: bumpMinor, // Use RunE for better error handling
}

var patchCmd = &cobra.Command{
Use: "patch",
Short: `Bump the patch version number (e.g. 1.2.3 -> 1.2.4).`,
Long: `Bump the patch version number (e.g. 1.2.3 -> 1.2.4).`,
Short: `bump the patch version number (e.g. 1.2.3 -> 1.2.4).`,
Long: `bump the patch version number (e.g. 1.2.3 -> 1.2.4).`,
RunE: bumpPatch, // Use RunE for better error handling
}

Expand Down Expand Up @@ -130,36 +130,36 @@ var gitTagHistoryCmd = &cobra.Command{

var preReleaseNextCmd = &cobra.Command{
Use: "prerelease",
Short: `Bump the next pre-release version label (e.g. 1.2.3-alpha -> 1.2.3-beta).`,
Long: `Bump the patch version number (e.g. 1.2.3 -> 1.2.4).`,
Short: `bump the next pre-release version label (e.g. 1.2.3-alpha -> 1.2.3-beta).`,
Long: `bump the patch version number (e.g. 1.2.3 -> 1.2.4).`,
RunE: bumpPreReleaseNext, // Use RunE for better error handling
}

var preReleaseMajorCmd = &cobra.Command{
Use: "prerelease-major",
Short: `Bump the pre-release major version number (e.g. 1.2.3-alpha -> 1.2.3-alpha.1).`,
Short: `bump the pre-release major version number (e.g. 1.2.3-alpha -> 1.2.3-alpha.1).`,
Long: `ump the pre-release major version number (e.g. 1.2.3-alpha -> 1.2.3-alpha.1).`,
RunE: bumpPreReleaseMajor, // Use RunE for better error handling
}

var preReleaseMinorCmd = &cobra.Command{
Use: "prerelease-minor",
Short: `Bump the pre-release minor version number (e.g. 1.2.3-alpha -> 1.2.3-alpha.0.1).`,
Short: `bump the pre-release minor version number (e.g. 1.2.3-alpha -> 1.2.3-alpha.0.1).`,
Long: `ump the pre-release minor version number (e.g. 1.2.3-alpha -> 1.2.3-alpha.0.1).`,
RunE: bumpPreReleaseMinor, // Use RunE for better error handling
}

var preReleasePatchCmd = &cobra.Command{
Use: "prerelease-patch",
Short: `Bump the pre-release patch version number (e.g. 1.2.3-alpha -> 1.2.3-alpha.0.0.1).`,
Short: `bump the pre-release patch version number (e.g. 1.2.3-alpha -> 1.2.3-alpha.0.0.1).`,
Long: `ump the pre-release patch version number (e.g. 1.2.3-alpha -> 1.2.3-alpha.0.0.1).`,
RunE: bumpPreReleasePatch, // Use RunE for better error handling
}

var preReleaseBuildCmd = &cobra.Command{
Use: "prerelease-build",
Short: `Bump the pre-release build version number (e.g. 1.2.3 -> 1.2.3+build.1).`,
Long: `Bump the pre-release build version number (e.g. 1.2.3 -> 1.2.3+build.1).`,
Short: `bump the pre-release build version number (e.g. 1.2.3 -> 1.2.3+build.1).`,
Long: `bump the pre-release build version number (e.g. 1.2.3 -> 1.2.3+build.1).`,
RunE: bumpPreReleaseBuild, // Use RunE for better error handling
}

Expand Down Expand Up @@ -241,7 +241,7 @@ func bumpPatch(cmd *cobra.Command, args []string) error {
}

func bumpPreReleaseNext(cmd *cobra.Command, args []string) error {
return runVersionBump(semver.PreReleaseNext)
return runVersionBump(semver.PreRelease)
}

func bumpPreReleaseMajor(cmd *cobra.Command, args []string) error {
Expand Down
26 changes: 24 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ package config

import (
"fmt"
"github.com/ptgoetz/go-versionbump/internal/semver"
"github.com/ptgoetz/go-versionbump/internal/utils"
"github.com/ptgoetz/go-versionbump/pkg/semver"
"gopkg.in/yaml.v3"
"os"
"path"
"sort"
)

const (
DefaultGitCommitTemplate = "Bump version {old} --> {new}"
DefaultGitCommitTemplate = "bump version {old} --> {new}"
DefaultGitTagTemplate = "v{new}"
DefaultGitTagMessageTemplate = "Release version {new}"
DefaultBuildLabel = "build"
)

var (
DetaultPreReleaseLabels = []string{"alpha", "beta", "rc"}
DefaultVersion = "0.0.0"
)

// Config represents the version bump configuration.
Expand Down Expand Up @@ -164,3 +170,19 @@ func LoadConfig(filePath string) (*Config, string, error) {
}
return configPtr, root, nil
}

// NewConfig creates a new Config instance with default values
func NewConfig() *Config {
return &Config{
Version: DefaultVersion,
BuildLabel: DefaultBuildLabel,
PreReleaseLabels: DetaultPreReleaseLabels,
GitCommit: false,
GitCommitTemplate: DefaultGitCommitTemplate,
GitSign: false,
GitTag: false,
GitTagTemplate: DefaultGitTagTemplate,
GitTagMessageTemplate: DefaultGitTagMessageTemplate,
Files: []VersionedFile{},
}
}
6 changes: 3 additions & 3 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ files:
- "v{version}"
- path: "README.md"
replace:
- "Version: {version}"
- "version: {version}"
`
if err := os.WriteFile(filePath, []byte(yamlContent), 0644); err != nil {
t.Fatalf("Failed to write to YAML config file: %v", err)
Expand Down Expand Up @@ -56,7 +56,7 @@ files:
if config.Files[0].Path != "version.go" || config.Files[0].Replace[0] != "v{version}" {
t.Errorf("Unexpected file config for 'version.go': %+v", config.Files[0])
}
if config.Files[1].Path != "README.md" || config.Files[1].Replace[0] != "Version: {version}" {
if config.Files[1].Path != "README.md" || config.Files[1].Replace[0] != "version: {version}" {
t.Errorf("Unexpected file config for 'README.md': %+v", config.Files[1])
}

Expand Down Expand Up @@ -98,7 +98,7 @@ files:
- path: "version.go"
replace: "v{version}"
- path: "README.md"
replace: "Version: {version"
replace: "version: {version"
`
if err := os.WriteFile(filePath, []byte(invalidYamlContent), 0644); err != nil {
t.Fatalf("Failed to write to YAML config file: %v", err)
Expand Down
82 changes: 0 additions & 82 deletions internal/semver/build.go

This file was deleted.

Loading

0 comments on commit 6c9abf5

Please sign in to comment.