diff --git a/pkg/cmd/gist/create/create.go b/pkg/cmd/gist/create/create.go index dc7d4b257..df12e8702 100644 --- a/pkg/cmd/gist/create/create.go +++ b/pkg/cmd/gist/create/create.go @@ -30,6 +30,8 @@ type CreateOptions struct { Filenames []string FilenameOverride string + WebMode bool + HttpClient func() (*http.Client, error) } @@ -86,6 +88,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co } cmd.Flags().StringVarP(&opts.Description, "desc", "d", "", "A description for this gist") + cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the web browser with created gist") cmd.Flags().BoolVarP(&opts.Public, "public", "p", false, "List the gist publicly (default: private)") cmd.Flags().StringVarP(&opts.FilenameOverride, "filename", "f", "", "Provide a filename to be used when reading from STDIN") return cmd @@ -138,6 +141,12 @@ func createRun(opts *CreateOptions) error { fmt.Fprintf(errOut, "%s %s\n", cs.SuccessIcon(), completionMessage) + if opts.WebMode { + fmt.Fprintf(opts.IO.Out, "Opening %s in your browser.\n", utils.DisplayURL(gist.HTMLURL)) + + return utils.OpenInBrowser(gist.HTMLURL) + } + fmt.Fprintln(opts.IO.Out, gist.HTMLURL) return nil diff --git a/pkg/cmd/gist/create/create_test.go b/pkg/cmd/gist/create/create_test.go index dd1912909..548abd743 100644 --- a/pkg/cmd/gist/create/create_test.go +++ b/pkg/cmd/gist/create/create_test.go @@ -3,6 +3,7 @@ package create import ( "bytes" "encoding/json" + "github.com/cli/cli/test" "io/ioutil" "net/http" "strings" @@ -254,6 +255,26 @@ func Test_createRun(t *testing.T) { }, }, }, + { + name: "web arg", + opts: &CreateOptions{ + WebMode: true, + Filenames: []string{fixtureFile}, + }, + wantOut: "Opening gist.github.com/aa5a315d61ae9438b18d in your browser.\n", + wantStderr: "- Creating gist fixture.txt\n✓ Created gist fixture.txt\n", + wantErr: false, + wantParams: map[string]interface{}{ + "description": "", + "updated_at": "0001-01-01T00:00:00Z", + "public": false, + "files": map[string]interface{}{ + "fixture.txt": map[string]interface{}{ + "content": "{}", + }, + }, + }, + }, } for _, tt := range tests { reg := &httpmock.Registry{} @@ -270,6 +291,13 @@ func Test_createRun(t *testing.T) { io, stdin, stdout, stderr := iostreams.Test() tt.opts.IO = io + cs, cmdTeardown := test.InitCmdStubber() + defer cmdTeardown() + + if tt.opts.WebMode { + cs.Stub("") + } + t.Run(tt.name, func(t *testing.T) { stdin.WriteString(tt.stdin) @@ -285,6 +313,12 @@ func Test_createRun(t *testing.T) { assert.Equal(t, tt.wantOut, stdout.String()) assert.Equal(t, tt.wantStderr, stderr.String()) assert.Equal(t, tt.wantParams, reqBody) + + if tt.opts.WebMode { + browserCall := cs.Calls[0].Args + assert.Equal(t, browserCall[len(browserCall)-1], "https://gist.github.com/aa5a315d61ae9438b18d") + } + reg.Verify(t) }) }