-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmap.go
177 lines (140 loc) · 4.21 KB
/
map.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package redirector
import (
"encoding/csv"
"encoding/json"
"errors"
"io"
"net/url"
"os"
"path"
"strings"
log "github.com/sirupsen/logrus"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// ErrUnsupportedFormat is returned when an unsupported map format is used.
var ErrUnsupportedFormat = errors.New("unsupported map format")
// loadMapFile loads a file as a map
func loadMapFile(file string) (map[string]string, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
ext := path.Ext(file)
switch ext {
case ".csv":
return loadMapCSV(f)
case ".json":
return loadMapJSON(f)
}
return nil, ErrUnsupportedFormat
}
// loadMapCSV loads a pipe separated file of mappings
func loadMapCSV(f io.Reader) (map[string]string, error) {
m := make(map[string]string)
r := csv.NewReader(f)
r.Comma = '|'
for {
row, err := r.Read()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
m[strings.TrimLeft(row[0], "/")] = strings.TrimLeft(row[1], "/")
}
return m, nil
}
// Map represents a JSON format of an asset list
type Map struct {
Assets []ReleaseFile `json:"assets"`
}
// ReleaseFile represents a file to be mapped
type ReleaseFile struct {
BoardSlug string `json:"board_slug"`
FileURL string `json:"file_url"`
FileUpdated string `json:"file_updated"`
FileSize string `json:"file_size"`
DistroRelease string `json:"distro_release"`
KernelBranch string `json:"kernel_branch"`
ImageVariant string `json:"image_variant"`
Preinstalled string `json:"preinstalled_application"`
Promoted string `json:"promoted"`
Repository string `json:"download_repository"`
Extension string `json:"file_extension"`
}
var distroCaser = cases.Title(language.Und)
var imageExtensions = []string{"img.xz", "img.qcow2.xz", "boot.bin.xz"}
// loadMapJSON loads a map file from JSON, based on the format specified in the github issue.
// See: https://github.com/armbian/os/pull/129
func loadMapJSON(f io.Reader) (map[string]string, error) {
m := make(map[string]string)
var data Map
if err := json.NewDecoder(f).Decode(&data); err != nil {
return nil, err
}
for _, file := range data.Assets {
// Because download mapping a full URL, redirecting, and finding a server again is redundant,
// we parse the URL and only return the path here. Previously, it would use https://dl.armbian.com/PATH
// which is not supported, as the redirector will always prepend a server
u, err := url.Parse(file.FileURL)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"uri": file.FileURL,
}).Warning("Error parsing redirect url or path")
continue
}
var sb strings.Builder
if file.Repository == "os" {
sb.WriteString("nightly/")
}
sb.WriteString(file.BoardSlug)
sb.WriteString("/")
sb.WriteString(distroCaser.String(file.DistroRelease))
sb.WriteString("_")
sb.WriteString(file.KernelBranch)
sb.WriteString("_")
sb.WriteString(file.ImageVariant)
if file.Preinstalled != "" {
sb.WriteString("-")
sb.WriteString(file.Preinstalled)
}
// Check special case for some extensions
switch {
case strings.Contains(file.Extension, "boot-sms.img.xz"):
sb.WriteString("-boot-sms")
case strings.Contains(file.Extension, "boot-boe.img.xz"):
sb.WriteString("-boot-boe")
case strings.Contains(file.Extension, "boot-csot.img.xz"):
sb.WriteString("-boot-csot")
case strings.Contains(file.Extension, "rootfs.img.xz"):
sb.WriteString("-rootfs")
case strings.Contains(file.Extension, "img.qcow2.xz"):
sb.WriteString("-qcow2")
case strings.Contains(file.Extension, "boot.bin.xz"):
sb.WriteString("-uboot-bin")
}
// Add board into the map without an extension
for _, ext := range imageExtensions {
if strings.HasSuffix(file.Extension, ext) {
m[sb.String()] = u.Path
break
}
}
sb.WriteString(".")
if strings.HasSuffix(file.Extension, ".sha") {
sb.WriteString("sha")
} else if strings.HasSuffix(file.Extension, ".asc") {
sb.WriteString("asc")
} else if strings.HasSuffix(file.Extension, ".torrent") {
sb.WriteString("torrent")
} else {
sb.WriteString(file.Extension)
}
m[sb.String()] = u.Path // Add board into the map with an extension
}
return m, nil
}