add detect-latest-release command

This commit is contained in:
rhysd 2017-12-30 16:14:06 +09:00
parent 07bc4f2027
commit 597627d0b7
7 changed files with 90 additions and 0 deletions

View File

@ -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%"

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/selfupdate-example
/release
/env.sh
/detect-latest-release

View File

@ -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)

View File

@ -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/

View File

@ -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
```

View File

@ -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)
}
}
}
}

View File

@ -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