Skip to content

Commit d4b45c6

Browse files
committed
Allow downloading assets from the latest release
1 parent 151a734 commit d4b45c6

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

pkg/cmd/release/download/download.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99

10+
"github.com/MakeNowJust/heredoc"
1011
"github.com/cli/cli/api"
1112
"github.com/cli/cli/internal/ghrepo"
1213
"github.com/cli/cli/pkg/cmd/release/shared"
@@ -35,16 +36,32 @@ func NewCmdDownload(f *cmdutil.Factory, runF func(*DownloadOptions) error) *cobr
3536
}
3637

3738
cmd := &cobra.Command{
38-
Use: "download <tag> [<pattern>]",
39+
Use: "download [<tag>]",
3940
Short: "Download release assets",
40-
Args: cobra.MinimumNArgs(1),
41+
Long: heredoc.Doc(`
42+
Download assets from a GitHub release.
43+
44+
Without an explicit tag name argument, assets are downloaded from the
45+
latest release in the project. In this case, '--pattern' is required.
46+
`),
47+
Example: heredoc.Doc(`
48+
# download all assets from a specific release
49+
$ gh release download v1.2.3
50+
51+
# download only Debian packages for the latest release
52+
$ gh release download --pattern '*.deb'
53+
`),
54+
Args: cobra.MaximumNArgs(1),
4155
RunE: func(cmd *cobra.Command, args []string) error {
4256
// support `-R, --repo` override
4357
opts.BaseRepo = f.BaseRepo
4458

45-
opts.TagName = args[0]
46-
if len(args) > 1 {
47-
opts.FilePattern = args[1]
59+
if len(args) == 0 {
60+
if opts.FilePattern == "" {
61+
return &cmdutil.FlagError{Err: errors.New("the '--pattern' flag is required when downloading the latest release")}
62+
}
63+
} else {
64+
opts.TagName = args[0]
4865
}
4966

5067
opts.Concurrency = 5
@@ -56,7 +73,8 @@ func NewCmdDownload(f *cmdutil.Factory, runF func(*DownloadOptions) error) *cobr
5673
},
5774
}
5875

59-
cmd.Flags().StringVarP(&opts.Destination, "dir", "C", ".", "The directory to download files into")
76+
cmd.Flags().StringVarP(&opts.Destination, "dir", "D", ".", "The directory to download files into")
77+
cmd.Flags().StringVarP(&opts.FilePattern, "pattern", "p", "", "Download only assets that match the glob pattern")
6078

6179
return cmd
6280
}
@@ -72,9 +90,18 @@ func downloadRun(opts *DownloadOptions) error {
7290
return err
7391
}
7492

75-
release, err := shared.FetchRelease(httpClient, baseRepo, opts.TagName)
76-
if err != nil {
77-
return err
93+
var release *shared.Release
94+
95+
if opts.TagName == "" {
96+
release, err = shared.FetchLatestRelease(httpClient, baseRepo)
97+
if err != nil {
98+
return err
99+
}
100+
} else {
101+
release, err = shared.FetchRelease(httpClient, baseRepo, opts.TagName)
102+
if err != nil {
103+
return err
104+
}
78105
}
79106

80107
var toDownload []shared.ReleaseAsset

pkg/cmd/release/download/download_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func Test_NewCmdDownload(t *testing.T) {
3838
},
3939
{
4040
name: "version and file pattern",
41-
args: "v1.2.3 *.tgz",
41+
args: "v1.2.3 -p *.tgz",
4242
isTTY: true,
4343
want: DownloadOptions{
4444
TagName: "v1.2.3",
@@ -49,7 +49,7 @@ func Test_NewCmdDownload(t *testing.T) {
4949
},
5050
{
5151
name: "version and destination",
52-
args: "v1.2.3 -C tmp/assets",
52+
args: "v1.2.3 -D tmp/assets",
5353
isTTY: true,
5454
want: DownloadOptions{
5555
TagName: "v1.2.3",
@@ -58,11 +58,22 @@ func Test_NewCmdDownload(t *testing.T) {
5858
Concurrency: 5,
5959
},
6060
},
61+
{
62+
name: "download latest",
63+
args: "-p *",
64+
isTTY: true,
65+
want: DownloadOptions{
66+
TagName: "",
67+
FilePattern: "*",
68+
Destination: ".",
69+
Concurrency: 5,
70+
},
71+
},
6172
{
6273
name: "no arguments",
6374
args: "",
6475
isTTY: true,
65-
wantErr: "requires at least 1 arg(s), only received 0",
76+
wantErr: "the '--pattern' flag is required when downloading the latest release",
6677
},
6778
}
6879
for _, tt := range tests {

pkg/cmd/release/view/view.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"time"
99

10+
"github.com/MakeNowJust/heredoc"
1011
"github.com/cli/cli/internal/ghrepo"
1112
"github.com/cli/cli/pkg/cmd/release/shared"
1213
"github.com/cli/cli/pkg/cmdutil"
@@ -32,7 +33,13 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
3233
cmd := &cobra.Command{
3334
Use: "view [<tag>]",
3435
Short: "View information about a release",
35-
Args: cobra.MaximumNArgs(1),
36+
Long: heredoc.Doc(`
37+
View information about a GitHub release.
38+
39+
Without an explicit tag name argument, the latest release in the project
40+
is shown.
41+
`),
42+
Args: cobra.MaximumNArgs(1),
3643
RunE: func(cmd *cobra.Command, args []string) error {
3744
// support `-R, --repo` override
3845
opts.BaseRepo = f.BaseRepo

0 commit comments

Comments
 (0)