-
Notifications
You must be signed in to change notification settings - Fork 76
/
main.go
66 lines (58 loc) · 1.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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)
}
}
}
}