/version

Command line tool for versioning projects

Primary LanguageGo

Version

Command line tool (and Go package) for keeping track of the versions of projects or directories. Version creates and maintains a .version file in the directory containing the current version number, and provides a command line tool to easily get, and update the version number.

Perfect for:

  • Automated build/release scripts
  • Integration with GitHub tags

Smart incrementing

Version increases numbers in a smart way, so if you increase the major version number it will automatically reset the minor and build numbers, like this:

  • v1.2.3 will jump to v1.3.0 if you increase the minor build number
  • v1.2.3 will jump to v2.0.0 if you increase the major build number

Command line

The version command line has the following syntax:

version [flags] path [option]
  • flags - Optionally any flags (see below)
  • path - Path to set the version for. Use ./ for current directory.
  • option
    • No option will just read and return the current value and will not change it
    • + Increase the build number (1.0.0 -> 1.0.1) and return the new value
    • ++ Increase the minor number (1.0.0 -> 1.1.0) and return the new value
    • +++ Increase the major number (1.0.0 -> 2.0.0) and return the new value

Supported flags

  • -n - Suppress the linefeed at the end of the output
  • -v=false - Do not print the v prefix
  • -short - Print the shortest possible representation of the version number, i.e. instead of v1.0.0, it will just output v1.

Download

Pick one that matches your machine:

Place it into your /usr/bin directory or equivalent.

Tips for writing scripts

Use backticks to get the current version and use it in another command:

echo `version ./`
= v1.0.0

Or get the version of another directory:

version /path/to/directory
= v2.3.1

Remove the linefeed using the -n flag:

echo `version -n ./`

Increase the build version at the same time as getting it using the + option:

echo `version -n ./ +`

If you find reading v1.0.0 annoying like we do, use the -short flag to give you the shortest possible representation:

version -short ./
= v1

To use the version multiple times, use variables:

VERSION=`version -n ./ +`; echo $VERSION; echo $VERSION; echo $VERSION

Releasing in GitHub

We built Version so we could write scripts that managed our GitHub releases, so it allows you to do things like this:

echo "Last version:" `version ./`

# increase the version and keep it in the VERSION variable
VERSION=`version -n ./ +`

echo "New version: $VERSION"

# get the human-readable version number
SHORTVERSION=`version -short -n ./`

echo "Tagging release..."

# Tag the new release
git tag -a `echo $VERSION` -m "Release SHORTVERSION"

echo "Updating version file..."

# Commit the new .version file, since it's changed
git commit .version -m "Updated to version $SHORTVERSION"

echo "Pushing changes..."

# push changes and tags
git push origin master
git push --tags

echo "Finished"

Development

Version is a Go package that you are welcome to use in your own projects.

To get started, go get the package:

go get github.com/matryer/version

Then you may use the version.Version object in your own programs.