-
Notifications
You must be signed in to change notification settings - Fork 0
/
apple.go
95 lines (91 loc) · 2.32 KB
/
apple.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
package distrodetector
import (
"encoding/xml"
"errors"
"io/ioutil"
"net/http"
"strings"
)
type versionInfo struct {
Name string `xml:"name"`
ConfigCode string `xml:"configCode"`
Locale string `xml:"locale"`
}
// AppleCodename returns a codename, or an empty string.
// Will first use the lookup table, and then try to fetch it from Apple over HTTP.
func AppleCodename(version string) string {
// See also: https://en.wikipedia.org/wiki/MacOS_version_history#Releases
var appleCodeNames = map[string]string{
"10.0": "Cheetah",
"10.1": "Puma",
"10.2": "Jaguar",
"10.3": "Panther",
"10.4": "Tiger",
"10.5": "Leopard",
"10.6": "Snow Leopard",
"10.7": "Lion",
"10.8": "Mountain Lion",
"10.9": "Mavericks",
"10.10": "Yosemite",
"10.11": "El Capitan",
"10.12": "Sierra",
"10.13": "High Sierra",
"10.14": "Mojave",
"10.15": "Catalina",
"11.0": "Big Sur",
"12.0": "Monterey",
"13.0": "Ventura",
}
// Search the keys, longest keys first
for keyLength := 5; keyLength >= 4; keyLength-- {
for k, v := range appleCodeNames {
if len(k) == keyLength {
if strings.HasPrefix(version, k) {
return v
}
}
}
}
// No codename found, use one with a matching major version number
majorVersionAndDot := version
if strings.Contains(version, ".") {
fields := strings.SplitN(version, ".", 2)
majorVersionAndDot = fields[0] + "."
}
for k, v := range appleCodeNames {
if strings.HasPrefix(k, majorVersionAndDot) {
return v
}
}
// No codename found
return ""
}
// codenameFromApple attempts to fetch the correct codename from Apple,
// given a version string. The URL that is used is:
// https://support-sp.apple.com/sp/product?edid=%s
func codenameFromApple(version string) (string, error) {
URL := "https://support-sp.apple.com/sp/product?edid=" + version
resp, err := http.Get(URL)
if err != nil {
return "", err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
//fmt.Println(string(data))
var vi versionInfo
xml.Unmarshal(data, &vi)
if vi.ConfigCode == "" {
return "", errors.New("No codename returned from " + URL)
}
codename := vi.ConfigCode
if strings.HasPrefix(codename, "macOS ") {
return codename[6:], nil
}
if strings.HasPrefix(codename, "OS X ") {
return codename[5:], nil
}
return codename, nil
}