Skip to content

Commit 2a0d4b1

Browse files
authored
Add support to print the version list of target package (#131)
1 parent 271bb41 commit 2a0d4b1

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

cmd/get.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ Thanks to https://github.com/hunshcn/gh-proxy`)
5656
flags.StringVarP(&opt.Arch, "arch", "", runtime.GOARCH, "The arch of target binary file")
5757
flags.BoolVarP(&opt.PrintSchema, "print-schema", "", false,
5858
"Print the schema of HDConfig if the flag is true without other function")
59+
flags.BoolVarP(&opt.PrintVersion, "print-version", "", false,
60+
"Print the version list")
61+
flags.IntVarP(&opt.PrintVersionCount, "print-version-count", "", 20,
62+
"The number of the version list")
5963

6064
_ = cmd.RegisterFlagCompletionFunc("proxy-github", ArrayCompletion("gh.api.99988866.xyz",
6165
"ghproxy.com", "mirror.ghproxy.com"))
@@ -80,9 +84,11 @@ type downloadOption struct {
8084
Arch string
8185
OS string
8286

83-
Thread int
84-
KeepPart bool
85-
PrintSchema bool
87+
Thread int
88+
KeepPart bool
89+
PrintSchema bool
90+
PrintVersion bool
91+
PrintVersionCount int
8692

8793
// inner fields
8894
name string
@@ -162,6 +168,18 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
162168
return
163169
}
164170

171+
if o.PrintVersion {
172+
client := &pkg.ReleaseClient{}
173+
client.Init()
174+
var list []pkg.ReleaseAsset
175+
if list, err = client.ListReleases(o.org, o.repo, o.PrintVersionCount); err == nil {
176+
for _, item := range list {
177+
cmd.Println(item.TagName)
178+
}
179+
}
180+
return
181+
}
182+
165183
cmd.Printf("start to download from %s\n", o.URL)
166184
if o.Thread <= 1 {
167185
err = pkg.DownloadWithContinue(o.URL, o.Output, o.ContinueAt, -1, 0, o.ShowProgress)

pkg/release.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type ReleaseClient struct {
1010
Client *github.Client
1111
Org string
1212
Repo string
13+
14+
ctx context.Context
1315
}
1416

1517
// ReleaseAsset is the asset from GitHub release
@@ -21,6 +23,25 @@ type ReleaseAsset struct {
2123
// Init init the GitHub client
2224
func (g *ReleaseClient) Init() {
2325
g.Client = github.NewClient(nil)
26+
g.ctx = context.TODO()
27+
}
28+
29+
// ListReleases returns the release list
30+
func (g *ReleaseClient) ListReleases(owner, repo string, count int) (list []ReleaseAsset, err error) {
31+
opt := &github.ListOptions{
32+
PerPage: count,
33+
}
34+
35+
var releaseList []*github.RepositoryRelease
36+
if releaseList, _, err = g.Client.Repositories.ListReleases(g.ctx, owner, repo, opt); err == nil {
37+
for i := range releaseList {
38+
list = append(list, ReleaseAsset{
39+
TagName: releaseList[i].GetTagName(),
40+
Body: releaseList[i].GetBody(),
41+
})
42+
}
43+
}
44+
return
2445
}
2546

2647
// GetLatestJCLIAsset returns the latest jcli asset
@@ -75,19 +96,13 @@ func (g *ReleaseClient) GetJCLIAsset(tagName string) (*ReleaseAsset, error) {
7596

7697
// GetReleaseAssetByTagName returns the release asset by tag name
7798
func (g *ReleaseClient) GetReleaseAssetByTagName(owner, repo, tagName string) (ra *ReleaseAsset, err error) {
78-
ctx := context.Background()
79-
80-
opt := &github.ListOptions{
81-
PerPage: 99999,
82-
}
83-
84-
var releaseList []*github.RepositoryRelease
85-
if releaseList, _, err = g.Client.Repositories.ListReleases(ctx, owner, repo, opt); err == nil {
86-
for _, item := range releaseList {
87-
if item.GetTagName() == tagName {
99+
var list []ReleaseAsset
100+
if list, err = g.ListReleases(owner, repo, 99999); err == nil {
101+
for _, item := range list {
102+
if item.TagName == tagName {
88103
ra = &ReleaseAsset{
89-
TagName: item.GetTagName(),
90-
Body: item.GetBody(),
104+
TagName: item.TagName,
105+
Body: item.Body,
91106
}
92107
break
93108
}

0 commit comments

Comments
 (0)