-
Notifications
You must be signed in to change notification settings - Fork 7
/
release.go
92 lines (84 loc) · 2.06 KB
/
release.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 (
"encoding/json"
"fmt"
"github.com/fatih/color"
"io"
"net/http"
"strings"
)
type GithubRelease struct {
Prerelease bool `json:"prerelease"`
TagName string `json:"tag_name"`
}
func GetNewRelease() (newVersion string) {
resp, err := http.Get("https://api.github.com/repos/harness/migrator/releases")
if err != nil {
return
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}
if resp.StatusCode != 200 {
return
}
releases := []GithubRelease{}
err = json.Unmarshal(body, &releases)
if err != nil {
return
}
// The newest release includes both release & pre-release
var latest GithubRelease
// Latest stable stable
var latestStableRelease GithubRelease
isPreRelease := strings.Contains(Version, "beta")
for _, v := range releases {
if len(latest.TagName) == 0 {
latest = v
}
if !v.Prerelease && len(latestStableRelease.TagName) == 0 {
latestStableRelease = v
}
if v.TagName == Version && v.Prerelease {
isPreRelease = true
}
}
if Version == latest.TagName {
return
}
if !isPreRelease && latestStableRelease.TagName == Version {
return
}
if !latest.Prerelease {
return latest.TagName
}
if isPreRelease {
return latest.TagName
}
if !isPreRelease && latestStableRelease.TagName != Version {
return latestStableRelease.TagName
}
return
}
func CheckGithubForReleases() {
if Version == "development" {
return
}
newRelease := GetNewRelease()
if len(newRelease) > 0 {
printUpgradeMessage(Version, newRelease)
}
}
func printUpgradeMessage(from string, to string) {
blue := color.New(color.FgHiBlue).SprintFunc()
green := color.New(color.FgGreen).SprintFunc()
red := color.New(color.FgHiRed).SprintFunc()
yellow := color.New(color.FgYellow).SprintFunc()
fmt.Printf("[%s] A new release of harness-upgrade is available: %s → %s\n", blue("notice"), red(from), green(to))
fmt.Printf("%s\n", yellow("https://github.com/harness/migrator/releases/tag/"+to))
fmt.Printf("To update, run: %s\n", green("harness-upgrade update"))
}