Skip to content

Commit f30e76a

Browse files
committed
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>
1 parent 0b8c218 commit f30e76a

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

pkg/cmd/gist/edit/edit.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type EditOptions struct {
3232
Selector string
3333
EditFilename string
3434
AddFilename string
35+
Description string
3536
}
3637

3738
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
6465
}
6566

6667
cmd.Flags().StringVarP(&opts.AddFilename, "add", "a", "", "Add a new file to the gist")
68+
cmd.Flags().StringVarP(&opts.Description, "desc", "d", "", "New description for the gist")
6769
cmd.Flags().StringVarP(&opts.EditFilename, "filename", "f", "", "Select a file to edit")
6870

6971
return cmd
@@ -114,6 +116,12 @@ func editRun(opts *EditOptions) error {
114116
return fmt.Errorf("You do not own this gist.")
115117
}
116118

119+
shouldUpdate := false
120+
if opts.Description != "" {
121+
shouldUpdate = true
122+
gist.Description = opts.Description
123+
}
124+
117125
if opts.AddFilename != "" {
118126
files, err := getFilesToAdd(opts.AddFilename)
119127
if err != nil {
@@ -166,7 +174,6 @@ func editRun(opts *EditOptions) error {
166174
return err
167175
}
168176
text, err := opts.Edit(editorCommand, filename, gistFile.Content, opts.IO)
169-
170177
if err != nil {
171178
return err
172179
}
@@ -215,16 +222,15 @@ func editRun(opts *EditOptions) error {
215222
}
216223
}
217224

218-
if len(filesToUpdate) == 0 {
219-
return nil
225+
if len(filesToUpdate) > 0 {
226+
shouldUpdate = true
220227
}
221228

222-
err = updateGist(apiClient, host, gist)
223-
if err != nil {
224-
return err
229+
if !shouldUpdate {
230+
return nil
225231
}
226232

227-
return nil
233+
return updateGist(apiClient, host, gist)
228234
}
229235

230236
func updateGist(apiClient *api.Client, hostname string, gist *shared.Gist) error {

pkg/cmd/gist/edit/edit_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ func TestNewCmdEdit(t *testing.T) {
6565
AddFilename: "cool.md",
6666
},
6767
},
68+
{
69+
name: "description",
70+
cli: `123 --desc "my new description"`,
71+
wants: EditOptions{
72+
Selector: "123",
73+
Description: "my new description",
74+
},
75+
},
6876
}
6977

7078
for _, tt := range tests {
@@ -262,6 +270,39 @@ func Test_editRun(t *testing.T) {
262270
AddFilename: fileToAdd,
263271
},
264272
},
273+
{
274+
name: "change description",
275+
gist: &shared.Gist{
276+
ID: "1234",
277+
Description: "my old description",
278+
Files: map[string]*shared.GistFile{
279+
"sample.txt": {
280+
Filename: "sample.txt",
281+
Type: "text/plain",
282+
},
283+
},
284+
Owner: &shared.GistOwner{Login: "octocat"},
285+
},
286+
httpStubs: func(reg *httpmock.Registry) {
287+
reg.Register(httpmock.REST("POST", "gists/1234"),
288+
httpmock.StatusStringResponse(201, "{}"))
289+
},
290+
wantParams: map[string]interface{}{
291+
"description": "my new description",
292+
"updated_at": "0001-01-01T00:00:00Z",
293+
"public": false,
294+
"files": map[string]interface{}{
295+
"sample.txt": map[string]interface{}{
296+
"content": "new file content",
297+
"filename": "sample.txt",
298+
"type": "text/plain",
299+
},
300+
},
301+
},
302+
opts: &EditOptions{
303+
Description: "my new description",
304+
},
305+
},
265306
}
266307

267308
for _, tt := range tests {

0 commit comments

Comments
 (0)