|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | | - "os" |
6 | | - "os/exec" |
7 | | - "path/filepath" |
8 | | - "runtime" |
9 | | - "strconv" |
10 | | - "strings" |
11 | | - "time" |
12 | | - |
13 | | - "github.com/Bananenpro/cli" |
14 | | - "github.com/adrg/xdg" |
15 | | - "github.com/code-game-project/go-utils/external" |
16 | | - "github.com/code-game-project/go-utils/semver" |
17 | | - |
18 | | - cgExec "github.com/code-game-project/go-utils/exec" |
19 | | - |
20 | 4 | "github.com/code-game-project/codegame-cli/cmd" |
21 | 5 | ) |
22 | 6 |
|
23 | | -const version = "dev" |
| 7 | +const version = "v0.8.0" |
24 | 8 |
|
25 | 9 | func main() { |
26 | | - checkVersion() |
27 | 10 | cmd.Execute(version) |
28 | 11 | } |
29 | | - |
30 | | -// checkVersion prints a warning, if there is a newer version of codegame-cli available. |
31 | | -// On macOS and linux the user is offered to update automatically. |
32 | | -func checkVersion() { |
33 | | - if version == "dev" { |
34 | | - return |
35 | | - } |
36 | | - |
37 | | - latest, err := getLatestVersion() |
38 | | - if err != nil { |
39 | | - return |
40 | | - } |
41 | | - |
42 | | - currentMajor, currentMinor, currentPatch, err := semver.ParseVersion(version) |
43 | | - if err != nil { |
44 | | - return |
45 | | - } |
46 | | - |
47 | | - latestMajor, latestMinor, latestPatch, err := semver.ParseVersion(latest) |
48 | | - if err != nil { |
49 | | - return |
50 | | - } |
51 | | - |
52 | | - if latestMajor > currentMajor || (latestMajor == currentMajor && latestMinor > currentMinor) || (latestMajor == currentMajor && latestMinor == currentMinor && latestPatch > currentPatch) { |
53 | | - _, shErr := exec.LookPath("sh") |
54 | | - _, sudoErr := exec.LookPath("sudo") |
55 | | - _, curlErr := exec.LookPath("curl") |
56 | | - _, tarErr := exec.LookPath("tar") |
57 | | - cgBin, codegameErr := os.Stat("/usr/local/bin/codegame") |
58 | | - if codegameErr == nil && !cgBin.IsDir() && shErr == nil && sudoErr == nil && curlErr == nil && tarErr == nil && (runtime.GOOS == "darwin" || runtime.GOOS == "linux") { |
59 | | - update() |
60 | | - } else { |
61 | | - cli.Warn("You are using an old version of codegame-cli (%s).\nUpdate to the latest version (%s): https://github.com/code-game-project/codegame-cli#installation", version, latest) |
62 | | - } |
63 | | - } |
64 | | -} |
65 | | - |
66 | | -func update() { |
67 | | - yes, err := cli.YesNo("A new version is available. Do you want to update now?", true) |
68 | | - if err != nil { |
69 | | - os.Exit(0) |
70 | | - } |
71 | | - if !yes { |
72 | | - return |
73 | | - } |
74 | | - |
75 | | - _, err = cgExec.Execute(false, "sh", "-c", fmt.Sprintf("curl -L https://github.com/code-game-project/codegame-cli/releases/latest/download/codegame-cli-%s-%s.tar.gz | tar -xz codegame && sudo mv codegame /usr/local/bin", runtime.GOOS, runtime.GOARCH)) |
76 | | - if err != nil { |
77 | | - cli.Error("Update failed.") |
78 | | - os.Exit(1) |
79 | | - } |
80 | | - cli.Success("Update successful.") |
81 | | - os.Exit(0) |
82 | | -} |
83 | | - |
84 | | -func getLatestVersion() (string, error) { |
85 | | - cacheDir := filepath.Join(xdg.CacheHome, "codegame", "cli") |
86 | | - os.MkdirAll(cacheDir, 0o755) |
87 | | - |
88 | | - content, err := os.ReadFile(filepath.Join(cacheDir, "latest_version")) |
89 | | - if err == nil { |
90 | | - parts := strings.Split(string(content), "\n") |
91 | | - if len(parts) >= 2 { |
92 | | - cacheTime, err := strconv.Atoi(parts[0]) |
93 | | - if err == nil && time.Now().Unix()-int64(cacheTime) <= 60*60*3 { |
94 | | - return parts[1], nil |
95 | | - } |
96 | | - } |
97 | | - } |
98 | | - |
99 | | - tag, err := external.LatestGithubTag("code-game-project", "codegame-cli") |
100 | | - if err != nil { |
101 | | - return "", err |
102 | | - } |
103 | | - os.WriteFile(filepath.Join(cacheDir, "latest_version"), []byte(fmt.Sprintf("%d\n%s", time.Now().Unix(), version)), 0o644) |
104 | | - return tag, nil |
105 | | -} |
0 commit comments