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
0 commit comments