-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
65 lines (50 loc) · 1.64 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/fatih/color"
)
const repoURL = "https://api.github.com/repos/dreth/hbd-cli/releases/latest"
type GitHubRelease struct {
TagName string `json:"tag_name"`
}
// GetLatestVersion fetches the latest release version from GitHub
func GetLatestVersion() (string, error) {
client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Get(repoURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
var release GitHubRelease
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return "", err
}
return release.TagName, nil
}
// CheckForNewVersion compares the current version with the latest GitHub version
func CheckForNewVersion() string {
// Get the latest version
latestVersion, err := GetLatestVersion()
c := color.New(color.FgRed, color.Italic)
if err != nil {
return c.Sprintf(" Failed to check for the latest version: %s\n", err)
}
if latestVersion != "" {
c2 := color.New(color.FgGreen, color.Bold)
if fmt.Sprintf("v%s", Version) != latestVersion {
// Print the new version message in red and bold
return c.Sprintf("\n A new version of hbd-cli is available: %s (current: %s)\n", latestVersion, Version) + c2.Sprint(" Download it: https://github.com/dreth/hbd-cli/releases/latest")
}
c3 := color.New(color.FgBlue, color.Bold)
return c3.Sprintf(" You are using the latest version of hbd-cli: %s\n", Version)
}
return ""
}
// Print the current version of the CLI
func HBDCLIVersion() string {
c := color.New(color.FgBlue, color.Bold)
return c.Sprintf(` hbd-cli version: %s`+"\n", Version) + CheckForNewVersion()
}