-
Notifications
You must be signed in to change notification settings - Fork 0
/
nexusutil.go
113 lines (102 loc) · 2.54 KB
/
nexusutil.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
json2 "encoding/json"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
)
type nexusItems struct {
Items []nexusComponent `json:"items"`
ContinuationToken string `json:"continuationToken"`
}
type nexusComponent struct {
Id string `json:"id"`
Repository string `json:"repository"`
Format string `json:"format"`
Group string `json:"group"`
Name string `json:"name"`
Version string `json:"version"`
Assets []nexusAsset `json:"assets"`
}
type nexusAsset struct {
DownloadUrl string `json:"download_url"`
Path string `json:"path"`
Id string `json:"id"`
Repository string `json:"repository"`
Format string `json:"format"`
Checksum nexusChecksum `json:"checksum"`
}
type nexusChecksum struct {
Sha1 string `json:"sha_1"`
Sha256 string `json:"sha_256"`
Md5 string `json:"md_5"`
}
func NexusGetAssets(url string) []byte {
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
components, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
return components
}
func NexusExtractVersions(components []byte) []string {
var nComponents nexusItems
err := json2.Unmarshal(components, &nComponents)
if err != nil {
log.Fatal(err)
}
versions := make([]string, 0, cap(nComponents.Items))
for _, item := range nComponents.Items {
versions = append(versions, item.Version)
}
return versions
}
func sortSemVer(versions []string, i, j int, reverse bool) bool {
//TODO: this function should only sort if versions are true semVer
splitVersions1 := strings.Split(versions[i], ".")
splitVersions2 := strings.Split(versions[j], ".")
major1, _ := strconv.Atoi(string(splitVersions1[0]))
major2, _ := strconv.Atoi(string(splitVersions2[0]))
if reverse {
if major1 > major2 {
return true
}
} else {
if major1 < major2 {
return true
}
}
if len(splitVersions1) >= 2 && len(splitVersions2) >= 2 {
minor1, _ := strconv.Atoi(string(splitVersions1[1]))
minor2, _ := strconv.Atoi(string(splitVersions2[1]))
if reverse {
if minor1 > minor2 {
return true
}
} else {
if minor1 < minor2 {
return true
}
}
}
if len(splitVersions1) == 3 && len(splitVersions2) == 3 {
patch1, _ := strconv.Atoi(string(splitVersions1[2]))
patch2, _ := strconv.Atoi(string(splitVersions2[2]))
if reverse {
if patch1 > patch2 {
return true
}
} else {
if patch1 < patch2 {
return true
}
}
}
return false
}