gosemver

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2022 License: MIT Imports: 2 Imported by: 1

README

go-semver

PkgGoDev

The package go-semver (Go Semantic Versioning) provides functions to handle semantic versioning. It integrates nicely with CLI packages like urfave/cli or cobra to show the current version and/or revision of a Go executable.

Table of contents

How to use

  1. Add this package as a dependency to your Go project by running the command go get github.com/erodrigufer/go-semver inside the directory where the go.mod file of your project lies.
  2. Import the package into the Go files that will use its methods.
Show the build revision of a Go executable

The method GetRevision() returns the commit's hash (revision) of the last commit of the codebase used to build an executable. If there are uncommitted modifications in the codebase since the last commit, the method will add the suffix '-dirty' to the string returned with the commit's hash.

If the method was unable to retrieve a revision, it will return the string 'unavailable' and a non-nil error.

import (
	"fmt"

	semver "github.com/erodrigufer/go-semver"
)

func printRevision() error {
	rev, err := semver.GetRevision()
	if err != nil {
		return err
	}
	
	fmt.Printf("revision: %s", rev)
	return nil
}

Troubleshooting

Revision is unavailable, i.e. is not showing up

You have to build your executable from within the VCS (Version Control System, e.g. git) repository/codebase in order for the go build tool to get the build information related to the VCS, like the revision, or its status (was it built with uncommitted modifications related to the last commit).

If you build your executable outside a VCS repository (aka not within a git repo) then the build tool is not going to be able to retrieve any revision or version information, and the GetRevision() function will return an error.

Minimum Go version required for a build

In order for the methods in this package to work, the minimum Go version used to compile an executable must be Go 1.18 since the struct type BuildSetting was first added in Go 1.18. Otherwise, the commit hash (i. e. revision) and revision status used for the build is not getting embedded into the executable and the executable is not able to access the BuildSetting struct.

If the package runtime/debug gets extended in the future, it might even be possible to read the tag with the latest release version used to compile an executable, but as of Go 1.19, this is only achieved when installing a package by running go install (one can access the versioning information embedded into an executable by running go version -m <PATH> with the path to the executable).

References

Documentation

Overview

go-sember gets version information embedded during the build process in the executable, e.g. revision and revision status from the VCS.

Dependencies: Some of the information being retrieved from the executable, can only be accessed from a build, if it was compiled with Go +1.18, most likely the compiler will not even allow a build if the version does not support the data structures required to access the build information.

This codebase has been modified from its original form, taken from autostrada.dev aka. Alex Edwards (2022). go-semver Eduardo Rodriguez (@erodrigufer) (c) 2022 -- MIT License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetRevision

func GetRevision() (string, error)

GetRevision, returns a string with the commit hash used to build the executable. If the executable was built from files with uncomitted modifications related to the last commit, the suffix '-dirty' will be added to the revision returned. If no revision information can be retrieved, the method returns the string 'unavailable' and an error.

Types

This section is empty.

Jump to

Keyboard shortcuts

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