X Tutup
Skip to content

Commit 0bb44c9

Browse files
cesounvilmibm
andauthored
Support for --web when using gist create (cli#2263)
* Support for --web when using gist create Proposal to close cli#2071 I have not worked with Go prior to this so please smite me down with the wisdom of a million Golang gods if I'm doing something terribly wrong. I also added a test to gist/create for the added web arg. Pretty much referenced the implementation from pr/create. * Fix for Tests / build (windows-latest) I believe this fixes it as it stopped failing on a local vm. Otherwise I will try and tackle it tomorrow. * minor cleanup Co-authored-by: vilmibm <vilmibm@github.com>
1 parent 99574f8 commit 0bb44c9

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

pkg/cmd/gist/create/create.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ type CreateOptions struct {
3030
Filenames []string
3131
FilenameOverride string
3232

33+
WebMode bool
34+
3335
HttpClient func() (*http.Client, error)
3436
}
3537

@@ -86,6 +88,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
8688
}
8789

8890
cmd.Flags().StringVarP(&opts.Description, "desc", "d", "", "A description for this gist")
91+
cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the web browser with created gist")
8992
cmd.Flags().BoolVarP(&opts.Public, "public", "p", false, "List the gist publicly (default: private)")
9093
cmd.Flags().StringVarP(&opts.FilenameOverride, "filename", "f", "", "Provide a filename to be used when reading from STDIN")
9194
return cmd
@@ -138,6 +141,12 @@ func createRun(opts *CreateOptions) error {
138141

139142
fmt.Fprintf(errOut, "%s %s\n", cs.SuccessIcon(), completionMessage)
140143

144+
if opts.WebMode {
145+
fmt.Fprintf(opts.IO.Out, "Opening %s in your browser.\n", utils.DisplayURL(gist.HTMLURL))
146+
147+
return utils.OpenInBrowser(gist.HTMLURL)
148+
}
149+
141150
fmt.Fprintln(opts.IO.Out, gist.HTMLURL)
142151

143152
return nil

pkg/cmd/gist/create/create_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package create
33
import (
44
"bytes"
55
"encoding/json"
6+
"github.com/cli/cli/test"
67
"io/ioutil"
78
"net/http"
89
"strings"
@@ -254,6 +255,26 @@ func Test_createRun(t *testing.T) {
254255
},
255256
},
256257
},
258+
{
259+
name: "web arg",
260+
opts: &CreateOptions{
261+
WebMode: true,
262+
Filenames: []string{fixtureFile},
263+
},
264+
wantOut: "Opening gist.github.com/aa5a315d61ae9438b18d in your browser.\n",
265+
wantStderr: "- Creating gist fixture.txt\n✓ Created gist fixture.txt\n",
266+
wantErr: false,
267+
wantParams: map[string]interface{}{
268+
"description": "",
269+
"updated_at": "0001-01-01T00:00:00Z",
270+
"public": false,
271+
"files": map[string]interface{}{
272+
"fixture.txt": map[string]interface{}{
273+
"content": "{}",
274+
},
275+
},
276+
},
277+
},
257278
}
258279
for _, tt := range tests {
259280
reg := &httpmock.Registry{}
@@ -270,6 +291,13 @@ func Test_createRun(t *testing.T) {
270291
io, stdin, stdout, stderr := iostreams.Test()
271292
tt.opts.IO = io
272293

294+
cs, cmdTeardown := test.InitCmdStubber()
295+
defer cmdTeardown()
296+
297+
if tt.opts.WebMode {
298+
cs.Stub("")
299+
}
300+
273301
t.Run(tt.name, func(t *testing.T) {
274302
stdin.WriteString(tt.stdin)
275303

@@ -285,6 +313,12 @@ func Test_createRun(t *testing.T) {
285313
assert.Equal(t, tt.wantOut, stdout.String())
286314
assert.Equal(t, tt.wantStderr, stderr.String())
287315
assert.Equal(t, tt.wantParams, reqBody)
316+
317+
if tt.opts.WebMode {
318+
browserCall := cs.Calls[0].Args
319+
assert.Equal(t, browserCall[len(browserCall)-1], "https://gist.github.com/aa5a315d61ae9438b18d")
320+
}
321+
288322
reg.Verify(t)
289323
})
290324
}

0 commit comments

Comments
 (0)
X Tutup