Skip to content
26 changes: 20 additions & 6 deletions pkg/cmd/gist/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
`, "`"),
Expand All @@ -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 -

Expand Down Expand Up @@ -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
}

filenames = append(filenames, readFromStdInArg...)

if len(filenames) == 0 {
filenames = []string{"-"}
}

files, err := processFiles(opts.IO.In, opts.FilenameOverride, fileArgs)
files, err := processFiles(opts.IO.In, opts.FilenameOverride, filenames)
if err != nil {
return fmt.Errorf("failed to collect files for posting: %w", err)
}
Expand Down
26 changes: 25 additions & 1 deletion pkg/cmd/gist/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "-"},
},
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/release/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions pkg/cmd/release/shared/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down
41 changes: 41 additions & 0 deletions pkg/cmdutil/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmdutil

import (
"fmt"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -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
}
Loading