-
Notifications
You must be signed in to change notification settings - Fork 0
/
portal-info.go
57 lines (43 loc) · 997 Bytes
/
portal-info.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
package soffit
import (
"bytes"
"log"
"github.com/blang/semver"
)
// PortalInfo is the representation of the portal information sent by the uPortal server.
type PortalInfo struct {
Provider string `json:"provider"`
Version semver.Version `json:"version"`
Snapshot bool `json:"snapshot"`
}
var snapshotBytes = []byte("-SNAPSHOT")
// UnmarshalJSON implements json.Unmarshaler
func (p *PortalInfo) UnmarshalJSON(bs []byte) error {
if len(bs) < 2 {
return nil
}
// Reslice, removing quotations from ends
if bs[0] == '"' {
bs = bs[1:]
}
if bs[len(bs)-1] == '"' {
bs = bs[:len(bs)-1]
}
bss := bytes.Split(bs, []byte{'/'})
if len(bss) == 0 {
return nil
}
p.Provider = string(bss[0])
if len(bss) < 2 {
return nil
}
sv := bss[1]
if bytes.Contains(sv, snapshotBytes) {
p.Snapshot = true
sv = bytes.Replace(sv, snapshotBytes, []byte{}, -1)
}
log.Println(string(sv))
var err error
p.Version, err = semver.Parse(string(sv))
return err
}