-
Notifications
You must be signed in to change notification settings - Fork 15
/
mkmanifest.go
350 lines (316 loc) · 7.84 KB
/
mkmanifest.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//go:build ignore
// +build ignore
package main
import (
"archive/zip"
"bufio"
"bytes"
"crypto/sha256"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
func queryReleases(user, repo string) ([]byte, error) {
resp, err := http.Get(
fmt.Sprintf("https://api.github.com/repos/%s/%s/releases", user, repo))
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
type Asset struct {
Name string `json:"name"`
BrowserDownloadUrl string `json:"browser_download_url"`
tagName string
}
type Release struct {
TagName string `json:"tag_name"`
Assets []*Asset `json:"assets"`
}
func getReleases(name, repo string) ([]*Release, error) {
releasesStr, err := queryReleases(name, repo)
if err != nil {
return nil, fmt.Errorf("getReleases: %w", err)
}
var releases []*Release
if err := json.Unmarshal(releasesStr, &releases); err != nil {
return nil, fmt.Errorf("json.Unmarshal: %w", err)
}
return releases, nil
}
func seekAssets(releases []*Release, fname string) (string, string) {
for _, rel := range releases {
for _, a := range rel.Assets {
if a.Name == fname {
return a.BrowserDownloadUrl, rel.TagName
}
}
}
return "", ""
}
func getHash(fname string) (string, error) {
fd, err := os.Open(fname)
if err != nil {
return "", err
}
defer fd.Close()
h := sha256.New()
io.Copy(h, fd)
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
type Archtecture struct {
Url string `json:"url"`
Hash string `json:"hash,omitempty"`
}
type AutoUpdate struct {
Archtectures map[string]*Archtecture `json:"architecture"`
}
type Manifest struct {
Version string `json:"version"`
Description string `json:"description,omitempty"`
Homepage string `json:"homepage,omitempty"`
License string `json:"license,omitempty"`
Archtectures map[string]*Archtecture `json:"architecture"`
Bin []string `json:"bin"`
CheckVer map[string]string `json:"checkver"`
AutoUpdate AutoUpdate `json:"autoupdate"`
}
func getBits(s string) string {
if strings.Contains(s, "386") {
return "32bit"
} else if strings.Contains(s, "amd64") {
return "64bit"
}
return ""
}
func queryDescription(user, repo string) ([]byte, error) {
resp, err := http.Get(
fmt.Sprintf("https://api.github.com/repos/%s/%s", user, repo))
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
type Description struct {
Name string `json:"name"`
Description string `json:"description"`
License map[string]string `json:"license"`
}
func getDescription(user, repo string) (*Description, error) {
bin, err := queryDescription(user, repo)
if err != nil {
return nil, err
}
var desc *Description
if err = json.Unmarshal(bin, &desc); err != nil {
return nil, err
}
return desc, nil
}
var flagInlineTemplate = flag.String("inline", "", "Set template inline")
var flagStdinTemplate = flag.Bool("stdin", false, "Read template from stdin")
func listUpExeInZip(fname string) ([]string, error) {
zr, err := zip.OpenReader(fname)
if err != nil {
return nil, err
}
defer zr.Close()
names := make([]string, 0)
for _, f := range zr.File {
if strings.EqualFold(filepath.Ext(f.Name), ".exe") {
names = append(names, f.Name)
}
}
return names, nil
}
func quote(args []string, f func(string) error) error {
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
r, err := cmd.StdoutPipe()
if err != nil {
return err
}
defer r.Close()
cmd.Start()
sc := bufio.NewScanner(r)
for sc.Scan() {
//println(sc.Text())
if err := f(sc.Text()); err != nil {
return err
}
}
return nil
}
func listUpRemoteBranch() ([]string, error) {
branches := []string{}
quote([]string{"git", "remote", "show"}, func(line string) error {
branches = append(branches, strings.TrimSpace(line))
return nil
})
return branches, nil
}
var rxURL = regexp.MustCompile(`Push +URL: \w+@github.com:(\w+)/(\w+).git`)
func getNameAndRepo() (string, string, error) {
branch, err := listUpRemoteBranch()
if err != nil {
return "", "", err
}
if len(branch) < 1 {
return "", "", errors.New("remote branch not found")
}
var user, repo string
quote([]string{"git", "remote", "show", "-n", branch[0]}, func(line string) error {
m := rxURL.FindStringSubmatch(line)
if m != nil {
user = m[1]
repo = m[2]
return io.EOF
}
return nil
})
return user, repo, nil
}
func writeWithCRLF(source []byte, w io.Writer) error {
for {
before, after, found := bytes.Cut(source, []byte{'\n'})
_, err := w.Write(before)
if err != nil {
return err
}
_, err = w.Write([]byte{'\r', '\n'})
if err != nil {
return err
}
if !found {
return nil
}
source = after
}
}
func mains(args []string) error {
name, repo, err := getNameAndRepo()
if err != nil {
return err
}
if name == "" || repo == "" {
return errors.New("getNameAndRepo: can not find remote repository")
}
//println("name:", name)
//println("repo:", repo)
releases, err := getReleases(name, repo)
if err != nil {
return fmt.Errorf("getReleases: %w", err)
}
arch := make(map[string]*Archtecture)
var url, tag string
var binfiles = map[string]struct{}{}
for _, arg1 := range args {
files, err := filepath.Glob(arg1)
if err != nil {
files = []string{arg1}
}
for _, fname := range files {
bits := getBits(fname)
if bits == "" {
return fmt.Errorf("%s: can not find `386` nor `amd64`", fname)
}
url, tag = seekAssets(releases, fname)
if url == "" {
return fmt.Errorf("%s not found in remote repository", fname)
}
hash, err := getHash(fname)
if err != nil {
return err
}
arch[bits] = &Archtecture{
Url: url,
Hash: hash,
}
if strings.EqualFold(filepath.Ext(fname), ".zip") {
if exefiles, err := listUpExeInZip(fname); err == nil {
for _, fn := range exefiles {
binfiles[fn] = struct{}{}
}
}
}
}
}
var input []byte
if *flagInlineTemplate != "" {
input = []byte(*flagInlineTemplate)
} else if *flagStdinTemplate {
input, err = io.ReadAll(os.Stdin)
if err != nil && err != io.EOF {
return err
}
}
var manifest Manifest
if input != nil {
if err = json.Unmarshal(input, &manifest); err != nil {
return err
}
}
if binfiles != nil {
for exe := range binfiles {
manifest.Bin = append(manifest.Bin, exe)
}
}
if manifest.Archtectures == nil {
manifest.Archtectures = make(map[string]*Archtecture)
}
if manifest.AutoUpdate.Archtectures == nil {
manifest.AutoUpdate.Archtectures = map[string]*Archtecture{}
}
if manifest.Homepage == "" {
manifest.Homepage = fmt.Sprintf(
"https://github.com/%s/%s", name, repo)
}
if manifest.CheckVer == nil {
manifest.CheckVer = make(map[string]string)
}
if _, ok := manifest.CheckVer["github"]; !ok {
manifest.CheckVer["github"] = fmt.Sprintf(
"https://github.com/%s/%s", name, repo)
}
if _, ok := manifest.CheckVer["regex"]; !ok {
manifest.CheckVer["regex"] = "tag/([\\d\\._]+)"
}
for name, val := range arch {
manifest.Archtectures[name] = val
manifest.Version = strings.TrimPrefix(tag, "v")
autoupdate := strings.ReplaceAll(val.Url, manifest.Version, "$version")
bits := getBits(val.Url)
manifest.AutoUpdate.Archtectures[bits] = &Archtecture{Url: autoupdate}
}
if desc, err := getDescription(name, repo); err == nil {
if manifest.Description == "" {
manifest.Description = desc.Description
}
if manifest.License == "" {
manifest.License = desc.License["name"]
}
}
jsonBin, err := json.MarshalIndent(&manifest, "", " ")
if err != nil {
return err
}
return writeWithCRLF(jsonBin, os.Stdout)
}
func main() {
flag.Parse()
if err := mains(flag.Args()); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}