/
check.go
52 lines (50 loc) · 1.26 KB
/
check.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
package version
import (
"fmt"
"github.com/blang/semver"
"github.com/spf13/cobra"
"io"
"net/http"
"strings"
"time"
)
func check() *cobra.Command {
return &cobra.Command{
Use: "check",
Short: "Check for a patch for the CLI",
RunE: func(cmd *cobra.Command, args []string) error {
client := http.Client{Timeout: time.Minute * 3}
reqUrl := fmt.Sprintf("https://raw.githubusercontent.com/arlonproj/arlon/v%s/version", strings.Join(strings.Split(cliVersion, ".")[:2], "."))
req, err := http.NewRequest(http.MethodGet, reqUrl, nil)
if err != nil {
return err
}
res, err := client.Do(req)
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(res.Body)
version, err := io.ReadAll(res.Body)
if err != nil {
return err
}
latestVersion := strings.TrimSpace(string(version))
ver, err := semver.Parse(latestVersion)
if err != nil {
return err
}
currentVersion, err := semver.Parse(cliVersion)
if err != nil {
return err
}
if ver.Compare(currentVersion) == 1 {
fmt.Printf("Arlon CLI version %s is outdated. New patch %s available\n", cliVersion, ver.String())
return nil
}
fmt.Printf("Arlon CLI version %s up-to-date\n", cliVersion)
return nil
},
}
}