Support for --web when using gist create (#2263)
* Support for --web when using gist create Proposal to close #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>
This commit is contained in:
parent
99574f85a3
commit
0bb44c9ced
2 changed files with 43 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue