-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (63 loc) · 1.54 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
67
68
69
70
71
72
73
74
75
76
package main
import (
"bufio"
"flag"
"fmt"
"github.com/blang/semver"
"github.com/rhysd/go-github-selfupdate/selfupdate"
"log"
"os"
"strings"
)
const version = "1.0.2"
func usage() {
_, _ = fmt.Fprintln(os.Stderr, "Usage: selfupdate-example [flags]\n")
flag.PrintDefaults()
}
func confirmAndSelfUpdate(slug string) {
latest, found, err := selfupdate.DetectLatest(slug)
if err != nil {
log.Println("Error occurred while detecting version:", err)
return
}
v := semver.MustParse(version)
if !found || latest.Version.LTE(v) {
log.Println("Current version is the latest")
return
}
fmt.Print("Do you want to update to", latest.Version, "? (y/n): ")
input, err := bufio.NewReader(os.Stdin).ReadString('\n')
s := strings.TrimSpace (input)
if err != nil || (s != "y" && s != "n") {
log.Println("Invalid input")
return
}
if input == "n\n" {
return
}
exe, err := os.Executable()
if err != nil {
log.Println("Could not locate executable path")
return
}
selfupdate.EnableLog()
if err := selfupdate.UpdateTo(latest.AssetURL, exe); err != nil {
log.Println("Error occurred while updating binary:", err)
return
}
log.Println("Successfully updated to version", latest.Version)
}
func main(){
ver := flag.Bool("version", false, "Show version")
slug := flag.String("slug", "luisfercho/go-github-selfupdate-test", "Repository of this command")
flag.Usage = usage
flag.Parse()
_ = os.Setenv("GITHUB_TOKEN","")
if *ver {
fmt.Println(version)
os.Exit(0)
}
selfupdate.EnableLog()
confirmAndSelfUpdate(*slug)
os.Exit(0)
}