Skip to content

Commit b6502de

Browse files
vilmibmvilmibm
authored andcommitted
allow naming stdin gist files
1 parent f3b4493 commit b6502de

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

pkg/cmd/gist/create/create.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ import (
2323
type CreateOptions struct {
2424
IO *iostreams.IOStreams
2525

26-
Description string
27-
Public bool
28-
Filenames []string
26+
Description string
27+
Public bool
28+
Filenames []string
29+
FilenameOverride string
2930

3031
HttpClient func() (*http.Client, error)
3132
}
@@ -84,6 +85,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
8485

8586
cmd.Flags().StringVarP(&opts.Description, "desc", "d", "", "A description for this gist")
8687
cmd.Flags().BoolVarP(&opts.Public, "public", "p", false, "List the gist publicly (default: private)")
88+
cmd.Flags().StringVarP(&opts.FilenameOverride, "filename", "f", "", "Provide a filename to be used when reading from STDIN")
8789
return cmd
8890
}
8991

@@ -93,7 +95,7 @@ func createRun(opts *CreateOptions) error {
9395
fileArgs = []string{"-"}
9496
}
9597

96-
files, err := processFiles(opts.IO.In, fileArgs)
98+
files, err := processFiles(opts.IO.In, opts.FilenameOverride, fileArgs)
9799
if err != nil {
98100
return fmt.Errorf("failed to collect files for posting: %w", err)
99101
}
@@ -137,7 +139,7 @@ func createRun(opts *CreateOptions) error {
137139
return nil
138140
}
139141

140-
func processFiles(stdin io.ReadCloser, filenames []string) (map[string]string, error) {
142+
func processFiles(stdin io.ReadCloser, filenameOverride string, filenames []string) (map[string]string, error) {
141143
fs := map[string]string{}
142144

143145
if len(filenames) == 0 {
@@ -149,7 +151,11 @@ func processFiles(stdin io.ReadCloser, filenames []string) (map[string]string, e
149151
var content []byte
150152
var err error
151153
if f == "-" {
152-
filename = fmt.Sprintf("gistfile%d.txt", i)
154+
if filenameOverride != "" {
155+
filename = filenameOverride
156+
} else {
157+
filename = fmt.Sprintf("gistfile%d.txt", i)
158+
}
153159
content, err = ioutil.ReadAll(stdin)
154160
if err != nil {
155161
return fs, fmt.Errorf("failed to read from stdin: %w", err)

pkg/cmd/gist/create/create_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121

2222
func Test_processFiles(t *testing.T) {
2323
fakeStdin := strings.NewReader("hey cool how is it going")
24-
files, err := processFiles(ioutil.NopCloser(fakeStdin), []string{"-"})
24+
files, err := processFiles(ioutil.NopCloser(fakeStdin), "", []string{"-"})
2525
if err != nil {
2626
t.Fatalf("unexpected error processing files: %s", err)
2727
}

0 commit comments

Comments
 (0)