version

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package version implements the "version" CLI command.

Index

Constants

View Source
const (
	// SourceRelease is a build with link-time release values (e.g. goreleaser).
	SourceRelease = "release"
	// SourceModule is a build from the module cache: go install/run pkg@version.
	SourceModule = "module"
	// SourceVCS is a build in a VCS checkout, with the toolchain's VCS stamp.
	SourceVCS = "vcs"
	// SourceLocal is a build from local files with no VCS stamp.
	SourceLocal = "local"
	// SourceUnknown is a binary with no module build info at all.
	SourceUnknown = "unknown"
)

Source values: where the binary's source came from, as far as the build recorded it. Reported in JSON as "source"; the text form shows SourceDetail.

Variables

View Source
var (
	Commit     string
	CommitDate string
	TreeState  string
	BuiltBy    string
)

Release metadata injected at link time, for example by goreleaser:

-X github.com/StevenACoffman/anarch-lint/cmd/version.Commit={{ .FullCommit }}
-X github.com/StevenACoffman/anarch-lint/cmd/version.CommitDate={{ .CommitDate }}
-X github.com/StevenACoffman/anarch-lint/cmd/version.TreeState={{ .GitTreeState }}
-X github.com/StevenACoffman/anarch-lint/cmd/version.BuiltBy=goreleaser

A build through the module proxy (goreleaser's gomod.proxy, or go install pkg@version) carries no VCS stamps in its build info, so these are the only way such a binary can report its commit. A non-empty value wins over build info; empty defers to it. The linker silently ignores -X for a variable that does not exist, so renaming one of these breaks the release without an error.

View Source
var Version = "dev"

Version is the application version string. When built from a tagged release or installed via "go install", the Go toolchain embeds the module version automatically, and it is read from build info at startup. Override at link time only if the auto-detected value is incorrect:

go build -ldflags "-X 'github.com/StevenACoffman/anarch-lint/cmd/version.Version=v1.2.3'"

Functions

This section is empty.

Types

type Config

type Config struct {
	*root.Config
	JSON    bool
	Flags   *ff.FlagSet
	Command *ff.Command
}

Config holds the configuration for the version command.

func New

func New(parent *root.Config) *Config

New creates and registers the version command with the given parent config.

type Info

type Info struct {
	GitVersion   string `json:"gitVersion"`
	Source       string `json:"source"`
	SourceDetail string `json:"sourceDetail"`
	ModuleSum    string `json:"moduleChecksum"`
	GitCommit    string `json:"gitCommit"`
	GitTreeState string `json:"gitTreeState"`
	BuildDate    string `json:"buildDate"`
	BuiltBy      string `json:"builtBy"`
	GoVersion    string `json:"goVersion"`
	Compiler     string `json:"compiler"`
	Platform     string `json:"platform"`

	ASCIIName   string `json:"-"`
	Name        string `json:"-"`
	Description string `json:"-"`
	URL         string `json:"-"`
}

Info holds build and VCS metadata for structured output.

func GetVersionInfoFrom

func GetVersionInfoFrom(bi *debug.BuildInfo, link Stamp, options ...Option) *Info

GetVersionInfoFrom builds an Info from an explicit BuildInfo value and link stamp. Each field takes the stamp's value when set, then the build info's, then what a pseudo-version encodes, then "unknown". Passing nil and a zero Stamp returns an Info with all VCS fields set to "unknown" and current runtime values for GoVersion, Compiler, and Platform. This is useful for testing without touching global state.

func (*Info) JSONString

func (i *Info) JSONString() (string, error)

JSONString returns the JSON representation of the version info.

func (*Info) String

func (i *Info) String() string

String returns the string representation of the version info.

type Option

type Option func(i *Info)

Option can be used to customize the Info after it is gathered from the environment.

func WithASCIIName

func WithASCIIName(name string) Option

WithASCIIName allows you to add an ASCII art of the name.

func WithAppDetails

func WithAppDetails(name, description, url string) Option

WithAppDetails allows setting the app name and description.

func WithBuiltBy

func WithBuiltBy(name string) Option

WithBuiltBy allows to set the builder name/builder system name.

type Stamp

type Stamp struct {
	Version    string
	Commit     string
	CommitDate string
	TreeState  string
	BuiltBy    string
}

Stamp holds the values injected at link time with -ldflags -X. An empty field, or a Version still at "dev", defers to the build info.

func LinkStamp

func LinkStamp() Stamp

LinkStamp returns the values this binary was linked with.

Jump to

Keyboard shortcuts

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