Documentation
¶
Overview ¶
Package buildinfo reports which build of a command is running.
Release builds stamp the version, commit and build date through linker flags. With GoReleaser:
builds:
- ldflags:
- -s -w
- -X github.com/mittwald/cli-kit/buildinfo.Version={{ .Version }}
- -X github.com/mittwald/cli-kit/buildinfo.Commit={{ .FullCommit }}
- -X github.com/mittwald/cli-kit/buildinfo.Date={{ .Date }}
and with a plain go build:
go build -ldflags "-X github.com/mittwald/cli-kit/buildinfo.Version=1.2.3 \ -X github.com/mittwald/cli-kit/buildinfo.Commit=$(git rev-parse HEAD)"
Builds without linker flags fall back to what the Go toolchain records in the binary: the module version for "go install example.com/tool@v1.2.3", the version Go 1.24 and later derive from the repository's tags for a plain "go build" of a tagged commit, and the VCS revision, commit time and dirty state. Any other build is a development build: Info.String reports it as "dev" with its revision, such as "dev+0123456789ab.dirty", rather than the pseudo-version the toolchain made up for it.
Index ¶
Examples ¶
Constants ¶
const DevVersion = "dev"
DevVersion is the version a build without any version information reports.
Variables ¶
var ( // Version is the release version, with or without a leading "v". Version string // Commit is the full VCS revision the binary was built from. Commit string // Date is the build or commit time in RFC 3339 format. Date string )
Values stamped at link time with -ldflags "-X …". Leave them empty in the source; Get falls back to the toolchain's build information for every value that was not stamped.
Functions ¶
This section is empty.
Types ¶
type Info ¶
type Info struct {
// Version is the release version as stamped or recorded, such as "v1.2.3"
// or "1.2.3", or [DevVersion] for a build that is no release. It keeps
// the version as it was stamped, a leading "v" included; [Info.String]
// is the form shown to users.
Version string
// Commit is the VCS revision, or "" when unknown. It is the full
// revision, except for a build installed by commit ("go install
// example.com/tool@main"), which only records its first 12 characters.
Commit string
// Date is the build or commit time, or the zero time when unknown.
Date time.Time
// Modified reports whether the working tree had uncommitted changes when
// the binary was built. Only the toolchain records it.
Modified bool
// GoVersion is the Go release the binary was built with.
GoVersion string
}
Info describes one build of a command.
func Get ¶
func Get() Info
Get returns the build information of the running binary: the values stamped through linker flags, completed from the toolchain's records.
A version stamped through linker flags is kept as it is, unless it is DevVersion. Otherwise the module version the toolchain recorded is used when it names a release: "go install example.com/tool@v1.2.3", or a plain "go build" (Go 1.24 and later) of a checkout whose commit carries the release tag and has no uncommitted changes. A version the toolchain made up for any other commit (a pseudo-version, such as "v1.2.4-0.20260929160445-2f9134944bc2"), and one it marked "+dirty", name no release: the build reports DevVersion instead, and Info.String shows it with its revision, such as "dev+2f9134944bc2.dirty".
func (Info) Detail ¶
Detail returns a multi-line description for a version command: the version line, then the revision, build date and Go release where known. name is the command's name, written in front of the version.
Example ¶
package main
import (
"fmt"
"time"
"github.com/mittwald/cli-kit/buildinfo"
)
func main() {
info := buildinfo.Info{
Version: "v1.4.0",
Commit: "0123456789abcdef0123456789abcdef01234567",
Date: time.Date(2026, 11, 10, 8, 0, 0, 0, time.UTC),
GoVersion: "go1.24.9",
}
fmt.Print(info.Detail("example"))
}
Output: example 1.4.0 commit: 0123456789abcdef0123456789abcdef01234567 built: 2026-11-10T08:00:00Z go: go1.24.9
func (Info) ShortCommit ¶
ShortCommit returns the first 12 characters of the revision, or "" when it is unknown.
func (Info) String ¶
String returns the version as shown to users. A version with a leading "v" before a digit loses it ("v1.4.0" becomes "1.4.0"; Version keeps it); a development build carries its short revision and, when the tree was dirty, a ".dirty" marker ("dev+0123456789ab.dirty").
Example ¶
package main
import (
"fmt"
"github.com/mittwald/cli-kit/buildinfo"
)
func main() {
release := buildinfo.Info{Version: "v1.4.0"}
dev := buildinfo.Info{Version: buildinfo.DevVersion, Commit: "0123456789abcdef0123", Modified: true}
fmt.Println(release)
fmt.Println(dev)
}
Output: 1.4.0 dev+0123456789ab.dirty