-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathgenerator.go
148 lines (120 loc) · 3.1 KB
/
generator.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
package main
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/sanity-io/litter"
)
// Release comes from github
type Release struct {
Version string
Assets []struct {
URL string `json:"browser_download_url"`
Name string `json:"name"`
Size int64 `json:"size"`
} `json:"assets"`
}
// OutputAsset show concrete asset info
type OutputAsset struct {
Arch string `json:"arch"`
URL string `json:"url"`
Size int64 `json:"size"`
}
// Output marks output item with included assets
type Output struct {
Name string `json:"name"`
Description string `json:"description"`
Assets []OutputAsset `json:"assets"`
}
var timeout = time.Duration(5 * time.Second)
func dialTimeout(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, timeout)
}
func main() {
if len(os.Args) <= 1 {
fmt.Printf("Repo not defined \n")
os.Exit(1)
}
repo := os.Args[1]
fmt.Printf("Parsing for repo: %#v \n", repo)
transport := http.Transport{
Dial: dialTimeout,
}
client := http.Client{
Transport: &transport,
}
req, _ := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", repo), nil)
req.SetBasicAuth(os.Getenv("GH_TOKEN"), "x-oauth-basic")
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Can't get json: %#v \n", err)
os.Exit(1)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var release Release
errJSON := json.Unmarshal(body, &release)
if errJSON != nil {
fmt.Printf("Can't parse json: %#v \n", errJSON)
os.Exit(1)
}
output := []Output{}
osAry := map[string][]OutputAsset{}
fmt.Printf("Release information: %s \n", litter.Sdump(release))
for _, a := range release.Assets {
osName := "Universal"
arch := ""
parts := strings.Split(a.Name, ".")
fmt.Printf("Parts: %#v \n", parts)
subParts := strings.SplitN(parts[len(parts)-2], "_", 2)
if strings.Contains(a.Name, "client") {
osName = "Client"
} else if len(subParts) >= 2 {
// Universal
osName = strings.Title(subParts[0])
arch = subParts[1]
}
if osName == "Darwin" {
osName = "OSx"
} else if osName == "Ios" {
osName = "iOS"
}
if _, ok := osAry[osName]; !ok {
osAry[osName] = []OutputAsset{}
}
arch = strings.Replace(arch, "-", "", -1)
osAry[osName] = append(osAry[osName], OutputAsset{arch, a.URL, a.Size})
}
output = append(output, Output{
Name: "Universal",
Assets: osAry["Universal"],
})
delete(osAry, "Universal")
if _, ok := osAry["Client"]; ok {
output = append(output, Output{
Name: "Client",
Assets: osAry["Client"],
})
delete(osAry, "Client")
}
for i, v := range osAry {
output = append(output, Output{
Name: i,
Assets: v,
})
}
fmt.Printf("Output: %s \n", litter.Sdump(output))
repoParts := strings.Split(repo, "/")
outputJSON, _ := json.Marshal(output)
os.MkdirAll(fmt.Sprintf("src/data/%s", repoParts[1]), os.ModePerm)
errWrite := os.WriteFile(fmt.Sprintf("src/data/%s/platforms.json", repoParts[1]), outputJSON, 0644)
if errWrite != nil {
fmt.Printf("Can't write json: %#v \n", errWrite)
os.Exit(1)
}
}