-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (54 loc) · 1.71 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
package main
import (
"encoding/json"
"fmt"
"os"
odin "github.com/dream11/odin/app"
"github.com/dream11/odin/internal/cli"
"github.com/dream11/odin/internal/ui"
"github.com/dream11/odin/pkg/request"
)
var logger ui.Logger
const GITHUB_TAGS_URL = "https://api.github.com/repos/dream11/odin/tags"
func getLatestVersion() string {
req := request.Request{
Method: "GET",
URL: GITHUB_TAGS_URL,
}
res := req.Make()
if res.Error != nil {
logger.Debug("Error making http request to fetch latest version: " + res.Error.Error())
return ""
}
if res.StatusCode != 200 {
logger.Debug("Invalid status code while checking latest version of Odin: " + fmt.Sprint(res.StatusCode))
return ""
}
var jsonResponse []map[string]interface{}
err := json.Unmarshal(res.Body, &jsonResponse)
if err != nil {
logger.Debug("Unable to unmarshal latest version response : " + err.Error())
return ""
}
// return the latest tag
return jsonResponse[0]["name"].(string)
}
func isLatestVersion(currentVersion string, latestVersion string) bool {
return currentVersion == latestVersion
}
func main() {
c := cli.Cli(odin.App.Name, odin.App.Version)
exitStatus, err := c.Run()
latestVersion := getLatestVersion()
if latestVersion != "" && !isLatestVersion(odin.App.Version, latestVersion) {
logger.Info(fmt.Sprintf("\nYou are using odin version %s; however, version %s is available", odin.App.Version, latestVersion))
logger.Info("Upgrade to the latest version via command 'brew install dream11/tools/odin'")
}
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
os.Exit(exitStatus)
}
// TODO: https://github.com/mitchellh/go-glint
// TODO: https://github.com/charmbracelet/bubbletea for advanced interactions with user