-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
172 lines (160 loc) · 3.9 KB
/
main.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
package main
import (
"errors"
"fmt"
"github.com/gek64/gek/gDownloader"
"github.com/gek64/gek/gToolbox"
"github.com/urfave/cli/v2"
"log"
"os"
"redl/internal"
)
func main() {
var github string
var gitlab string
var sourceforge string
var tagName string
var included_parts cli.StringSlice
var excluded_parts cli.StringSlice
var no_download bool
var output string
flags := []cli.Flag{
&cli.StringFlag{
Name: "github",
Aliases: []string{"gh"},
Usage: "set github repo (example: gek64/redl)",
Destination: &github,
},
&cli.StringFlag{
Name: "gitlab",
Aliases: []string{"gl"},
Usage: "set gitlab project id (example: 36189 or fdroid%2ffdroidclient)",
Destination: &gitlab,
},
&cli.StringFlag{
Name: "sourceforge",
Aliases: []string{"sf"},
Usage: "set sourceforge rss url (example: https://sourceforge.net/projects/mpv-player-windows/rss?path=/64bit)",
Destination: &sourceforge,
},
&cli.StringFlag{
Name: "tag",
Aliases: []string{"t"},
Usage: "set tag name",
Destination: &tagName,
},
&cli.StringSliceFlag{
Name: "included_parts",
Aliases: []string{"p"},
Usage: "set release file name included parts",
Destination: &included_parts,
},
&cli.StringSliceFlag{
Name: "excluded_parts",
Aliases: []string{"ep"},
Usage: "set release file name excluded parts",
Destination: &excluded_parts,
},
&cli.BoolFlag{
Name: "no_download",
Aliases: []string{"nd"},
Usage: "output the download link without starting to download the file",
Destination: &no_download,
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "set output file",
Destination: &output,
},
}
// 打印版本函数
cli.VersionPrinter = func(cCtx *cli.Context) {
fmt.Printf("%s", cCtx.App.Version)
}
app := &cli.App{
Usage: "Release Download Tool",
Version: "v2.01",
Flags: flags,
Action: func(ctx *cli.Context) (err error) {
var downloadLink string
// 获取下载地址
if github != "" {
var a *internal.GithubAPI
if tagName != "" {
a, err = internal.GetGithubApiByTagName(github, tagName)
if err != nil {
return err
}
} else {
a, err = internal.GetGithubApiLatest(github)
if err != nil {
return err
}
}
downloadLink, err = a.GetDownloadLink(included_parts.Value(), excluded_parts.Value())
if err != nil {
return err
}
}
if gitlab != "" {
var a *internal.GitlabAPI
if tagName != "" {
a, err = internal.GetGitlabApiByTagName(gitlab, tagName)
if err != nil {
return err
}
} else {
a, err = internal.GetGitlabApiLatest(gitlab)
if err != nil {
return err
}
}
downloadLink, err = a.GetDownloadLink(included_parts.Value(), excluded_parts.Value())
if err != nil {
return err
}
}
if sourceforge != "" {
var a *internal.SourceForgeAPI
if tagName != "" {
return errors.New("sourceforge does not support searching by tag name")
} else {
a, err = internal.GetSourceForgeByRss(sourceforge)
if err != nil {
return err
}
}
downloadLink, err = a.GetDownloadLink(included_parts.Value(), excluded_parts.Value())
if err != nil {
return err
}
}
// 不进行下载文件的情况
if no_download {
fmt.Print(downloadLink)
return nil
}
// 进行下载文件的情况
if downloadLink != "" {
_, err = gToolbox.CheckToolbox([]string{"curl"})
if err != nil {
err := gDownloader.Download(downloadLink, output, "")
if err != nil {
return err
}
} else {
err := gDownloader.DownloadWithCurl(downloadLink, output, "")
if err != nil {
return err
}
}
}
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}