Categorygithub.com/checkr/go-latest
modulepackage
0.0.0-20191015165527-5e2b4eb40f9c
Repository: https://github.com/checkr/go-latest.git
Documentation: pkg.go.dev

# README

go-latest

GitHub release Wercker MIT License Go Documentation

go-latest is a package to check a provided version is latest or not from various sources.

Once you distribute your tool by golang and user start to use it, it's difficult to tell users that new version is released and encourage them to use new one. go-latest enables you to do that by just preparing simple source. For sources, currently you can use tags on Github, HTML meta tag (HTML scraping) and JSON response.

See more details in document at https://godoc.org/github.com/tcnksm/go-latest.

Install

To install, use go get:

$ go get -d github.com/tcnksm/go-latest

Usage

For sources to check, currently you can use tags on Github, HTML meta tag (HTML scraping) and JSON response.

Github Tag

To check 0.1.0 is the latest in tags on GitHub.

githubTag := &latest.GithubTag{
    Owner: "username",
    Repository: "reponame",
}

res, _ := latest.Check(githubTag, "0.1.0")
if res.Outdated {
    fmt.Printf("0.1.0 is not latest, you should upgrade to %s", res.Current)
}

go-latest uses Semantic Versioning to compare versions. If tagging name strategy on GitHub is different from it, you need to fix it with FixVersionStrFunc. For example, if you add v charactor in the begining of version string like v0.1.0, you need to transform it to 0.1.0, you can use DeleteFrontV() function like below,

githubTag := &latest.GithubTag{
    Owner:             "username",
    Repository:        "reponame",
    FixVersionStrFunc: latest.DeleteFrontV(),
}

You can define your own FixVersionStrFunc. See more on https://godoc.org/github.com/tcnksm/go-latest

HTML meta tag

You can use simple HTTP+HTML meta tag for a checking source.

For example, if you have a tool named reduce-worker and want to check 0.1.0 is latest or not, prepare HTML page which includes following meta tag,

<meta name="go-latest" content="reduce-worker 0.1.1 New version include security update">

And make request,

html := &latest.HTMLMeta{
    URL: "http://example.com/info",
    Name: "reduce-worker",
}

res, _ := latest.Check(html, "0.1.0")
if res.Outdated {
    fmt.Printf("0.1.0 is not latest, %s, upgrade to %s", res.Meta.Message, res.Current)
}

To know about HTML meta tag specification, see HTML Meta tag.

You can prepare your own HTML page and its scraping function. See more details in document at https://godoc.org/github.com/tcnksm/go-latest.

JSON

You can also use a JSON response.

If you want to check 0.1.0 is latest or not, prepare an API server which returns a following response,

{
    "version":"1.2.3",
    "message":"New version include security update, you should update soon",
    "url":"http://example.com/info"
}

And make request,

json := &latest.JSON{
    URL: "http://example.com/json",
}

res, _ := latest.Check(json, "0.1.0")
if res.Outdated {
    fmt.Printf("0.1.0 is not latest, %s, upgrade to %s", res.Meta.Message, res.Current)
}

You can use your own json schema by defining JSONReceive interface. See more details in document at https://godoc.org/github.com/tcnksm/go-latest.

Version comparing

To compare version, we use hashicorp/go-version. go-version follows Semantic Versioning. So to use go-latest you need to follow SemVer format.

For user who doesn't use SemVer format, go-latest has function to transform it into SemVer format.

Contribution

  1. Fork (https://github.com/tcnksm/go-latest/fork)
  2. Create a feature branch
  3. Commit your changes
  4. Rebase your local changes against the master branch
  5. Run test suite with the go test ./... command and confirm that it passes
  6. Run gofmt -s
  7. Create new Pull Request

Author

Taichi Nakashima

# Packages

No description provided by the author

# Functions

Check fetches last version information from its source and compares with target and return result (CheckResponse).
DeleteFrontV delete first `v` charactor on version string.

# Constants

EnvGoLatestDisable is environmental variable to disable go-latest execution.
MetaTagName is common HTML meta tag name which is defined on https://github.com/tcnksm/go-latest/blob/master/doc/html_meta.md.

# Structs

CheckResponse is a response for a Check request.
FetchResponse the commom response of Fetch request.
GithubTag is used to fetch version(tag) information from Github.
HTML is used to fetch version information from a single HTML page.
HTMLMeta is used to fetch a single HTML page and extract version information from specific meta tag.
JSON is used to get version information as json format from remote host.
Meta is meta information from Fetch request.

# Interfaces

HTMLScrap is used to scrap a single HTML page and extract version information.
JSONResponse is used to decode json as Struct and extract information.
Source is the interface that every version information source must implement.

# Type aliases

FixVersionStrFunc is function to fix version string so that it can be interpreted as Semantic Versiongin by http://godoc.org/github.com/hashicorp/go-version.
TagFilterFunc is fucntion to filter unexpected tags from GitHub.