-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
92 lines (77 loc) · 1.58 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"passline/pkg/out"
"github.com/blang/semver"
"github.com/rhysd/go-github-selfupdate/selfupdate"
"golang.org/x/term"
)
const (
name = "passline"
repo = "perryrh0dan/passline"
)
var (
// Version is the released version of passline
version string = "1.14.2"
// BuildTime is the time the binary was built
date string
)
func main() {
ctx := context.Background()
// Get the initial state of the terminal.
initialTermState, _ := term.GetState(int(syscall.Stdin))
//trap Ctrl+C and call cancel on the context
ctx, cancel := context.WithCancel(ctx)
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, os.Kill)
defer func() {
signal.Stop(c)
cancel()
}()
go func() {
select {
case <-c:
cancel()
exit(initialTermState)
case <-ctx.Done():
cancel()
exit(initialTermState)
}
}()
sv := getVersion()
// check for updates
latest, found, _ := selfupdate.DetectLatest(repo)
if found && latest.Version.GT(sv) {
out.UpdateFound(sv, latest.Version)
}
ctx, app := setupApp(ctx, sv)
if err := app.RunContext(ctx, os.Args); err != nil {
log.Fatal(err)
}
}
func exit(initialTermState *term.State) {
_ = term.Restore(int(syscall.Stdin), initialTermState)
fmt.Println()
os.Exit(1)
}
func getVersion() semver.Version {
sv, err := semver.Parse(strings.TrimPrefix(version, "v"))
if err == nil {
return sv
}
return semver.Version{
Major: 1,
Minor: 9,
Patch: 2,
Pre: []semver.PRVersion{
{VersionStr: "git"},
},
Build: []string{"HEAD"},
}
}