-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
117 lines (100 loc) · 2.56 KB
/
generate.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
package main
import (
"fmt"
"html/template"
"io"
"net/http"
"os"
"strings"
"sync"
)
var kinds = []string{
"ffmpeg",
"ffprobe",
}
var builds = []struct {
System string
Arch string
}{
{System: "darwin", Arch: "amd64"},
{System: "darwin", Arch: "arm64"},
{System: "windows", Arch: "amd64"},
{System: "windows", Arch: "386"},
{System: "linux", Arch: "amd64"},
{System: "linux", Arch: "arm64"},
{System: "linux", Arch: "arm"},
{System: "linux", Arch: "386"},
{System: "freebsd", Arch: "amd64"},
}
const tag = "b4.4.0-rc.11"
var tpls = template.Must(template.ParseGlob("templates/*.tpl"))
func genGoFile(system, arch string) error {
for _, t := range tpls.Templates() {
targetFile := fmt.Sprintf("%s-%s/%s", system, arch, strings.Trim(t.Name(), ".tpl"))
os.Remove(targetFile)
file, err := os.OpenFile(targetFile, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("open file %s: %v", t.Name(), err)
}
if err := t.Execute(file, map[string]interface{}{
"system": system,
"arch": arch,
}); err != nil {
return fmt.Errorf("execute template %s: %v", t.Name(), err)
}
}
return nil
}
func download(kind, system, arch string) error {
targetFile := system + "-" + arch + "/" + kind
os.Remove(targetFile)
switch arch {
case "386":
arch = "ia32"
case "amd64":
arch = "x64"
}
switch system {
case "windows":
system = "win32"
}
downloadURL := fmt.Sprintf("https://github.com/descriptinc/ffmpeg-ffprobe-static/releases/download/%s/%s-%s-%s", tag, kind, system, arch)
resp, err := http.Get(downloadURL)
if err != nil {
return fmt.Errorf("failed to download %s: %v", downloadURL, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
file, err := os.OpenFile(targetFile, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
if _, err := io.Copy(file, resp.Body); err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
return nil
}
func main() {
var g sync.WaitGroup
for _, build := range builds {
system, arch := build.System, build.Arch
for _, kind := range kinds {
kind, system, arch := kind, system, arch
g.Add(1)
go func() {
defer g.Done()
err := download(kind, system, arch)
if err != nil {
fmt.Printf("Fail to download %s-%s-%s: %s\n", kind, system, arch, err.Error())
}
}()
}
if err := genGoFile(system, arch); err != nil {
panic(fmt.Errorf("failed to generate go file: %w", err))
}
}
g.Wait()
}