Merge pull request #10413 from iamazeem/5099-gh-release-create-upload-expand-glob-patterns-on-windows
[gh release create/upload] Expand glob patterns for all platforms
This commit is contained in:
commit
4a8ecee3b8
6 changed files with 312 additions and 9 deletions
|
|
@ -48,13 +48,13 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
|
|||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "create [<filename>... | -]",
|
||||
Use: "create [<filename>... | <pattern>... | -]",
|
||||
Short: "Create a new gist",
|
||||
Long: heredoc.Docf(`
|
||||
Create a new GitHub gist with given contents.
|
||||
|
||||
Gists can be created from one or multiple files. Alternatively, pass %[1]s-%[1]s as
|
||||
file name to read from standard input.
|
||||
filename to read from standard input.
|
||||
|
||||
By default, gists are secret; use %[1]s--public%[1]s to make publicly listed ones.
|
||||
`, "`"),
|
||||
|
|
@ -68,6 +68,9 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
|
|||
# Create a gist containing several files
|
||||
$ gh gist create hello.py world.py cool.txt
|
||||
|
||||
# Create a gist containing several files using patterns
|
||||
$ gh gist create *.md *.txt artifact.*
|
||||
|
||||
# Read from standard input to create a gist
|
||||
$ gh gist create -
|
||||
|
||||
|
|
@ -102,12 +105,23 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
|
|||
}
|
||||
|
||||
func createRun(opts *CreateOptions) error {
|
||||
fileArgs := opts.Filenames
|
||||
if len(fileArgs) == 0 {
|
||||
fileArgs = []string{"-"}
|
||||
|
||||
readFromStdInArg, filenames := cmdutil.Partition(opts.Filenames, func(f string) bool {
|
||||
return f == "-"
|
||||
})
|
||||
|
||||
filenames, err := cmdutil.GlobPaths(filenames)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
files, err := processFiles(opts.IO.In, opts.FilenameOverride, fileArgs)
|
||||
filenames = append(filenames, readFromStdInArg...)
|
||||
|
||||
if len(filenames) == 0 {
|
||||
filenames = []string{"-"}
|
||||
}
|
||||
|
||||
files, err := processFiles(opts.IO.In, opts.FilenameOverride, filenames)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect files for posting: %w", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -225,7 +225,7 @@ func Test_createRun(t *testing.T) {
|
|||
responseStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "multiple files",
|
||||
name: "when both a file and the stdin '-' are provided, it matches on all the files passed in and stdin",
|
||||
opts: &CreateOptions{
|
||||
Filenames: []string{fixtureFile, "-"},
|
||||
},
|
||||
|
|
@ -248,6 +248,30 @@ func Test_createRun(t *testing.T) {
|
|||
},
|
||||
responseStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "when both a file and the stdin '-' are provided, but '-' is not the last argument, it matches on all the files provided and stdin",
|
||||
opts: &CreateOptions{
|
||||
Filenames: []string{"-", fixtureFile},
|
||||
},
|
||||
stdin: "cool stdin content",
|
||||
wantOut: "https://gist.github.com/aa5a315d61ae9438b18d\n",
|
||||
wantStderr: "- Creating gist with multiple files\n✓ Created secret gist fixture.txt\n",
|
||||
wantErr: false,
|
||||
wantParams: map[string]interface{}{
|
||||
"description": "",
|
||||
"updated_at": "0001-01-01T00:00:00Z",
|
||||
"public": false,
|
||||
"files": map[string]interface{}{
|
||||
"fixture.txt": map[string]interface{}{
|
||||
"content": "{}",
|
||||
},
|
||||
"gistfile1.txt": map[string]interface{}{
|
||||
"content": "cool stdin content",
|
||||
},
|
||||
},
|
||||
},
|
||||
responseStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "file with empty content",
|
||||
opts: &CreateOptions{
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
|
|||
cmd := &cobra.Command{
|
||||
DisableFlagsInUseLine: true,
|
||||
|
||||
Use: "create [<tag>] [<files>...]",
|
||||
Use: "create [<tag>] [<filename>... | <pattern>...]",
|
||||
Short: "Create a new release",
|
||||
Long: heredoc.Docf(`
|
||||
Create a new GitHub Release for a repository.
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ import (
|
|||
|
||||
"github.com/cenkalti/backoff/v4"
|
||||
"github.com/cli/cli/v2/api"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
|
|
@ -36,6 +37,17 @@ type AssetForUpload struct {
|
|||
}
|
||||
|
||||
func AssetsFromArgs(args []string) (assets []*AssetForUpload, err error) {
|
||||
labeledArgs, unlabeledArgs := cmdutil.Partition(args, func(arg string) bool {
|
||||
return strings.Contains(arg, "#")
|
||||
})
|
||||
|
||||
args, err = cmdutil.GlobPaths(unlabeledArgs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
args = append(args, labeledArgs...)
|
||||
|
||||
for _, arg := range args {
|
||||
var label string
|
||||
fn := arg
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package cmdutil
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
|
|
@ -57,3 +58,43 @@ func NoArgsQuoteReminder(cmd *cobra.Command, args []string) error {
|
|||
|
||||
return FlagErrorf("%s", errMsg)
|
||||
}
|
||||
|
||||
// Partition takes a slice of any type T and separates it into two slices
|
||||
// of the same type based on the provided predicate function. Any item
|
||||
// that returns true for the predicate will be included in the first slice
|
||||
// returned, and any item that returns false for the predicate will be
|
||||
// included in the second slice returned.
|
||||
func Partition[T any](slice []T, predicate func(T) bool) ([]T, []T) {
|
||||
var matching, nonMatching []T
|
||||
for _, item := range slice {
|
||||
if predicate(item) {
|
||||
matching = append(matching, item)
|
||||
} else {
|
||||
nonMatching = append(nonMatching, item)
|
||||
}
|
||||
}
|
||||
return matching, nonMatching
|
||||
}
|
||||
|
||||
// GlobPaths expands a list of file patterns into a list of file paths.
|
||||
// If no files match a pattern, that pattern will return an error.
|
||||
// If no pattern is passed, this returns an empty list and no error.
|
||||
//
|
||||
// For information on supported glob patterns, see
|
||||
// https://pkg.go.dev/path/filepath#Match
|
||||
func GlobPaths(patterns []string) ([]string, error) {
|
||||
expansions := []string{}
|
||||
|
||||
for _, pattern := range patterns {
|
||||
matches, err := filepath.Glob(pattern)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %v", pattern, err)
|
||||
}
|
||||
if len(matches) == 0 {
|
||||
return []string{}, fmt.Errorf("no matches found for `%s`", pattern)
|
||||
}
|
||||
expansions = append(expansions, matches...)
|
||||
}
|
||||
|
||||
return expansions, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,14 @@
|
|||
package cmdutil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestMinimumArgs(t *testing.T) {
|
||||
tests := []struct {
|
||||
|
|
@ -48,3 +56,207 @@ func TestMinimumNs_with_error(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartition(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
slice []any
|
||||
predicate func(any) bool
|
||||
wantMatching []any
|
||||
wantNonMatching []any
|
||||
}{
|
||||
{
|
||||
name: "When the slice is empty, it returns two empty slices",
|
||||
slice: []any{},
|
||||
predicate: func(any) bool {
|
||||
return true
|
||||
},
|
||||
wantMatching: []any{},
|
||||
wantNonMatching: []any{},
|
||||
},
|
||||
{
|
||||
name: "when the slice has one element that satisfies the predicate, it returns a slice with that element and an empty slice",
|
||||
slice: []any{
|
||||
"foo",
|
||||
},
|
||||
predicate: func(any) bool {
|
||||
return true
|
||||
},
|
||||
wantMatching: []any{"foo"},
|
||||
wantNonMatching: []any{},
|
||||
},
|
||||
{
|
||||
name: "when the slice has one element that does not satisfy the predicate, it returns an empty slice and a slice with that element",
|
||||
slice: []any{
|
||||
"foo",
|
||||
},
|
||||
predicate: func(any) bool {
|
||||
return false
|
||||
},
|
||||
wantMatching: []any{},
|
||||
wantNonMatching: []any{"foo"},
|
||||
},
|
||||
{
|
||||
name: "when the slice has multiple elements, it returns a slice with the elements that satisfy the predicate and a slice with the elements that do not satisfy the predicate",
|
||||
slice: []any{
|
||||
"foo",
|
||||
"bar",
|
||||
"baz",
|
||||
},
|
||||
predicate: func(s any) bool {
|
||||
return s.(string) != "foo"
|
||||
},
|
||||
wantMatching: []any{"bar", "baz"},
|
||||
wantNonMatching: []any{"foo"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotMatching, gotNonMatching := Partition(tt.slice, tt.predicate)
|
||||
assert.ElementsMatch(t, tt.wantMatching, gotMatching)
|
||||
assert.ElementsMatch(t, tt.wantNonMatching, gotNonMatching)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGlobPaths(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
os string
|
||||
patterns []string
|
||||
wantOut []string
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "When no patterns are passed, return an empty slice",
|
||||
patterns: []string{},
|
||||
wantOut: []string{},
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "When no files match, it returns an empty expansions array with error",
|
||||
patterns: []string{"foo"},
|
||||
wantOut: []string{},
|
||||
wantErr: errors.New("no matches found for `foo`"),
|
||||
},
|
||||
{
|
||||
name: "When a single pattern, '*.txt' is passed with one match, it returns that match",
|
||||
patterns: []string{
|
||||
"*.txt",
|
||||
},
|
||||
wantOut: []string{
|
||||
"rootFile.txt",
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "When a single pattern, '*/*.txt' is passed with multiple matches, it returns those matches",
|
||||
patterns: []string{
|
||||
"*/*.txt",
|
||||
},
|
||||
wantOut: []string{
|
||||
filepath.Join("subDir1", "subDir1_file.txt"),
|
||||
filepath.Join("subDir2", "subDir2_file.txt"),
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "When multiple patterns, '*/*.txt' and '*/*.go', are passed with multiple matches, it returns those matches",
|
||||
patterns: []string{
|
||||
"*/*.txt",
|
||||
"*/*.go",
|
||||
},
|
||||
wantOut: []string{
|
||||
filepath.Join("subDir1", "subDir1_file.txt"),
|
||||
filepath.Join("subDir2", "subDir2_file.txt"),
|
||||
filepath.Join("subDir2", "subDir2_file.go"),
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cleanupFn := createTestDir(t)
|
||||
defer cleanupFn()
|
||||
|
||||
got, err := GlobPaths(tt.patterns)
|
||||
if tt.wantErr != nil {
|
||||
assert.EqualError(t, err, tt.wantErr.Error())
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
assert.Equal(t, tt.wantOut, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Creates a temporary directory with the structure below. Returns
|
||||
// a cleanup function that will remove the directory and all of its
|
||||
// contents. The cleanup function should be wrapped in a defer statement.
|
||||
//
|
||||
// | root
|
||||
// |-- rootFile.txt
|
||||
// |-- subDir1
|
||||
// | |-- subDir1_file.txt
|
||||
// |
|
||||
// |-- subDir2
|
||||
// |-- subDir2_file.go
|
||||
// |-- subDir2_file.txt
|
||||
func createTestDir(t *testing.T) (cleanupFn func()) {
|
||||
t.Helper()
|
||||
// Make Directories
|
||||
rootDir := t.TempDir()
|
||||
|
||||
// Move workspace to temporary directory
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = os.Chdir(rootDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Make subdirectories
|
||||
err = os.Mkdir(filepath.Join(rootDir, "subDir1"), 0755)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = os.Mkdir(filepath.Join(rootDir, "subDir2"), 0755)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Make Files
|
||||
err = os.WriteFile(filepath.Join(rootDir, "rootFile.txt"), []byte(""), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = os.WriteFile(filepath.Join(rootDir, "subDir1", "subDir1_file.txt"), []byte(""), 0o644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = os.WriteFile(filepath.Join(rootDir, "subDir2", "subDir2_file.go"), []byte(""), 0o644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = os.WriteFile(filepath.Join(rootDir, "subDir2", "subDir2_file.txt"), []byte(""), 0o644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cleanupFn = func() {
|
||||
os.RemoveAll(rootDir)
|
||||
err = os.Chdir(cwd)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return cleanupFn
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue