semver

package module
v0.0.0-...-e85932b Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2017 License: MIT Imports: 5 Imported by: 0

README

go-semver

Build Status

codecov

A semver package for go.

See godoc for the API.

This package was designed to be as lightweight and simple as possible. It provides a single Version type:

type Version struct {
	Major      int
	Minor      int
	Patch      int
	Prerelease string
}

The type has methods for comparison with other Version values: Equals, GreaterThan and LessThan.

The package has a test suite with 100% coverage. To run the tests:

go test -v -cover github.com/ceralena/go-semver

Non-Standard Behaviour

The Parse() function supports incomplete version strings where the meaning can be inferred. For example:

  • v2 -> v2.0.0
  • v2.4 -> v2.4.0

Usage

See godoc for the API.

Assuming familiarity with how to write Go code:

To get the package, run go get github.com/ceralena/go-semver, or just add it to the imports for your package:

import "github.com/ceralena/go-semver"

To parse a version string:

s := "v1.0.7-alpha"
v, err := semver.Parse(s)
if err != nil {
	panic(err)
}
fmt.Println(v)

To compare two versions as equal, greater or less than:

a := semver.Version{1, 2, 3, "rc.1"}
b := semver.Version{4, 2, 3, "rc.1"}

a.Equals(b) // false
a.GreaterThan(b) // false
a.LessThan(b) // true

Printing the Version type will produce a standard semver string:

a := semver.Version{4, 12, 1, "beta"}
fmt.Sprintf("%s", a) // v4.12.1-beta

Contributing

All contributions and bug reports are welcome.

If you have any questions or issues, please use GitHub's issue tracker.

If you want to contribute, you can either send a pull request or contact me on twitter.

License

This package uses the MIT license. Generally speaking: you can do anything with this code. See the LICENSE file for the full text.

Documentation

Overview

package semver provides a type representing a semantic version, and facilities for parsing, serialisation and comparison.

See http://semver.org for more information on semantic versioning.

This package expands on the specification: a partial version string like "v2" or "v2.0" is considered valid, and expanded to "v2.0.0".

To parse a version string:

  s := "v1.0.7-alpha"
  v, err := semver.Parse(s)
  if err != nil {
	panic(err)
  }
  fmt.Println(v)

Visit godoc.org/github.com/ceralena/semver for the full package API.

Index

Constants

This section is empty.

Variables

View Source
var (
	EmptyVersion   = errors.New("Empty version string")
	IllegalVersion = errors.New("Illegal version string")
)

Functions

This section is empty.

Types

type Version

type Version struct {
	Major      int
	Minor      int
	Patch      int
	Prerelease string
}

A Version is a parsed semver version string.

If only a partial version was specified, the missing parts will be -1.

func Parse

func Parse(s string) (Version, error)

Parse takes the string representation of a version and returns a Version value if is valid.

The error value will either be nil, EmptyVersion or IllegalVersion.

func (Version) Equals

func (v Version) Equals(o Version) bool

Equals returns true if v and o are the same version.

func (Version) GreaterThan

func (v Version) GreaterThan(o Version) bool

GreaterThan returns true if v is a higher version than o.

func (Version) LessThan

func (v Version) LessThan(o Version) bool

LessThan returns true if v is a lesser version than o.

func (Version) String

func (v Version) String() string

Returns the standard string representation of the Version value.

For example, given this Version value:

Version{Major: 1, Minor: 2, Patch: 3, Prerelease: "beta1"}

The following string is produced:

v1.2.3-beta1

Jump to

Keyboard shortcuts

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