version

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2023 License: MIT Imports: 8 Imported by: 15

README

import "go.szostok.io/version"

Go package to present your CLI version in a classy way. All magic included!

Like the idea? Give a GitHub star ⭐!

Quick Start

go get go.szostok.io/version

Visit version.szostok.io/quick-start for the most popular way of the setup.

Documentation

Visit version.szostok.io for complete documentation about setup and usage.

Why?

If you create a new CLI, it's natural that you use a framework such as Cobra, urfave/cli, or similar. Each of your CLIs has also an option to show its version. But in this case, we repeat the same stuff: collecting and displaying related information.

This package aims to solve that problem. To register the version command, simply add:

extension.NewVersionCobraCmd()

Go 1.18 simplified collecting version-related data, as commit, date, and other data are embedded. You can still override these fields with ldflags, e.g.:

-X go.szostok.io/version.version=1.42.0

You can gain more features, such as upgrade notice, just by adding:

extension.WithUpgradeNotice("repo-owner", "repo-name")

Functionality

  • For Go 1.18+, detect version, commit, commitDate, and dirtyBuild automatically
    • Allow version data overriding via -ldflags
  • Print the version in the YAML, JSON, short, and pretty formats
  • Detect and display an upgrade notice if a newer version of your project has been released
  • Automatically disable color output for non-tty output streams
    • Handle the version and upgrade notices separately
  • Designed in a way that lets you use each component individually
  • Everything can be enabled with a single line of code. For example, use extension.NewVersionCobraCmd() to enable the version command for Cobra
  • Customize the output format and behaviour (e.g. timeouts, re-check intervals)
  • Parse any dates and print them in the local date and time format
  • All provided functionality is fully tested to ensure no regression
  • Extend the version info with own fields just by assigning your Go struct

Stay informed

Follow @m_szostok on Twitter to get the latest news. You can also subscribe for new version releases on GitHub, where you can find a detailed changelog for each of them.

For additional content, check Mateusz Szostok's blog.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExtraFields added in v1.1.0

type ExtraFields any

ExtraFields provides an option to pass extra fields.

type Info

type Info struct {
	Version    string `yaml:"version,omitempty"     json:"version,omitempty"`
	GitCommit  string `yaml:"gitCommit,omitempty"   json:"gitCommit,omitempty"`
	BuildDate  string `yaml:"buildDate,omitempty"   json:"buildDate,omitempty"`
	CommitDate string `yaml:"commitDate,omitempty"  json:"commitDate,omitempty"`
	DirtyBuild bool   `yaml:"dirtyBuild,omitempty"  json:"dirtyBuild,omitempty"`
	GoVersion  string `yaml:"goVersion,omitempty"   json:"goVersion,omitempty"`
	Compiler   string `yaml:"compiler,omitempty"    json:"compiler,omitempty"`
	Platform   string `yaml:"platform,omitempty"    json:"platform,omitempty"`

	Meta        Meta        `json:"-" yaml:"-"`
	ExtraFields ExtraFields `json:"-" yaml:"-"`
}

Info contains versioning information.

func Get

func Get() *Info

Get returns the overall codebase version. It's for detecting what code a binary was built from.

Version related variables are resolved in such order:

  1. Go -ldflags
  2. or debug.ReadBuildInfo() in Go 1.18+ * version is set only if the binary is built with "go install url/tool@version". * commit is taken from the vcs.revision tag. * commitDate is taken from the vcs.time tag. * dirtyBuild is taken from the vcs.modified tag.
  3. in their absence fallback to the settings in ./base.go.

func (*Info) MarshalJSON added in v1.1.0

func (p *Info) MarshalJSON() ([]byte, error)

MarshalJSON marshal data into JSON but ensures that extra fields are inlined.

func (*Info) MarshalYAML added in v1.1.0

func (p *Info) MarshalYAML() (interface{}, error)

MarshalYAML marshal data into YAML but ensures that extra fields are inlined.

type Meta

type Meta struct {
	CLIName string
}

Meta holds additional version metadata.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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