-
Notifications
You must be signed in to change notification settings - Fork 1
/
version.go
87 lines (73 loc) · 1.89 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
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
package version
import (
"bytes"
"encoding/json"
"fmt"
"github.com/Masterminds/semver"
"github.com/hazelops/ize/internal/requirements"
"github.com/pterm/pterm"
"log"
"net/http"
"runtime"
"time"
)
var (
GitCommit string
Version = "development"
)
func FullVersionNumber() string {
var versionString bytes.Buffer
if Version == "development" {
return fmt.Sprintf("development %s", time.Now().Format("2006-01-02T15:04:05"))
}
fmt.Fprintf(&versionString, "%s", Version)
if GitCommit != "" {
fmt.Fprintf(&versionString, " (%s)", GitCommit)
}
return versionString.String()
}
func CheckLatestRelease() {
_, err := semver.NewVersion(Version)
if err != nil {
return
}
resp, err := http.Get("https://api.github.com/repos/hazelops/ize/releases/latest")
if err != nil {
log.Fatalln(err)
}
var gr gitResponse
if err := json.NewDecoder(resp.Body).Decode(&gr); err != nil {
log.Fatal(err)
}
var versionChangeAction = "upgrading"
if Version > gr.Version {
versionChangeAction = "downgrading"
}
if Version != gr.Version {
pterm.Warning.Printfln("The newest stable version is %s, but your version is %s. Consider %s.", gr.Version, Version, versionChangeAction)
ShowUpgradeCommand()
}
}
type gitResponse struct {
Version string `json:"tag_name"`
}
func ShowUpgradeCommand() error {
switch goos := runtime.GOOS; goos {
case "darwin":
pterm.Warning.Println("Use the command to update\n:\tbrew upgrade ize")
case "linux":
distroName, err := requirements.ReadOSRelease("/etc/os-release")
if err != nil {
return err
}
switch distroName["ID"] {
case "ubuntu":
pterm.Warning.Println("Use the command to update:\n\tapt update && apt install ize")
default:
pterm.Warning.Println("See https://github.com/hazelops/ize/blob/main/DOCS.md#installation")
}
default:
pterm.Warning.Println("See https://github.com/hazelops/ize/blob/main/DOCS.md#installation")
}
return nil
}