Support editing gist description

Add a --desc flag to gh gist edit to support editing gist
descriptions. This flag can be used in combination with the other gist
editing flags to edit the description whilst also adding/editing files.

Signed-off-by: Ben Steadman <steadmanben1@gmail.com>
This commit is contained in:
Ben Steadman 2021-11-16 20:05:54 +00:00
parent 0b8c218632
commit f30e76aab8
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 {