Fix gh gist create for multiple filenames and glob patterns
This commit is contained in:
parent
161ad92b20
commit
d9bfa606cc
3 changed files with 34 additions and 29 deletions
|
|
@ -85,7 +85,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
|
|||
},
|
||||
Aliases: []string{"new"},
|
||||
RunE: func(c *cobra.Command, args []string) error {
|
||||
opts.Filenames = args
|
||||
opts.Filenames = args[:]
|
||||
|
||||
if runF != nil {
|
||||
return runF(&opts)
|
||||
|
|
@ -102,12 +102,16 @@ 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{"-"}
|
||||
filenames, err := cmdutil.GlobWindowsPaths(opts.Filenames)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
files, err := processFiles(opts.IO.In, opts.FilenameOverride, fileArgs)
|
||||
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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,20 +4,18 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/cenkalti/backoff/v4"
|
||||
"github.com/cli/cli/v2/api"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
|
|
@ -39,11 +37,9 @@ type AssetForUpload struct {
|
|||
}
|
||||
|
||||
func AssetsFromArgs(args []string) (assets []*AssetForUpload, err error) {
|
||||
if runtime.GOOS == "windows" {
|
||||
args, err = globAssetPatterns(args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
args, err = cmdutil.GlobWindowsPaths(args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, arg := range args {
|
||||
var label string
|
||||
|
|
@ -72,22 +68,6 @@ func AssetsFromArgs(args []string) (assets []*AssetForUpload, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func globAssetPatterns(patterns []string) ([]string, error) {
|
||||
var assets []string
|
||||
for _, pattern := range patterns {
|
||||
matches, err := filepath.Glob(pattern)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %s", pattern, err)
|
||||
}
|
||||
if len(matches) > 0 {
|
||||
assets = append(assets, matches...)
|
||||
} else {
|
||||
assets = append(assets, pattern)
|
||||
}
|
||||
}
|
||||
return assets, nil
|
||||
}
|
||||
|
||||
func typeForFilename(fn string) string {
|
||||
ext := fileExt(fn)
|
||||
switch ext {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ package cmdutil
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
|
|
@ -57,3 +59,22 @@ func NoArgsQuoteReminder(cmd *cobra.Command, args []string) error {
|
|||
|
||||
return FlagErrorf("%s", errMsg)
|
||||
}
|
||||
|
||||
func GlobWindowsPaths(patterns []string) ([]string, error) {
|
||||
if runtime.GOOS != "windows" {
|
||||
return patterns, nil
|
||||
}
|
||||
var expansions []string
|
||||
for _, pattern := range patterns {
|
||||
matches, err := filepath.Glob(pattern)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %s", pattern, err)
|
||||
}
|
||||
if len(matches) > 0 {
|
||||
expansions = append(expansions, matches...)
|
||||
} else {
|
||||
expansions = append(expansions, pattern)
|
||||
}
|
||||
}
|
||||
return expansions, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue