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 ==== [][release] [][wercker] [][license] [][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() //
This is sample HTML page for go-latest
================================================ FILE: test-fixtures/original.html ================================================