-
Notifications
You must be signed in to change notification settings - Fork 81
/
version.go
60 lines (50 loc) · 1.27 KB
/
version.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
package main
import (
"bytes"
"fmt"
"time"
latest "github.com/tcnksm/go-latest"
)
// Name is application name
const Name = "ghr"
// Version is application version
const Version string = "0.17.0"
// GitCommit describes latest commit hash.
// This is automatically extracted by git describe --always.
var GitCommit string
// OutputVersion checks the current version and compares it against releases
// available on GitHub. If there is a newer version available, it prints an
// update warning.
func OutputVersion() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "%s version v%s", Name, Version)
if len(GitCommit) != 0 {
fmt.Fprintf(&buf, " (%s)", GitCommit)
}
fmt.Fprintf(&buf, "\n")
// Check latest version is release or not.
verCheckCh := make(chan *latest.CheckResponse)
go func() {
githubTag := &latest.GithubTag{
Owner: "tcnksm",
Repository: "ghr",
}
res, err := latest.Check(githubTag, Version)
if err != nil {
// Don't return error
Debugf("[ERROR] Check latest version is failed: %s", err)
return
}
verCheckCh <- res
}()
select {
case <-time.After(defaultCheckTimeout):
case res := <-verCheckCh:
if res.Outdated {
fmt.Fprintf(&buf,
"Latest version of ghr is v%s, please upgrade!\n",
res.Current)
}
}
return buf.String()
}