-
Notifications
You must be signed in to change notification settings - Fork 246
/
versions.go
70 lines (55 loc) · 1.83 KB
/
versions.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
package releases
import (
"context"
"fmt"
"runtime"
"runtime/debug"
"github.com/jzelinskie/cobrautil/v2"
"golang.org/x/mod/semver"
)
// CurrentVersion returns the current version of the binary.
func CurrentVersion() (string, error) {
bi, ok := debug.ReadBuildInfo()
if !ok {
return "", fmt.Errorf("failed to read BuildInfo because the program was compiled with Go %s", runtime.Version())
}
return cobrautil.VersionWithFallbacks(bi), nil
}
// SoftwareUpdateState is the state of this software relative to whether updates are available.
type SoftwareUpdateState int
const (
// Unknown indicates an unknown state.
Unknown SoftwareUpdateState = iota
// UnreleasedVersion indicates that the current software version running is an unreleased version.
UnreleasedVersion
// UpdateAvailable indicates an update is available.
UpdateAvailable
// UpToDate indicates that the software is fully up-to-date.
UpToDate
)
// CheckIsLatestVersion returns whether the current version of the software is the latest released.
// Returns the state, as well as the latest release.
func CheckIsLatestVersion(
ctx context.Context,
getCurrentVersion func() (string, error),
getLatestRelease func(context.Context) (*Release, error),
) (SoftwareUpdateState, string, *Release, error) {
currentVersion, err := getCurrentVersion()
if err != nil {
return Unknown, currentVersion, nil, err
}
if currentVersion == "" || !semver.IsValid(currentVersion) {
return UnreleasedVersion, currentVersion, nil, nil
}
release, err := getLatestRelease(ctx)
if err != nil {
return Unknown, currentVersion, nil, err
}
if !semver.IsValid(release.Version) {
return Unknown, currentVersion, nil, err
}
if semver.Compare(currentVersion, release.Version) < 0 {
return UpdateAvailable, currentVersion, release, nil
}
return UpToDate, currentVersion, release, nil
}