-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
72 lines (61 loc) · 1.67 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
66
67
68
69
70
71
72
package main
import (
"bufio"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)
// versionCheck function to return the local and remote version
func versionCheck(msg string) string {
fmt.Println()
fmt.Println(msg)
fmt.Println()
return msg
}
// versionRemotefunction the retrieves the remote ersion and return it's value
func versionRemote() (msg string, remote string, upgrade bool) {
upgrade = false
remote_version := ""
file := filepath.Base(os.Args[0])
lines, err := UrlToLines("https://raw.githubusercontent.com/gcclinux/scmd/main/release")
if err != nil {
log.Println(err)
}
for _, line := range lines {
remote_version = line
}
local_int := strings.Replace(Release, ".", "", -1)
remote_int := strings.Replace(remote_version, ".", "", -1)
if local_int == remote_int {
msg = fmt.Sprintf("Currently \"%v\" is already at the latest version (%v)", file, remote_version)
} else if local_int > remote_int {
msg = fmt.Sprintf("Currently \"%v\" (%v) is newer than the online version (%v)", file, Release, remote_version)
} else if local_int < remote_int {
msg = fmt.Sprintf("Newer version for \"%v\" (%v) is available for downloading v(%v)", file, Release, remote_version)
upgrade = true
}
return msg, remote_version, upgrade
}
func UrlToLines(url string) ([]string, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return LinesFromReader(resp.Body)
}
func LinesFromReader(r io.Reader) ([]string, error) {
var lines []string
scanner := bufio.NewScanner(r)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return lines, nil
}