Git Product home page Git Product logo

go-latest's Introduction

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

go-latest's People

Contributors

tcnksm avatar databus23 avatar syohex avatar mattn avatar sesquipedalian-dev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.