Repository: tcnksm/go-latest Branch: master Commit: e3007ae9052e Files: 28 Total size: 34.9 KB Directory structure: gitextract_f9zuztq5/ ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── doc/ │ └── html_meta.md ├── github.go ├── github_test.go ├── helper_test.go ├── html.go ├── html_meta.go ├── html_meta_test.go ├── html_test.go ├── json.go ├── json_test.go ├── latest/ │ ├── README.md │ ├── cli.go │ ├── main.go │ ├── scripts/ │ │ ├── compile.sh │ │ └── package.sh │ └── version.go ├── latest.go ├── latest_test.go ├── test-fixtures/ │ ├── default.html │ ├── default.json │ ├── meta.html │ ├── original.html │ └── original.json └── wercker.yml ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ *.test bin latest/pkg/ .envrc ================================================ FILE: CHANGELOG.md ================================================ ## 0.1.1 (2015-04-14) Add `latest` command ### Added - `DeleveFrontV()` function to delete font `v` charactor - `latest` command in `cmd` diretory, to check version is latest or not from command line ### Deprecated - Nothing ### Removed - Nothing ### Fixed - More documentation ## 0.1.0 (2015-04-07) Initial release ### Added - Fundamental features ### Deprecated - Nothing ### Removed - Nothing ### Fixed - Nothing ================================================ FILE: LICENSE ================================================ Copyright (c) 2015 Taichi Nakashima MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ go-latest ==== [![GitHub release](http://img.shields.io/github/release/tcnksm/go-latest.svg?style=flat-square)][release] [![Wercker](http://img.shields.io/wercker/ci/551e58c16b7badb977000128.svg?style=flat-square)][wercker] [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)][license] [![Go Documentation](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)][godocs] [release]: https://github.com/tcnksm/go-latest/releases [wercker]: https://app.wercker.com/project/bykey/1059e8b0cf3bde5fc220477d39a1bf0e [license]: https://github.com/tcnksm/go-latest/blob/master/LICENSE [godocs]: http://godoc.org/github.com/tcnksm/go-latest `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](doc/html_meta.md) (HTML scraping) and JSON response. See more details in document at [https://godoc.org/github.com/tcnksm/go-latest](https://godoc.org/github.com/tcnksm/go-latest). ## Install To install, use `go get`: ```bash $ go get -d github.com/tcnksm/go-latest ``` ## Usage For sources to check, currently you can use tags on Github, [HTML meta tag](doc/html_meta.md) (HTML scraping) and JSON response. ### Github Tag To check `0.1.0` is the latest in tags on GitHub. ```golang 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](http://semver.org/) 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, ```golang 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](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, ```html ``` And make request, ```golang 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](doc/html_meta.md). 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](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, ```json { "version":"1.2.3", "message":"New version include security update, you should update soon", "url":"http://example.com/info" } ``` And make request, ```golang 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](https://godoc.org/github.com/tcnksm/go-latest). ## Version comparing To compare version, we use [hashicorp/go-version](https://github.com/hashicorp/go-version). `go-version` follows [Semantic Versioning](http://semver.org/). 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](https://github.com/tcnksm/go-latest/fork)) 1. Create a feature branch 1. Commit your changes 1. Rebase your local changes against the master branch 1. Run test suite with the `go test ./...` command and confirm that it passes 1. Run `gofmt -s` 1. Create new Pull Request ## Author [Taichi Nakashima](https://github.com/tcnksm) ================================================ FILE: doc/html_meta.md ================================================ # HTML meta tag version discovery `go-latest.HTMLMeta` uses HTML meta tag to check latest version of your tool. It will request provided `URL` and inspec the HTML returned for meta tags that have the following format: ```bash ``` - `product-name` is your tool name. It MUST be filled - `SemVer` is your tool version by [Semantic Versioning](http://semver.org/). It MUST be filled - `message` is a message. It MAY be filled For example, if you want to check latest version of `reduce-worker`, you just prepare a HTML page which contains following tags. ```bash ``` You can know latest version is `1.2.3`. ## References `go-latest`'s HTML meta tag version discovery specification refers following: - [Golang Remote import paths](https://golang.org/cmd/go/#hdr-Remote_import_paths) - [App Container Image Discovery](https://github.com/appc/spec/blob/master/SPEC.md#app-container-image-discovery) ================================================ FILE: github.go ================================================ package latest import ( "context" "fmt" "net/url" "strings" "github.com/google/go-github/github" "github.com/hashicorp/go-version" ) // 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 type FixVersionStrFunc func(string) string // TagFilterFunc is fucntion to filter unexpected tags // from GitHub. Check a given tag as string (before FixVersionStr) // and return bool. If it's expected, return true. If not return false. type TagFilterFunc func(string) bool var ( defaultFixVersionStrFunc FixVersionStrFunc defaultTagFilterFunc TagFilterFunc ) func init() { defaultFixVersionStrFunc = fixNothing() defaultTagFilterFunc = filterNothing() } // GithubTag is used to fetch version(tag) information from Github. type GithubTag struct { // Owner and Repository are GitHub owner name and its repository name. // e.g., If you want to check https://github.com/tcnksm/ghr version // Repository is `ghr`, and Owner is `tcnksm`. Owner string Repository string // FixVersionStrFunc is function to fix version string (in this case tag // name string) on GitHub so that it can be interpreted as Semantic Versioning // by hashicorp/go-version. By default, it does nothing. FixVersionStrFunc FixVersionStrFunc // TagFilterFunc is function to filter tags from GitHub. Some project includes // tags you don't want to use for version comparing. It can be used to exclude // such tags. By default, it does nothing. TagFilterFunc TagFilterFunc // URL & Token is used for GitHub Enterprise URL string Token string } func (g *GithubTag) fixVersionStrFunc() FixVersionStrFunc { if g.FixVersionStrFunc == nil { return defaultFixVersionStrFunc } return g.FixVersionStrFunc } func (g *GithubTag) tagFilterFunc() TagFilterFunc { if g.TagFilterFunc == nil { return defaultTagFilterFunc } return g.TagFilterFunc } // fixNothing does nothing. This is a default function of FixVersionStrFunc. func fixNothing() FixVersionStrFunc { return func(s string) string { return s } } func filterNothing() TagFilterFunc { return func(s string) bool { return true } } // DeleteFrontV delete first `v` charactor on version string. // For example version name `v0.1.1` becomes `0.1.1` func DeleteFrontV() FixVersionStrFunc { return func(s string) string { return strings.Replace(s, "v", "", 1) } } func (g *GithubTag) newClient() *github.Client { client := github.NewClient(nil) if g.URL != "" { client.BaseURL, _ = url.Parse(g.URL) } return client } func (g *GithubTag) Validate() error { if len(g.Repository) == 0 { return fmt.Errorf("GitHub repository name must be set") } if len(g.Owner) == 0 { return fmt.Errorf("GitHub owner name must be set") } if g.URL != "" { if _, err := url.Parse(g.URL); err != nil { return fmt.Errorf("GitHub API Url invalid: %s", err) } } return nil } func (g *GithubTag) Fetch() (*FetchResponse, error) { fr := newFetchResponse() // Create a client client := g.newClient() tags, resp, err := client.Repositories.ListTags(context.Background(), g.Owner, g.Repository, nil) if err != nil { return fr, err } if resp.StatusCode != 200 { return fr, fmt.Errorf("Unknown status: %d", resp.StatusCode) } // fixF is FixVersionStrFunc transform tag name string into SemVer string // By default, it does nothing. fixF := g.fixVersionStrFunc() // filterF is TagFilterFunc to filter unexpected tags // By default, it filter nothing. filterF := g.tagFilterFunc() for _, tag := range tags { if !filterF(*tag.Name) { fr.Malformeds = append(fr.Malformeds, *tag.Name) continue } v, err := version.NewVersion(fixF(*tag.Name)) if err != nil { fr.Malformeds = append(fr.Malformeds, fixF(*tag.Name)) continue } fr.Versions = append(fr.Versions, v) } return fr, nil } ================================================ FILE: github_test.go ================================================ package latest import ( "testing" ) func TestGithubTag_implement(t *testing.T) { var _ Source = &GithubTag{} } ================================================ FILE: helper_test.go ================================================ package latest import ( "io" "net/http" "net/http/httptest" "os" ) func fakeServer(fixture string) *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { f, err := os.Open(fixture) if err != nil { // Should not reach here panic(err) } io.Copy(w, f) })) } ================================================ FILE: html.go ================================================ package latest import ( "bytes" "fmt" "io" "io/ioutil" "net" "net/http" "net/url" "github.com/hashicorp/go-version" ) // HTML is used to fetch version information from a single HTML page. type HTML struct { // URL is HTML page URL which include version information. URL string // Scrap is used to scrap a single HTML page and extract version information. // See more about HTMLScrap interface. // By default, it does nothing, just return HTML contents. Scrap HTMLScrap } // HTMLScrap is used to scrap a single HTML page and extract version information. type HTMLScrap interface { // Exec is called from Fetch after fetching a HTMl page from source. // It must return version information as string list format. Exec(r io.Reader) ([]string, *Meta, error) } type defaultHTMLScrap struct{} func (s *defaultHTMLScrap) Exec(r io.Reader) ([]string, *Meta, error) { meta := &Meta{} b, err := ioutil.ReadAll(r) if err != nil { return []string{}, meta, err } b = bytes.Replace(b, []byte("\n"), []byte(""), -1) return []string{string(b[:])}, meta, nil } func (h *HTML) scrap() HTMLScrap { if h.Scrap == nil { return &defaultHTMLScrap{} } return h.Scrap } func (h *HTML) Validate() error { if len(h.URL) == 0 { return fmt.Errorf("URL must be set") } // Check URL can be parsed if _, err := url.Parse(h.URL); err != nil { return fmt.Errorf("%s is invalid URL: %s", h.URL, err.Error()) } return nil } func (h *HTML) Fetch() (*FetchResponse, error) { fr := newFetchResponse() // URL is validated before call u, _ := url.Parse(h.URL) // Create a new request req, err := http.NewRequest("GET", u.String(), nil) if err != nil { return fr, err } req.Header.Add("Accept", "application/json") // Create client t := &http.Transport{ Proxy: http.ProxyFromEnvironment, Dial: func(n, a string) (net.Conn, error) { return net.DialTimeout(n, a, defaultDialTimeout) }, } client := &http.Client{ Transport: t, } resp, err := client.Do(req) if err != nil { return fr, err } if resp.StatusCode != 200 { return fr, fmt.Errorf("unknown status: %d", resp.StatusCode) } scrap := h.scrap() verStrs, meta, err := scrap.Exec(resp.Body) if err != nil { return fr, err } if len(verStrs) == 0 { return fr, fmt.Errorf("version info is not found on %s", h.URL) } for _, verStr := range verStrs { v, err := version.NewVersion(verStr) if err != nil { fr.Malformeds = append(fr.Malformeds, verStr) continue } fr.Versions = append(fr.Versions, v) } fr.Meta = meta return fr, nil } ================================================ FILE: html_meta.go ================================================ package latest import ( "fmt" "io" "strings" "golang.org/x/net/html" "golang.org/x/net/html/atom" ) // MetaTagName is common HTML meta tag name which is defined on https://github.com/tcnksm/go-latest/blob/master/doc/html_meta.md const MetaTagName = "go-latest" // HTMLMeta is used to fetch a single HTML page and extract version information from // specific meta tag. See meta tag specification that HTMLMeta tries to extract on https://github.com/tcnksm/go-latest/blob/master/doc/html_meta.md type HTMLMeta struct { // URL is HTML page URL which include version information. URL string // Name is tool name which you want to check. This name must be // written in HTML meta tag content field. HTMLMeta use this to // extract version information. Name string } func (hm *HTMLMeta) newHTML() *HTML { return &HTML{ URL: hm.URL, Scrap: &metaTagScrap{Name: hm.Name}, } } func (hm *HTMLMeta) Validate() error { return hm.newHTML().Validate() } func (hm *HTMLMeta) Fetch() (*FetchResponse, error) { return hm.newHTML().Fetch() } type metaTagScrap struct { Name string } type tagInside struct { name string prefix string version string meta *Meta } func (mt *metaTagScrap) Exec(r io.Reader) ([]string, *Meta, error) { z := html.NewTokenizer(r) for { switch z.Next() { case html.ErrorToken: return []string{}, &Meta{}, fmt.Errorf("meta tag for %s is not found", mt.Name) case html.StartTagToken, html.SelfClosingTagToken: tok := z.Token() if tok.DataAtom == atom.Meta { product, version, message := attrAnalizer(tok.Attr) // Return first founded version. // Assumes that mata tag exist only one for each product if product == mt.Name { return []string{version}, &Meta{Message: message}, nil } } } } } func attrAnalizer(attrs []html.Attribute) (product, version, message string) { for _, a := range attrs { if a.Namespace != "" { continue } switch a.Key { case "name": if a.Val != MetaTagName { break } case "content": parts := strings.SplitN(strings.TrimSpace(a.Val), " ", 3) if len(parts) < 2 { break } product = parts[0] version = parts[1] // message is optional if len(parts) == 3 { message = parts[2] } } } return } ================================================ FILE: html_meta_test.go ================================================ package latest import ( "net/http/httptest" "testing" ) func TestHTMLMeta_implement(t *testing.T) { var _ Source = &HTMLMeta{} } func TestHTMLMetaFetch(t *testing.T) { tests := []struct { name string testServer *httptest.Server expectCurrent string expectMessage string }{ { name: "reduce-worker", testServer: fakeServer("test-fixtures/meta.html"), expectCurrent: "1.2.1", expectMessage: "New version include security update", }, } for i, tt := range tests { ts := tt.testServer defer ts.Close() h := &HTMLMeta{ URL: ts.URL, Name: tt.name, } fr, err := h.Fetch() if err != nil { t.Fatalf("#%d Fetch() expects error:%q to be nil", i, err.Error()) } versions := fr.Versions if len(versions) == 0 { t.Fatalf("#%d Fetch() expects number of versions found from HTML not to be 0", i) } current := versions[0].String() if current != tt.expectCurrent { t.Fatalf("#%d Fetch() expects %s to be %s", i, current, tt.expectCurrent) } message := fr.Meta.Message if message != tt.expectMessage { t.Fatalf("#%d Fetch() expects %q to be %q", i, message, tt.expectMessage) } } } ================================================ FILE: html_test.go ================================================ package latest import ( "io" "net/http/httptest" "sort" "testing" "github.com/hashicorp/go-version" "golang.org/x/net/html" "golang.org/x/net/html/atom" ) func TestHTML_implement(t *testing.T) { var _ Source = &HTML{} } func TestHTMLFetch(t *testing.T) { tests := []struct { testServer *httptest.Server expectCurrent string expectMessage string scrap HTMLScrap }{ { testServer: fakeServer("test-fixtures/default.html"), expectCurrent: "1.2.3", }, { testServer: fakeServer("test-fixtures/original.html"), expectCurrent: "1.2.5", expectMessage: "New version include security update, you should update soon", scrap: &DivAttributeScrap{}, }, } for i, tt := range tests { ts := tt.testServer defer ts.Close() h := &HTML{ URL: ts.URL, Scrap: tt.scrap, } fr, err := h.Fetch() if err != nil { t.Fatalf("#%d Fetch() expects error:%q to be nil", i, err.Error()) } versions := fr.Versions if len(versions) == 0 { t.Fatalf("#%d Fetch() expects number of versions found from HTML not to be 0", i) } sort.Sort(version.Collection(versions)) current := versions[len(versions)-1].String() if current != tt.expectCurrent { t.Fatalf("#%d Fetch() expects %s to be %s", i, current, tt.expectCurrent) } message := fr.Meta.Message if message != tt.expectMessage { t.Fatalf("#%d Fetch() expects %q to be %q", i, message, tt.expectMessage) } } } type DivAttributeScrap struct { } func (s *DivAttributeScrap) Exec(r io.Reader) ([]string, *Meta, error) { // Check function attrs has correct class="val" key&value isTarget := func(targetVal string, attrs []html.Attribute) bool { for _, a := range attrs { if a.Namespace != "" { continue } if a.Key == "class" && a.Val == targetVal { return true } } return false } var verStrs []string meta := &Meta{} z := html.NewTokenizer(r) for { switch z.Next() { case html.ErrorToken: return verStrs, meta, nil case html.StartTagToken: tok := z.Token() //
VERSION
if tok.DataAtom == atom.Div && isTarget("version", tok.Attr) { z.Next() newTok := z.Token() verStrs = append(verStrs, newTok.String()) } //
MESSAGE
if tok.DataAtom == atom.Div && isTarget("message", tok.Attr) { z.Next() newTok := z.Token() meta.Message = newTok.String() } } } } ================================================ FILE: json.go ================================================ package latest import ( "encoding/json" "fmt" "net" "net/http" "net/url" "time" "github.com/hashicorp/go-version" ) var ( defaultDialTimeout = 5 * time.Second ) // JSON is used to get version information as json format from remote host. type JSON struct { // URL is URL which return json response with version information. URL string // Response is used to decode json as Struct and extract version information. // See JSONResponse interface. By Default, it is used defaultJSONResponse. Response JSONResponse } // JSONResponse is used to decode json as Struct and extract information. type JSONResponse interface { // VersionInfo is called from Fetch to extract version info. // It must return Semantic Version format version string list. VersionInfo() ([]string, error) // MetaInfo is called from Fetch to extract meta info. MetaInfo() (*Meta, error) } type defaultJSONResponse struct { Version string `json:"version"` Message string `json:"message"` URL string `json:"url"` } func (res *defaultJSONResponse) VersionInfo() ([]string, error) { return []string{res.Version}, nil } func (res *defaultJSONResponse) MetaInfo() (*Meta, error) { return &Meta{ Message: res.Message, URL: res.URL, }, nil } func (j *JSON) response() JSONResponse { if j.Response == nil { return &defaultJSONResponse{} } return j.Response } func (j *JSON) Validate() error { if len(j.URL) == 0 { return fmt.Errorf("URL must be set") } // Check URL can be parsed by net.URL if _, err := url.Parse(j.URL); err != nil { return fmt.Errorf("%s is invalid URL: %s", j.URL, err.Error()) } return nil } func (j *JSON) Fetch() (*FetchResponse, error) { fr := newFetchResponse() // URL is validated before call u, _ := url.Parse(j.URL) // Create a new request req, err := http.NewRequest("GET", u.String(), nil) if err != nil { return fr, err } req.Header.Add("Accept", "application/json") // Create client t := &http.Transport{ Proxy: http.ProxyFromEnvironment, Dial: func(n, a string) (net.Conn, error) { return net.DialTimeout(n, a, defaultDialTimeout) }, } client := &http.Client{ Transport: t, } resp, err := client.Do(req) if err != nil { return fr, err } if resp.StatusCode != 200 { return fr, fmt.Errorf("unknown status: %d", resp.StatusCode) } result := j.response() dec := json.NewDecoder(resp.Body) if err := dec.Decode(&result); err != nil { return fr, err } verStrs, err := result.VersionInfo() if err != nil { return fr, err } if len(verStrs) == 0 { return fr, fmt.Errorf("version info is not found on %s", j.URL) } for _, verStr := range verStrs { v, err := version.NewVersion(verStr) if err != nil { fr.Malformeds = append(fr.Malformeds, verStr) } fr.Versions = append(fr.Versions, v) } fr.Meta, err = result.MetaInfo() if err != nil { return fr, err } return fr, nil } ================================================ FILE: json_test.go ================================================ package latest import ( "net/http/httptest" "strings" "testing" ) func TestJSON_implement(t *testing.T) { var _ Source = &JSON{} } func TestJSONValidate(t *testing.T) { tests := []struct { JSON *JSON expectErr bool }{ { JSON: &JSON{ URL: "http://good.com", }, expectErr: false, }, { JSON: &JSON{ URL: "", }, expectErr: true, }, } for i, tt := range tests { j := tt.JSON err := j.Validate() if tt.expectErr == (err == nil) { t.Fatalf("#%d Validate() expects err == nil to eq %t", i, tt.expectErr) } } } // OriginalResponse implements Receiver and receives test-fixtures/original.json type OriginalResponse struct { Name string `json:"name"` Version string `json:"version_info"` Status string `json:"status"` } func (r *OriginalResponse) VersionInfo() ([]string, error) { verStr := strings.Replace(r.Version, "v", "", 1) return []string{verStr}, nil } func (r *OriginalResponse) MetaInfo() (*Meta, error) { return &Meta{ Message: r.Status, }, nil } func TestJSONFetch(t *testing.T) { tests := []struct { testServer *httptest.Server response JSONResponse expectCurrent string expectMessage string expectURL string }{ { testServer: fakeServer("test-fixtures/default.json"), expectCurrent: "1.2.3", expectMessage: "New version include security update, you should update soon", expectURL: "http://example.com/info", }, { testServer: fakeServer("test-fixtures/original.json"), expectCurrent: "1.0.0", expectMessage: "We are releasing now", response: &OriginalResponse{}, }, } for i, tt := range tests { ts := tt.testServer defer ts.Close() j := &JSON{ URL: ts.URL, Response: tt.response, } fr, err := j.Fetch() if err != nil { t.Fatalf("#%d Fetch() expects error:%q to be nil", i, err.Error()) } versions := fr.Versions current := versions[0].String() if current != tt.expectCurrent { t.Fatalf("#%d Fetch() expects %s to be %s", i, current, tt.expectCurrent) } message := fr.Meta.Message if message != tt.expectMessage { t.Fatalf("#%d Fetch() expects %q to be %q", i, message, tt.expectMessage) } url := fr.Meta.URL if url != tt.expectURL { t.Fatalf("#%d Fetch() expects %q to be %q", i, url, tt.expectURL) } } } ================================================ FILE: latest/README.md ================================================ # latest `latest` is a command to check a provided version is latest or not in GitHub. ## Usage To check cloned repository is latest or not, just run with owner name and repository name which you want to check. If it is not latest version, it returns non-zero exit code. ```bash $ latest -owner=tcnksm -repo=go-latest 2.4.1 $ echo $? 0 ``` You can check version is new, it means version is not exist on GitHub and greater than others, and more outputs can be enabled with `-debug` flag, ```bash $ latest -debug -new -owner=tcnksm repo=go-latest 2.4.1 2.2.1 is new ``` See more usage with `-help` options. ## Install To install `latest` command just run `go get`, ```bash $ go get github.com/tcnksm/go-latest/latest ``` ================================================ FILE: latest/cli.go ================================================ package main import ( "flag" "fmt" "io" "os" "github.com/tcnksm/go-latest" ) type CLI struct { // out/err stream is the stdout and stderr // to write message from CLI outStream, errStream io.Writer } // Run executes CLI and return its exit code func (c *CLI) Run(args []string) int { var githubTag latest.GithubTag flags := flag.NewFlagSet(Name, flag.ExitOnError) flags.Usage = func() { fmt.Fprintf(c.errStream, helpText) } flags.SetOutput(c.errStream) flags.StringVar(&githubTag.Repository, "repo", "", "Repository name") flags.StringVar(&githubTag.Owner, "owner", "", "Repository owner name") flgNew := flags.Bool("new", false, "Check TAG(VERSION) is new and greater") flgFixVerStrFunc := flags.String("fix", "none", "Specify FixVersionStrFunc") flgVersion := flags.Bool("version", false, "Print version information") flgHelp := flags.Bool("help", false, "Print this message and quit") flgDebug := flags.Bool("debug", false, "Print verbose(debug) output") if err := flags.Parse(args[1:]); err != nil { fmt.Fprint(c.errStream, "Failed to parse flag\n") return 1 } // Show version and quit if *flgVersion { fmt.Fprintf(c.errStream, "%s Version v%s build %s\n", Name, Version, GitCommit) return 0 } // Show help and quit if *flgHelp { fmt.Fprintf(c.errStream, helpText) return 0 } // Run as debug mode if os.Getenv(envDebug) != "" { *flgDebug = true } parsedArgs := flags.Args() if len(parsedArgs) != 1 { fmt.Fprintf(c.errStream, "Invalid arguments\n") return 1 } target := parsedArgs[0] // Specify FixVersionStrFunc // e.g., if version is v0.3.1 it should be 0.3.1 (SemVer format) var f latest.FixVersionStrFunc switch *flgFixVerStrFunc { case "none": f = nil case "frontv": f = latest.DeleteFrontV() target = f(target) default: fmt.Fprintf(c.errStream, "Invalid fix func: %s\n", *flgFixVerStrFunc) return 1 } githubTag.FixVersionStrFunc = f res, err := latest.Check(&githubTag, target) if err != nil { fmt.Fprintf(c.errStream, "Failed to check: %s\n", err.Error()) return 1 } // Default variables exitCode := 0 output := fmt.Sprintf("%s is latest\n", target) // Check version is `new` if *flgNew { if !res.New { exitCode = 1 output = fmt.Sprintf("%s is not new\n", target) } else { output = fmt.Sprintf("%s is new\n", target) } } else { if !res.Latest { exitCode = 1 output = fmt.Sprintf("%s is not latest\n", target) } } if *flgDebug { fmt.Fprint(c.outStream, output) } return exitCode } const helpText = `Usage: latest [options] TAG latest command check TAG(VERSION) is latest. If is not latest, it returns non-zero value. It try to compare version by Semantic Versioning. Options: -owner=NAME Set GitHub repository owner name. -repo=NAME Set Github repository name. -new Check TAG(VERSION) is new. 'new' means TAG(VERSION) is not exist and greater than others. -fix=none Specify FixVersionStrFunc (Fix version string to SemVer) 'none': does nothing (default) 'front': deletes front 'v' charactor -help Print this message and quit. -debug Print verbose(debug) output. Example: $ latest -debug 0.2.0 ` ================================================ FILE: latest/main.go ================================================ package main import "os" // envDebug is used for changing verbose outoput var envDebug = "DEBUG" func main() { cli := &CLI{outStream: os.Stdout, errStream: os.Stderr} os.Exit(cli.Run(os.Args)) } ================================================ FILE: latest/scripts/compile.sh ================================================ #!/bin/bash set -e DIR=$(cd $(dirname ${0})/.. && pwd) cd ${DIR} XC_ARCH=${XC_ARCH:-386 amd64} XC_OS=${XC_OS:-darwin linux windows} COMMIT=`git describe --always` rm -rf pkg/ gox \ -ldflags "-X main.GitCommit \"${COMMIT}\"" \ -os="${XC_OS}" \ -arch="${XC_ARCH}" \ -output "pkg/{{.OS}}_{{.Arch}}/{{.Dir}}" ================================================ FILE: latest/scripts/package.sh ================================================ #!/bin/bash set -e DIR=$(cd $(dirname ${0})/.. && pwd) cd ${DIR} VERSION=$(grep "const Version " version.go | sed -E 's/.*"(.+)"$/\1/') REPO="latest" # Run Compile ./scripts/compile.sh if [ -d pkg ];then rm -rf ./pkg/dist fi # Package all binary as .zip mkdir -p ./pkg/dist/${VERSION} for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do PLATFORM_NAME=$(basename ${PLATFORM}) ARCHIVE_NAME=${REPO}_${VERSION}_${PLATFORM_NAME} if [ $PLATFORM_NAME = "dist" ]; then continue fi pushd ${PLATFORM} zip ${DIR}/pkg/dist/${VERSION}/${ARCHIVE_NAME}.zip ./* popd done # Generate shasum pushd ./pkg/dist/${VERSION} shasum * > ./${VERSION}_SHASUMS popd ================================================ FILE: latest/version.go ================================================ package main const Name = "latest" const Version = "0.1.1" var GitCommit = "" ================================================ FILE: latest.go ================================================ /* go-latest is pacakge to check a provided version is latest from various sources. http://github.com/tcnksm/go-latest package main import ( "github.com/tcnksm/go-latest" ) githubTag := &latest.GithubTag{ Owner: "tcnksm", Repository: "ghr" } res, _ := latest.Check("0.1.0",githubTag) if res.Outdated { fmt.Printf("version 0.1.0 is out of date, you can upgrade to %s", res.Current) } */ package latest import ( "fmt" "os" "sort" "github.com/hashicorp/go-version" ) // EnvGoLatestDisable is environmental variable to disable go-latest // execution. const EnvGoLatestDisable = "GOLATEST_DISABLE" // Source is the interface that every version information source must implement. type Source interface { // Validate is called before Fetch in Check. // Source may need to have various information like URL or product name, // so it is used for check each variables are correctly set. // If it is failed, Fetch() will not executed. Validate() error // Fetch is called in Check to fetch information from remote sources. // After fetching, it will convert it into common expression (FetchResponse) Fetch() (*FetchResponse, error) } // FetchResponse the commom response of Fetch request. type FetchResponse struct { Versions []*version.Version Malformeds []string Meta *Meta } // Meta is meta information from Fetch request. type Meta struct { Message string URL string } // CheckResponse is a response for a Check request. type CheckResponse struct { // Current is current latest version on source. Current string // Outdate is true when target version is less than Curernt on source. Outdated bool // Latest is true when target version is equal to Current on source. Latest bool // New is true when target version is greater than Current on source. New bool // Malformed store versions or tags which can not be parsed as // Semantic versioning (not compared with target). Malformeds []string // Meta is meta information from source. Meta *Meta } // Check fetches last version information from its source // and compares with target and return result (CheckResponse). func Check(s Source, target string) (*CheckResponse, error) { if os.Getenv(EnvGoLatestDisable) != "" { return &CheckResponse{}, nil } // Convert target to *version.Version targetV, err := version.NewVersion(target) if err != nil { return nil, fmt.Errorf("failed to parse %s, %s", target, err.Error()) } // Validate source if err = s.Validate(); err != nil { return nil, err } fr, err := s.Fetch() if err != nil { return nil, err } // Source must has at leaset one version information versions := fr.Versions if len(fr.Versions) == 0 { return nil, fmt.Errorf("no version to compare") } sort.Sort(version.Collection(versions)) currentV := versions[len(versions)-1] var outdated, latest, new bool if targetV.LessThan(currentV) { outdated = true } // If target = current, target is `latest` if targetV.Equal(currentV) { latest = true } // If target > current, target is `latest` and `new` if targetV.GreaterThan(currentV) { latest, new = true, true } return &CheckResponse{ Current: currentV.String(), Outdated: outdated, Latest: latest, New: new, Malformeds: fr.Malformeds, Meta: fr.Meta, }, nil } // newFetchResponse is constructor of FetchResponse. This is only for // implement your own Source func newFetchResponse() *FetchResponse { var versions []*version.Version var malformeds []string return &FetchResponse{ Versions: versions, Malformeds: malformeds, Meta: &Meta{}, } } ================================================ FILE: latest_test.go ================================================ package latest ================================================ FILE: test-fixtures/default.html ================================================ 1.2.3 ================================================ FILE: test-fixtures/default.json ================================================ { "version":"1.2.3", "message":"New version include security update, you should update soon", "url":"http://example.com/info" } ================================================ FILE: test-fixtures/meta.html ================================================ go-latest

go-latest

This is sample HTML page for go-latest

================================================ FILE: test-fixtures/original.html ================================================ go-latest
go-latest
1.2.3
1.2.4
1.2.5
New version include security update, you should update soon
================================================ FILE: test-fixtures/original.json ================================================ { "name":"go-latest", "version_info":"v1.0.0", "status": "We are releasing now" } ================================================ FILE: wercker.yml ================================================ box: tcnksm/gox build: steps: - setup-go-workspace - script: name: go version code: | go version - script: name: go get code: | go get -t ./... - script: name: go test code: | go test