From f30e76aab8a1584bb3e78bfad691897990f76bb0 Mon Sep 17 00:00:00 2001 From: Ben Steadman Date: Tue, 16 Nov 2021 20:05:54 +0000 Subject: [PATCH] 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 --- pkg/cmd/gist/edit/edit.go | 22 +++++++++++------- pkg/cmd/gist/edit/edit_test.go | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/pkg/cmd/gist/edit/edit.go b/pkg/cmd/gist/edit/edit.go index 174b326cd..a43cf9a5b 100644 --- a/pkg/cmd/gist/edit/edit.go +++ b/pkg/cmd/gist/edit/edit.go @@ -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 { diff --git a/pkg/cmd/gist/edit/edit_test.go b/pkg/cmd/gist/edit/edit_test.go index cac99b923..62b6fcb25 100644 --- a/pkg/cmd/gist/edit/edit_test.go +++ b/pkg/cmd/gist/edit/edit_test.go @@ -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 {