-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
134 lines (108 loc) · 2.73 KB
/
cli.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
package main
import (
"context"
"flag"
"fmt"
"io"
"net/http"
"os"
"path"
"strings"
"github.com/hashicorp/go-version"
"github.com/google/go-github/github"
)
var (
basePath string
)
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func init() {
flag.StringVar(&basePath, "base", "/var/www/pojt-get", "-base /var/www/pojt-get")
}
func main() {
flag.Parse()
baseVersion := "v0.0.0-test.0"
lastVersion, err := version.NewVersion(baseVersion)
if err != nil {
panic(err)
}
client := github.NewClient(nil)
opt := &github.ListOptions{}
releases, _, err := client.Repositories.ListReleases(context.Background(), "Pigeon-Developer", "pigeon-oj-tool", opt)
if err != nil {
panic(err)
}
downloadDir := path.Join(basePath, "download")
for _, release := range releases {
releasePath := path.Join(downloadDir, *release.TagName)
os.MkdirAll(releasePath, os.ModePerm)
fmt.Println("sync release ", *release.TagName)
// isStable := !*release.Prerelease && !*release.Draft
isStable := true
if isStable {
v, err := version.NewVersion(*release.TagName)
if err != nil {
continue
}
if v.GreaterThan(lastVersion) {
lastVersion = v
}
}
for _, asset := range release.Assets {
filePath := path.Join(releasePath, *asset.Name)
if fileExists(filePath) {
fmt.Println("sync file ", filePath, " already exists")
continue
}
if !strings.HasPrefix(*asset.Name, "pojt-") {
continue
}
fmt.Println("sync file ", *asset.Name)
rc, url, err := client.Repositories.DownloadReleaseAsset(context.Background(), "Pigeon-Developer", "pigeon-oj-tool", *asset.ID)
if err != nil {
fmt.Println("sync file ", filePath, " error: ", err)
continue
}
f, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
fmt.Println("sync file ", filePath, " error: ", err)
continue
}
defer f.Close()
if rc != nil {
defer rc.Close()
_, err := io.Copy(f, rc)
if err != nil {
fmt.Println("sync file ", filePath, " error: ", err)
continue
}
} else {
resp, err := http.Get(url)
if err != nil {
fmt.Println("sync file ", filePath, " error: ", err)
continue
}
defer resp.Body.Close()
_, err = io.Copy(f, resp.Body)
if err != nil {
fmt.Println("sync file ", filePath, " error: ", err)
continue
}
}
}
}
if lastVersion.Original() != baseVersion {
versionPath := path.Join(basePath, "latest-version")
versionFile, err := os.OpenFile(versionPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
if err != nil {
panic(err)
}
defer versionFile.Close()
versionFile.WriteString(lastVersion.String())
}
}