diff --git a/.appveyor.yml b/.appveyor.yml index f7acf09..077f75c 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -13,6 +13,7 @@ build: off test_script: - go build ./selfupdate - go build ./cmd/selfupdate-example + - go build ./cmd/detect-latest-release - go test -v -race -coverprofile=coverage.txt ./selfupdate after_test: - "SET PATH=C:\\Python34;C:\\Python34\\Scripts;%PATH%" diff --git a/.gitignore b/.gitignore index 609fb35..9b2000f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /selfupdate-example /release /env.sh +/detect-latest-release diff --git a/.travis.yml b/.travis.yml index ce53262..571180d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,7 @@ install: script: - go build ./selfupdate/ - go build ./cmd/selfupdate-example/ + - go build ./cmd/detect-latest-release/ - go test -v -race -coverprofile=coverage.txt ./selfupdate after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/README.md b/README.md index e6f4f1b..52774d4 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,10 @@ If newer version than itself is detected, it downloads released binary from GitH - Tested on Linux, macOS and Windows (using Travis CI and AppVeyor) - Many archive and compression formats are supported (zip, gzip, tar) +And small wrapper CLI is provided: + +- [detect-latest-release](./cmd/detect-latest-release): Detect the latest release of given GitHub repository from command line + [go-github-selfupdate]: https://github.com/rhysd/go-github-selfupdate [GitHub Releases API]: https://developer.github.com/v3/repos/releases/ diff --git a/cmd/detect-latest-release/README.md b/cmd/detect-latest-release/README.md new file mode 100644 index 0000000..06cfd0d --- /dev/null +++ b/cmd/detect-latest-release/README.md @@ -0,0 +1,14 @@ +This command line tool is a small wrapper of [`selfupdate.DetectLatest()`](https://godoc.org/github.com/rhysd/go-github-selfupdate/selfupdate#DetectLatest). + +Please install using `go get`. + +``` +$ go get -u github.com/rhysd/go-github-selfupdate/cmd/detect-latest-release +``` + +To know the usage, please try the command without any argument. + +``` +$ detect-latest-release +``` + diff --git a/cmd/detect-latest-release/main.go b/cmd/detect-latest-release/main.go new file mode 100644 index 0000000..bf8de19 --- /dev/null +++ b/cmd/detect-latest-release/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "flag" + "fmt" + "github.com/rhysd/go-github-selfupdate/selfupdate" + "os" + "regexp" + "strings" +) + +func usage() { + fmt.Fprintln(os.Stderr, "Usage: detect-latest-release [flags] {repo}\n\n {repo} must be URL to GitHub repository or in 'owner/name' format.\n\nFlags:\n") + flag.PrintDefaults() +} + +func main() { + asset := flag.Bool("asset", false, "Output URL to asset") + notes := flag.Bool("release-notes", false, "Output release notes additionally") + url := flag.Bool("url", false, "Output URL for release page") + + flag.Usage = usage + flag.Parse() + + if flag.NArg() != 1 { + usage() + os.Exit(1) + } + + repo := flag.Arg(0) + if strings.HasPrefix(repo, "https://") { + repo = repo[len("https://"):] + } + if strings.HasPrefix(repo, "github.com/") { + repo = repo[len("github.com/"):] + } + + matched, err := regexp.MatchString("[^/]+/[^/]+", repo) + if err != nil { + panic(err) + } + if !matched { + usage() + os.Exit(1) + } + + latest, found, err := selfupdate.DetectLatest(repo) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + if !found { + fmt.Println("No release was found") + } else { + if *asset { + fmt.Println(latest.AssetURL) + } else if *url { + fmt.Println(latest.URL) + } else { + fmt.Println(latest.Version) + if *notes { + fmt.Printf("\nRelease Notes:\n%s\n", latest.ReleaseNotes) + } + } + } +} diff --git a/selfupdate/doc.go b/selfupdate/doc.go index e162a45..3d11cd9 100644 --- a/selfupdate/doc.go +++ b/selfupdate/doc.go @@ -30,5 +30,8 @@ Naming Rules of Git Tags: This package is hosted on GitHub: https://github.com/rhysd/go-github-selfupdate + +Small CLI tool as wrapper of this library is available also: + https://github.com/rhysd/go-github-selfupdate/cmd/detect-latest-release */ package selfupdate