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.
To install, use go get
:
$ go get -d github.com/tcnksm/go-latest
For sources to check, currently you can use tags on Github, HTML meta tag (HTML scraping) and JSON response.
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
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.
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.
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.
- Fork (https://github.com/tcnksm/go-latest/fork)
- Create a feature branch
- Commit your changes
- Rebase your local changes against the master branch
- Run test suite with the
go test ./...
command and confirm that it passes - Run
gofmt -s
- Create new Pull Request