Support globbing for all platforms
This commit is contained in:
parent
8748bb0b1a
commit
092eb7e962
5 changed files with 35 additions and 35 deletions
|
|
@ -10,7 +10,6 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
|
|
@ -49,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.
|
||||
`, "`"),
|
||||
|
|
@ -69,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 -
|
||||
|
||||
|
|
@ -103,13 +105,9 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
|
|||
}
|
||||
|
||||
func createRun(opts *CreateOptions) error {
|
||||
filenames := opts.Filenames
|
||||
if runtime.GOOS == "windows" {
|
||||
globbedNames, err := cmdutil.GlobWindowsPaths(opts.Filenames)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
filenames = globbedNames
|
||||
filenames, err := cmdutil.GlobPaths(opts.Filenames)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(filenames) == 0 {
|
||||
|
|
|
|||
|
|
@ -77,7 +77,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.
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import (
|
|||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
|
@ -38,11 +37,9 @@ type AssetForUpload struct {
|
|||
}
|
||||
|
||||
func AssetsFromArgs(args []string) (assets []*AssetForUpload, err error) {
|
||||
if runtime.GOOS == "windows" {
|
||||
args, err = cmdutil.GlobWindowsPaths(args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
args, err = cmdutil.GlobPaths(args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, arg := range args {
|
||||
var label string
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package cmdutil
|
|||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
|
|
@ -59,18 +60,21 @@ func NoArgsQuoteReminder(cmd *cobra.Command, args []string) error {
|
|||
return FlagErrorf("%s", errMsg)
|
||||
}
|
||||
|
||||
func GlobWindowsPaths(patterns []string) ([]string, error) {
|
||||
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 {
|
||||
expansions = append(expansions, matches...)
|
||||
} else {
|
||||
if pattern == "-" || strings.Contains(pattern, "#") {
|
||||
expansions = append(expansions, pattern)
|
||||
} else {
|
||||
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...)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package cmdutil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
|
@ -56,7 +57,7 @@ func TestMinimumNs_with_error(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGlobWindowsPaths(t *testing.T) {
|
||||
func TestGlobPaths(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
os string
|
||||
|
|
@ -72,9 +73,9 @@ func TestGlobWindowsPaths(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "When no files match, it returns an empty expansions array, it returns the unmatched patterns",
|
||||
patterns: []string{"foo", "bar"},
|
||||
wantOut: []string{"foo", "bar"},
|
||||
wantErr: nil,
|
||||
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",
|
||||
|
|
@ -92,8 +93,8 @@ func TestGlobWindowsPaths(t *testing.T) {
|
|||
"*/*.txt",
|
||||
},
|
||||
wantOut: []string{
|
||||
"subDir1/subDir1_file.txt",
|
||||
"subDir2/subDir2_file.txt",
|
||||
filepath.Join("subDir1", "subDir1_file.txt"),
|
||||
filepath.Join("subDir2", "subDir2_file.txt"),
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
|
|
@ -104,9 +105,9 @@ func TestGlobWindowsPaths(t *testing.T) {
|
|||
"*/*.go",
|
||||
},
|
||||
wantOut: []string{
|
||||
"subDir1/subDir1_file.txt",
|
||||
"subDir2/subDir2_file.txt",
|
||||
"subDir2/subDir2_file.go",
|
||||
filepath.Join("subDir1", "subDir1_file.txt"),
|
||||
filepath.Join("subDir2", "subDir2_file.txt"),
|
||||
filepath.Join("subDir2", "subDir2_file.go"),
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
|
|
@ -117,7 +118,7 @@ func TestGlobWindowsPaths(t *testing.T) {
|
|||
cleanupFn := createTestDir(t)
|
||||
defer cleanupFn()
|
||||
|
||||
got, err := GlobWindowsPaths(tt.patterns)
|
||||
got, err := GlobPaths(tt.patterns)
|
||||
if tt.wantErr != nil {
|
||||
assert.EqualError(t, err, tt.wantErr.Error())
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue