diff --git a/pkg/cmd/gist/edit/edit.go b/pkg/cmd/gist/edit/edit.go index 74716f334..ecfa3248d 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 74c4f3502..92d043a05 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 {