makeversion

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2022 License: MIT Imports: 11 Imported by: 0

README

build Coverage Status Go Report Card

makeversion

Create a project version string from Git tags and build counters.

//go:generate go run github.com/cparta/makeversion/cmd/mkver@latest -name packagename -out version.gen.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckGitRepo added in v0.0.6

func CheckGitRepo(dir string) (repo string, err error)

CheckGitRepo checks that the given directory is part of a git repository, meaning that it or one of it's parent directories has a '.git' subdirectory. If it is, it returns the absolute path of the git repo and a nil error.

Types

type DefaultGitter

type DefaultGitter string

func (DefaultGitter) GetBranch

func (dg DefaultGitter) GetBranch(repo string) (branch string)

func (DefaultGitter) GetBuild

func (dg DefaultGitter) GetBuild(repo string) string

func (DefaultGitter) GetTag

func (dg DefaultGitter) GetTag(repo string) string

type Environment

type Environment interface {
	Getenv(string) string
	LookupEnv(string) (string, bool)
}

Environment allows us to mock the OS environment

type Gitter

type Gitter interface {
	// GetTag returns the latest Git tag that starts with a lowercase 'v' followed by a number, otherwise an empty string.
	GetTag(repo string) string
	// GetBranch returns the current branch in the repository, or the string "HEAD" if we're in a Git repo, otherwise an empty string.
	GetBranch(repo string) string
	// GetBuild returns the number of commits in the currently checked out branch as a string, or an empty string
	GetBuild(repo string) string
}

Gitter is an interface exposing the required Git functionality

func NewDefaultGitter

func NewDefaultGitter(gitBin string) (gitter Gitter, err error)

type OsEnvironment

type OsEnvironment struct{}

OsEnvironment calls the OS functions.

func (OsEnvironment) Getenv

func (OsEnvironment) Getenv(key string) string

func (OsEnvironment) LookupEnv

func (OsEnvironment) LookupEnv(key string) (string, bool)

type VersionInfo

type VersionInfo struct {
	Tag     string // git tag, e.g. "v1.2.3"
	Branch  string // git branch, e.g. "mybranch"
	Build   string // git or CI build number, e.g. "456"
	Version string // composite version, e.g. "v1.2.3-mybranch.456"
}

func (*VersionInfo) Render

func (vi *VersionInfo) Render(pkgName string) (string, error)

Render returns either the Version string followed by a newline, or, if the pkgName is not an empty string, a small piece of Go code defining global variables named "PkgName" and "PkgVersion" with the given pkgName and the contents of Version. If the pkgName is given but isn't a valid Go identifier, an error is returned.

type VersionStringer

type VersionStringer struct {
	Git Gitter      // Git
	Env Environment // environment
}

func NewVersionStringer

func NewVersionStringer(gitBin string) (vs *VersionStringer, err error)

NewVersionStringer returns a VersionStringer ready to examine the git repositories using the given Git binary.

func (*VersionStringer) GetBranch

func (vs *VersionStringer) GetBranch(repo string) (branchText, branchName string)

GetBranch returns the current branch as a string suitable for inclusion in the semver text as well as the actual branch name in the build system or Git. If no branch name can be found, then "HEAD" is returned if we are running within a Git repo, or an empty string if we're not.

func (*VersionStringer) GetBuild

func (vs *VersionStringer) GetBuild(repo string) (build string)

GetBuild returns the build counter. This is taken from the CI system if available, otherwise the Git commit count is used. Returns an empty string if no reasonable build counter can be found.

func (*VersionStringer) GetTag

func (vs *VersionStringer) GetTag(repo string) (tag string, err error)

GetTag returns the git version tag. Returns an error if the tag is not in the form "vX.Y.Z".

func (*VersionStringer) GetVersion

func (vs *VersionStringer) GetVersion(repo string, forRelease bool) (vi VersionInfo, err error)

GetVersion returns a version string for the source code in the Git repository.

func (*VersionStringer) IsEnvTrue

func (vs *VersionStringer) IsEnvTrue(envvar string) bool

IsEnvTrue returns true if the given environment variable exists and is set to the string "true" (not case sensitive).

func (*VersionStringer) IsReleaseBranch

func (vs *VersionStringer) IsReleaseBranch(branchName string) bool

IsReleaseBranch returns true if the given branch name should be allowed to use 'release mode', where the version string doesn't contains build information suffix.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL