Merge pull request #2898 from Ma3oBblu/add-gist-view-files

add --files to list filenames in gist (#2885)
This commit is contained in:
Mislav Marohnić 2021-02-03 22:52:09 +01:00 committed by GitHub
commit 481ec92ebf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 98 additions and 28 deletions

View file

@ -27,7 +27,7 @@ func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
}
cmd := &cobra.Command{
Use: "delete {<gist ID> | <gist URL>}",
Use: "delete {<id> | <url>}",
Short: "Delete a gist",
Args: cmdutil.MinimumArgs(1, "cannot delete: gist argument required"),
RunE: func(c *cobra.Command, args []string) error {

View file

@ -47,7 +47,7 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
}
cmd := &cobra.Command{
Use: "edit {<gist ID> | <gist URL>}",
Use: "edit {<id> | <url>}",
Short: "Edit one of your gists",
Args: cmdutil.MinimumArgs(1, "cannot edit: gist argument required"),
RunE: func(c *cobra.Command, args []string) error {
@ -60,7 +60,7 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
return editRun(&opts)
},
}
cmd.Flags().StringVarP(&opts.Filename, "filename", "f", "", "a specific file to edit")
cmd.Flags().StringVarP(&opts.Filename, "filename", "f", "", "Select a file to edit")
return cmd
}

View file

@ -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 {
@ -32,7 +33,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
}
cmd := &cobra.Command{
Use: "view {<gist id> | <gist url>}",
Use: "view {<id> | <url>}",
Short: "View a gist",
Args: cmdutil.MinimumArgs(1, "cannot view: gist argument required"),
RunE: func(cmd *cobra.Command, args []string) error {
@ -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, "List file names 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))

View file

@ -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 {