diff --git a/pkg/cmd/gist/view/view.go b/pkg/cmd/gist/view/view.go index e81bbeba6..95f8223e3 100644 --- a/pkg/cmd/gist/view/view.go +++ b/pkg/cmd/gist/view/view.go @@ -19,10 +19,11 @@ type ViewOptions struct { IO *iostreams.IOStreams HttpClient func() (*http.Client, error) - Selector string - Filename string - Raw bool - Web bool + Selector string + Filename string + Raw bool + Web bool + ListFiles bool } func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command { @@ -51,6 +52,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman cmd.Flags().BoolVarP(&opts.Raw, "raw", "r", false, "Print raw instead of rendered gist contents") cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open gist in the browser") + cmd.Flags().BoolVarP(&opts.ListFiles, "files", "", false, "Display filenames list from the gist") cmd.Flags().StringVarP(&opts.Filename, "filename", "f", "", "Display a single file from the gist") return cmd @@ -126,7 +128,7 @@ func viewRun(opts *ViewOptions) error { cs := opts.IO.ColorScheme() - if gist.Description != "" { + if gist.Description != "" && !opts.ListFiles { fmt.Fprintf(opts.IO.Out, "%s\n\n", cs.Bold(gist.Description)) } @@ -137,6 +139,13 @@ func viewRun(opts *ViewOptions) error { } sort.Strings(filenames) + if opts.ListFiles { + for _, fn := range filenames { + fmt.Fprintln(opts.IO.Out, fn) + } + return nil + } + for i, fn := range filenames { if showFilenames { fmt.Fprintf(opts.IO.Out, "%s\n\n", cs.Gray(fn)) diff --git a/pkg/cmd/gist/view/view_test.go b/pkg/cmd/gist/view/view_test.go index a296a10fb..23571e837 100644 --- a/pkg/cmd/gist/view/view_test.go +++ b/pkg/cmd/gist/view/view_test.go @@ -25,16 +25,18 @@ func TestNewCmdView(t *testing.T) { tty: true, cli: "123", wants: ViewOptions{ - Raw: false, - Selector: "123", + Raw: false, + Selector: "123", + ListFiles: false, }, }, { name: "nontty no arguments", cli: "123", wants: ViewOptions{ - Raw: true, - Selector: "123", + Raw: true, + Selector: "123", + ListFiles: false, }, }, { @@ -42,9 +44,20 @@ func TestNewCmdView(t *testing.T) { cli: "-fcool.txt 123", tty: true, wants: ViewOptions{ - Raw: false, - Selector: "123", - Filename: "cool.txt", + Raw: false, + Selector: "123", + Filename: "cool.txt", + ListFiles: false, + }, + }, + { + name: "files passed", + cli: "--files 123", + tty: true, + wants: ViewOptions{ + Raw: false, + Selector: "123", + ListFiles: true, }, }, } @@ -92,14 +105,16 @@ func Test_viewRun(t *testing.T) { { name: "no such gist", opts: &ViewOptions{ - Selector: "1234", + Selector: "1234", + ListFiles: false, }, wantErr: true, }, { name: "one file", opts: &ViewOptions{ - Selector: "1234", + Selector: "1234", + ListFiles: false, }, gist: &shared.Gist{ Files: map[string]*shared.GistFile{ @@ -114,8 +129,9 @@ func Test_viewRun(t *testing.T) { { name: "filename selected", opts: &ViewOptions{ - Selector: "1234", - Filename: "cicada.txt", + Selector: "1234", + Filename: "cicada.txt", + ListFiles: false, }, gist: &shared.Gist{ Files: map[string]*shared.GistFile{ @@ -134,9 +150,10 @@ func Test_viewRun(t *testing.T) { { name: "filename selected, raw", opts: &ViewOptions{ - Selector: "1234", - Filename: "cicada.txt", - Raw: true, + Selector: "1234", + Filename: "cicada.txt", + Raw: true, + ListFiles: false, }, gist: &shared.Gist{ Files: map[string]*shared.GistFile{ @@ -155,7 +172,8 @@ func Test_viewRun(t *testing.T) { { name: "multiple files, no description", opts: &ViewOptions{ - Selector: "1234", + Selector: "1234", + ListFiles: false, }, gist: &shared.Gist{ Files: map[string]*shared.GistFile{ @@ -174,7 +192,8 @@ func Test_viewRun(t *testing.T) { { name: "multiple files, trailing newlines", opts: &ViewOptions{ - Selector: "1234", + Selector: "1234", + ListFiles: false, }, gist: &shared.Gist{ Files: map[string]*shared.GistFile{ @@ -193,7 +212,8 @@ func Test_viewRun(t *testing.T) { { name: "multiple files, description", opts: &ViewOptions{ - Selector: "1234", + Selector: "1234", + ListFiles: false, }, gist: &shared.Gist{ Description: "some files", @@ -213,8 +233,9 @@ func Test_viewRun(t *testing.T) { { name: "multiple files, raw", opts: &ViewOptions{ - Selector: "1234", - Raw: true, + Selector: "1234", + Raw: true, + ListFiles: false, }, gist: &shared.Gist{ Description: "some files", @@ -231,6 +252,46 @@ func Test_viewRun(t *testing.T) { }, wantOut: "some files\n\ncicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n- foo\n", }, + { + name: "one file, list files", + opts: &ViewOptions{ + Selector: "1234", + Raw: false, + ListFiles: true, + }, + gist: &shared.Gist{ + Description: "some files", + Files: map[string]*shared.GistFile{ + "cicada.txt": { + Content: "bwhiizzzbwhuiiizzzz", + Type: "text/plain", + }, + }, + }, + wantOut: "cicada.txt\n", + }, + { + name: "multiple file, list files", + opts: &ViewOptions{ + Selector: "1234", + Raw: false, + ListFiles: true, + }, + gist: &shared.Gist{ + Description: "some files", + Files: map[string]*shared.GistFile{ + "cicada.txt": { + Content: "bwhiizzzbwhuiiizzzz", + Type: "text/plain", + }, + "foo.md": { + Content: "- foo", + Type: "application/markdown", + }, + }, + }, + wantOut: "cicada.txt\nfoo.md\n", + }, } for _, tt := range tests {