Skip to content

Commit 3db9792

Browse files
committed
Warn on new version
1 parent 8af04fa commit 3db9792

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

external/cg_gen_events.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package external
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"net/http"
78
"os"
@@ -18,7 +19,7 @@ var cgGenEventsPath = filepath.Join(xdg.DataHome, "codegame", "bin", "cg-gen-eve
1819
func LatestGithubTag(owner, repo string) (string, error) {
1920
res, err := http.Get(fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", owner, repo))
2021
if err != nil || res.StatusCode != http.StatusOK || !HasContentType(res.Header, "application/json") {
21-
return "", cli.Error("Couldn't access git tags from 'github.com/%s/%s'.", owner, repo)
22+
return "", fmt.Errorf("failed to access git tags from 'github.com/%s/%s'.", owner, repo)
2223
}
2324
defer res.Body.Close()
2425
type response []struct {
@@ -27,15 +28,15 @@ func LatestGithubTag(owner, repo string) (string, error) {
2728
var data response
2829
err = json.NewDecoder(res.Body).Decode(&data)
2930
if err != nil {
30-
return "", cli.Error("Couldn't decode git tag data.")
31+
return "", errors.New("failed to decode git tag data.")
3132
}
3233
return data[0].Name, nil
3334
}
3435

3536
func LatestCGEVersion() (string, error) {
3637
tag, err := LatestGithubTag("code-game-project", "cg-gen-events")
3738
if err != nil {
38-
return "", err
39+
return "", cli.Error("Couldn't determine the latest CGE version: %s", err)
3940
}
4041

4142
return strings.TrimPrefix(strings.Join(strings.Split(tag, ".")[:2], "."), "v"), nil

external/version.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package external
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strconv"
8+
"strings"
9+
"time"
10+
11+
"github.com/adrg/xdg"
12+
"github.com/code-game-project/codegame-cli/cli"
13+
)
14+
15+
const currentVersion = "0.4.1"
16+
17+
func CheckVersion() {
18+
latest, err := getLatestVersion()
19+
if err != nil {
20+
return
21+
}
22+
if currentVersion != latest {
23+
cli.Warn("You are using an old version of codegame-cli (v%s).\nUpdate to the latest version (v%s): https://github.com/code-game-project/codegame-cli#installation", currentVersion, latest)
24+
}
25+
}
26+
27+
func getLatestVersion() (string, error) {
28+
cacheDir := filepath.Join(xdg.CacheHome, "codegame", "cli")
29+
os.MkdirAll(cacheDir, 0755)
30+
31+
content, err := os.ReadFile(filepath.Join(cacheDir, "latest_version"))
32+
if err == nil {
33+
parts := strings.Split(string(content), "\n")
34+
if len(parts) >= 2 {
35+
cacheTime, err := strconv.Atoi(parts[0])
36+
if err == nil && time.Now().Unix()-int64(cacheTime) <= 3*24*60*60 {
37+
return parts[1], nil
38+
}
39+
}
40+
}
41+
42+
tag, err := LatestGithubTag("code-game-project", "codegame-cli")
43+
if err != nil {
44+
return "", err
45+
}
46+
version := strings.TrimPrefix(tag, "v")
47+
os.WriteFile(filepath.Join(cacheDir, "latest_version"), []byte(fmt.Sprintf("%d\n%s", time.Now().Unix(), version)), 0644)
48+
return version, nil
49+
}

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ func main() {
3030
os.Exit(1)
3131
}
3232

33+
external.CheckVersion()
34+
3335
command := strings.ToLower(pflag.Arg(0))
3436

3537
var err error

0 commit comments

Comments
 (0)