Allow downloading assets from the latest release

This commit is contained in:
Mislav Marohnić 2020-09-03 15:34:10 +02:00
parent 151a7340a6
commit d4b45c68e2
3 changed files with 58 additions and 13 deletions

View file

@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/cmd/release/shared"
@ -35,16 +36,32 @@ func NewCmdDownload(f *cmdutil.Factory, runF func(*DownloadOptions) error) *cobr
}
cmd := &cobra.Command{
Use: "download <tag> [<pattern>]",
Use: "download [<tag>]",
Short: "Download release assets",
Args: cobra.MinimumNArgs(1),
Long: heredoc.Doc(`
Download assets from a GitHub release.
Without an explicit tag name argument, assets are downloaded from the
latest release in the project. In this case, '--pattern' is required.
`),
Example: heredoc.Doc(`
# download all assets from a specific release
$ gh release download v1.2.3
# download only Debian packages for the latest release
$ gh release download --pattern '*.deb'
`),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo
opts.TagName = args[0]
if len(args) > 1 {
opts.FilePattern = args[1]
if len(args) == 0 {
if opts.FilePattern == "" {
return &cmdutil.FlagError{Err: errors.New("the '--pattern' flag is required when downloading the latest release")}
}
} else {
opts.TagName = args[0]
}
opts.Concurrency = 5
@ -56,7 +73,8 @@ func NewCmdDownload(f *cmdutil.Factory, runF func(*DownloadOptions) error) *cobr
},
}
cmd.Flags().StringVarP(&opts.Destination, "dir", "C", ".", "The directory to download files into")
cmd.Flags().StringVarP(&opts.Destination, "dir", "D", ".", "The directory to download files into")
cmd.Flags().StringVarP(&opts.FilePattern, "pattern", "p", "", "Download only assets that match the glob pattern")
return cmd
}
@ -72,9 +90,18 @@ func downloadRun(opts *DownloadOptions) error {
return err
}
release, err := shared.FetchRelease(httpClient, baseRepo, opts.TagName)
if err != nil {
return err
var release *shared.Release
if opts.TagName == "" {
release, err = shared.FetchLatestRelease(httpClient, baseRepo)
if err != nil {
return err
}
} else {
release, err = shared.FetchRelease(httpClient, baseRepo, opts.TagName)
if err != nil {
return err
}
}
var toDownload []shared.ReleaseAsset

View file

@ -38,7 +38,7 @@ func Test_NewCmdDownload(t *testing.T) {
},
{
name: "version and file pattern",
args: "v1.2.3 *.tgz",
args: "v1.2.3 -p *.tgz",
isTTY: true,
want: DownloadOptions{
TagName: "v1.2.3",
@ -49,7 +49,7 @@ func Test_NewCmdDownload(t *testing.T) {
},
{
name: "version and destination",
args: "v1.2.3 -C tmp/assets",
args: "v1.2.3 -D tmp/assets",
isTTY: true,
want: DownloadOptions{
TagName: "v1.2.3",
@ -58,11 +58,22 @@ func Test_NewCmdDownload(t *testing.T) {
Concurrency: 5,
},
},
{
name: "download latest",
args: "-p *",
isTTY: true,
want: DownloadOptions{
TagName: "",
FilePattern: "*",
Destination: ".",
Concurrency: 5,
},
},
{
name: "no arguments",
args: "",
isTTY: true,
wantErr: "requires at least 1 arg(s), only received 0",
wantErr: "the '--pattern' flag is required when downloading the latest release",
},
}
for _, tt := range tests {

View file

@ -7,6 +7,7 @@ import (
"strings"
"time"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/cmd/release/shared"
"github.com/cli/cli/pkg/cmdutil"
@ -32,7 +33,13 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
cmd := &cobra.Command{
Use: "view [<tag>]",
Short: "View information about a release",
Args: cobra.MaximumNArgs(1),
Long: heredoc.Doc(`
View information about a GitHub release.
Without an explicit tag name argument, the latest release in the project
is shown.
`),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo