-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathos.go
108 lines (88 loc) · 1.93 KB
/
os.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
package main
import (
"encoding/json"
"io"
"net/http"
"os"
"path"
"sort"
"strconv"
"strings"
"github.com/blang/semver"
)
var allowedRange = semver.MustParseRange(">=1.0.0-beta0")
type File struct {
Name string `json:"name"`
Url string `json:"browser_download_url"`
}
type Release struct {
Version semver.Version
Tag string `json:"tag_name"`
Files []File `json:"assets"`
}
type Releases []Release
func (rs Releases) Len() int {
return len(rs)
}
func (rs Releases) Less(i, j int) bool {
return rs[i].Version.LT(rs[j].Version)
}
func (rs Releases) Swap(i, j int) {
rs[i], rs[j] = rs[j], rs[i]
}
func getOSReleases() (Releases, error) {
res, err := http.Get("https://api.github.com/repos/nlf/dlite-os/releases")
if err != nil {
return nil, err
}
defer res.Body.Close()
releases := Releases{}
decoder := json.NewDecoder(res.Body)
err = decoder.Decode(&releases)
if err != nil {
return nil, err
}
allowedReleases := Releases{}
for _, release := range releases {
release.Version = semver.MustParse(strings.TrimPrefix(release.Tag, "v"))
if allowedRange(release.Version) {
allowedReleases = append(allowedReleases, release)
}
}
sort.Sort(allowedReleases)
return allowedReleases, nil
}
func getLatestOSRelease() (Release, error) {
releases, err := getOSReleases()
if err != nil {
return Release{}, err
}
return releases[len(releases)-1], nil
}
func downloadOS(target string) error {
latest, err := getLatestOSRelease()
if err != nil {
return err
}
for _, file := range latest.Files {
tempPath := path.Join(target, file.Name)
output, err := os.Create(tempPath)
if err != nil {
return err
}
res, err := http.Get(file.Url)
defer res.Body.Close()
if err != nil {
return err
}
length, err := strconv.ParseInt(res.Header.Get("Content-Length"), 10, 64)
if err != nil {
return err
}
_, err = io.CopyN(output, res.Body, length)
if err != nil {
return err
}
}
return nil
}