Skip to content

Commit 907524f

Browse files
Ma3oBblumislav
authored andcommitted
add --files to list filenames in gist (cli#2885)
1 parent 962791b commit 907524f

File tree

2 files changed

+94
-24
lines changed

2 files changed

+94
-24
lines changed

pkg/cmd/gist/view/view.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ type ViewOptions struct {
1919
IO *iostreams.IOStreams
2020
HttpClient func() (*http.Client, error)
2121

22-
Selector string
23-
Filename string
24-
Raw bool
25-
Web bool
22+
Selector string
23+
Filename string
24+
Raw bool
25+
Web bool
26+
ListFiles bool
2627
}
2728

2829
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
5152

5253
cmd.Flags().BoolVarP(&opts.Raw, "raw", "r", false, "Print raw instead of rendered gist contents")
5354
cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open gist in the browser")
55+
cmd.Flags().BoolVarP(&opts.ListFiles, "files", "", false, "Display filenames list from the gist")
5456
cmd.Flags().StringVarP(&opts.Filename, "filename", "f", "", "Display a single file from the gist")
5557

5658
return cmd
@@ -126,7 +128,7 @@ func viewRun(opts *ViewOptions) error {
126128

127129
cs := opts.IO.ColorScheme()
128130

129-
if gist.Description != "" {
131+
if gist.Description != "" && !opts.ListFiles {
130132
fmt.Fprintf(opts.IO.Out, "%s\n\n", cs.Bold(gist.Description))
131133
}
132134

@@ -137,6 +139,13 @@ func viewRun(opts *ViewOptions) error {
137139
}
138140
sort.Strings(filenames)
139141

142+
if opts.ListFiles {
143+
for _, fn := range filenames {
144+
fmt.Fprintln(opts.IO.Out, fn)
145+
}
146+
return nil
147+
}
148+
140149
for i, fn := range filenames {
141150
if showFilenames {
142151
fmt.Fprintf(opts.IO.Out, "%s\n\n", cs.Gray(fn))

pkg/cmd/gist/view/view_test.go

Lines changed: 80 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,39 @@ func TestNewCmdView(t *testing.T) {
2525
tty: true,
2626
cli: "123",
2727
wants: ViewOptions{
28-
Raw: false,
29-
Selector: "123",
28+
Raw: false,
29+
Selector: "123",
30+
ListFiles: false,
3031
},
3132
},
3233
{
3334
name: "nontty no arguments",
3435
cli: "123",
3536
wants: ViewOptions{
36-
Raw: true,
37-
Selector: "123",
37+
Raw: true,
38+
Selector: "123",
39+
ListFiles: false,
3840
},
3941
},
4042
{
4143
name: "filename passed",
4244
cli: "-fcool.txt 123",
4345
tty: true,
4446
wants: ViewOptions{
45-
Raw: false,
46-
Selector: "123",
47-
Filename: "cool.txt",
47+
Raw: false,
48+
Selector: "123",
49+
Filename: "cool.txt",
50+
ListFiles: false,
51+
},
52+
},
53+
{
54+
name: "files passed",
55+
cli: "--files 123",
56+
tty: true,
57+
wants: ViewOptions{
58+
Raw: false,
59+
Selector: "123",
60+
ListFiles: true,
4861
},
4962
},
5063
}
@@ -92,14 +105,16 @@ func Test_viewRun(t *testing.T) {
92105
{
93106
name: "no such gist",
94107
opts: &ViewOptions{
95-
Selector: "1234",
108+
Selector: "1234",
109+
ListFiles: false,
96110
},
97111
wantErr: true,
98112
},
99113
{
100114
name: "one file",
101115
opts: &ViewOptions{
102-
Selector: "1234",
116+
Selector: "1234",
117+
ListFiles: false,
103118
},
104119
gist: &shared.Gist{
105120
Files: map[string]*shared.GistFile{
@@ -114,8 +129,9 @@ func Test_viewRun(t *testing.T) {
114129
{
115130
name: "filename selected",
116131
opts: &ViewOptions{
117-
Selector: "1234",
118-
Filename: "cicada.txt",
132+
Selector: "1234",
133+
Filename: "cicada.txt",
134+
ListFiles: false,
119135
},
120136
gist: &shared.Gist{
121137
Files: map[string]*shared.GistFile{
@@ -134,9 +150,10 @@ func Test_viewRun(t *testing.T) {
134150
{
135151
name: "filename selected, raw",
136152
opts: &ViewOptions{
137-
Selector: "1234",
138-
Filename: "cicada.txt",
139-
Raw: true,
153+
Selector: "1234",
154+
Filename: "cicada.txt",
155+
Raw: true,
156+
ListFiles: false,
140157
},
141158
gist: &shared.Gist{
142159
Files: map[string]*shared.GistFile{
@@ -155,7 +172,8 @@ func Test_viewRun(t *testing.T) {
155172
{
156173
name: "multiple files, no description",
157174
opts: &ViewOptions{
158-
Selector: "1234",
175+
Selector: "1234",
176+
ListFiles: false,
159177
},
160178
gist: &shared.Gist{
161179
Files: map[string]*shared.GistFile{
@@ -174,7 +192,8 @@ func Test_viewRun(t *testing.T) {
174192
{
175193
name: "multiple files, trailing newlines",
176194
opts: &ViewOptions{
177-
Selector: "1234",
195+
Selector: "1234",
196+
ListFiles: false,
178197
},
179198
gist: &shared.Gist{
180199
Files: map[string]*shared.GistFile{
@@ -193,7 +212,8 @@ func Test_viewRun(t *testing.T) {
193212
{
194213
name: "multiple files, description",
195214
opts: &ViewOptions{
196-
Selector: "1234",
215+
Selector: "1234",
216+
ListFiles: false,
197217
},
198218
gist: &shared.Gist{
199219
Description: "some files",
@@ -213,8 +233,9 @@ func Test_viewRun(t *testing.T) {
213233
{
214234
name: "multiple files, raw",
215235
opts: &ViewOptions{
216-
Selector: "1234",
217-
Raw: true,
236+
Selector: "1234",
237+
Raw: true,
238+
ListFiles: false,
218239
},
219240
gist: &shared.Gist{
220241
Description: "some files",
@@ -231,6 +252,46 @@ func Test_viewRun(t *testing.T) {
231252
},
232253
wantOut: "some files\n\ncicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n- foo\n",
233254
},
255+
{
256+
name: "one file, list files",
257+
opts: &ViewOptions{
258+
Selector: "1234",
259+
Raw: false,
260+
ListFiles: true,
261+
},
262+
gist: &shared.Gist{
263+
Description: "some files",
264+
Files: map[string]*shared.GistFile{
265+
"cicada.txt": {
266+
Content: "bwhiizzzbwhuiiizzzz",
267+
Type: "text/plain",
268+
},
269+
},
270+
},
271+
wantOut: "cicada.txt\n",
272+
},
273+
{
274+
name: "multiple file, list files",
275+
opts: &ViewOptions{
276+
Selector: "1234",
277+
Raw: false,
278+
ListFiles: true,
279+
},
280+
gist: &shared.Gist{
281+
Description: "some files",
282+
Files: map[string]*shared.GistFile{
283+
"cicada.txt": {
284+
Content: "bwhiizzzbwhuiiizzzz",
285+
Type: "text/plain",
286+
},
287+
"foo.md": {
288+
Content: "- foo",
289+
Type: "application/markdown",
290+
},
291+
},
292+
},
293+
wantOut: "cicada.txt\nfoo.md\n",
294+
},
234295
}
235296

236297
for _, tt := range tests {

0 commit comments

Comments
 (0)