Merge pull request #4738 from SteadBytes/gist-edit-description

Support editing gist description
This commit is contained in:
Nate Smith 2022-01-18 09:34:22 -06:00 committed by GitHub
commit f950637b0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 8 deletions

View file

@ -32,6 +32,7 @@ type EditOptions struct {
Selector string
EditFilename string
AddFilename string
Description string
}
func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Command {
@ -64,6 +65,7 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
}
cmd.Flags().StringVarP(&opts.AddFilename, "add", "a", "", "Add a new file to the gist")
cmd.Flags().StringVarP(&opts.Description, "desc", "d", "", "New description for the gist")
cmd.Flags().StringVarP(&opts.EditFilename, "filename", "f", "", "Select a file to edit")
return cmd
@ -114,6 +116,12 @@ func editRun(opts *EditOptions) error {
return fmt.Errorf("You do not own this gist.")
}
shouldUpdate := false
if opts.Description != "" {
shouldUpdate = true
gist.Description = opts.Description
}
if opts.AddFilename != "" {
files, err := getFilesToAdd(opts.AddFilename)
if err != nil {
@ -166,7 +174,6 @@ func editRun(opts *EditOptions) error {
return err
}
text, err := opts.Edit(editorCommand, filename, gistFile.Content, opts.IO)
if err != nil {
return err
}
@ -215,16 +222,15 @@ func editRun(opts *EditOptions) error {
}
}
if len(filesToUpdate) == 0 {
if len(filesToUpdate) > 0 {
shouldUpdate = true
}
if !shouldUpdate {
return nil
}
err = updateGist(apiClient, host, gist)
if err != nil {
return err
}
return nil
return updateGist(apiClient, host, gist)
}
func updateGist(apiClient *api.Client, hostname string, gist *shared.Gist) error {

View file

@ -65,6 +65,14 @@ func TestNewCmdEdit(t *testing.T) {
AddFilename: "cool.md",
},
},
{
name: "description",
cli: `123 --desc "my new description"`,
wants: EditOptions{
Selector: "123",
Description: "my new description",
},
},
}
for _, tt := range tests {
@ -262,6 +270,39 @@ func Test_editRun(t *testing.T) {
AddFilename: fileToAdd,
},
},
{
name: "change description",
gist: &shared.Gist{
ID: "1234",
Description: "my old description",
Files: map[string]*shared.GistFile{
"sample.txt": {
Filename: "sample.txt",
Type: "text/plain",
},
},
Owner: &shared.GistOwner{Login: "octocat"},
},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(httpmock.REST("POST", "gists/1234"),
httpmock.StatusStringResponse(201, "{}"))
},
wantParams: map[string]interface{}{
"description": "my new description",
"updated_at": "0001-01-01T00:00:00Z",
"public": false,
"files": map[string]interface{}{
"sample.txt": map[string]interface{}{
"content": "new file content",
"filename": "sample.txt",
"type": "text/plain",
},
},
},
opts: &EditOptions{
Description: "my new description",
},
},
}
for _, tt := range tests {