Skip to content

Commit 05e45e3

Browse files
committed
Feature of adding new files to an existing Github gist
1 parent 4e5aa91 commit 05e45e3

File tree

2 files changed

+127
-72
lines changed

2 files changed

+127
-72
lines changed

pkg/cmd/gist/edit/edit.go

Lines changed: 103 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8-
"net/http"
9-
"sort"
10-
"strings"
11-
128
"github.com/AlecAivazis/survey/v2"
139
"github.com/cli/cli/api"
1410
"github.com/cli/cli/internal/config"
@@ -19,6 +15,9 @@ import (
1915
"github.com/cli/cli/pkg/prompt"
2016
"github.com/cli/cli/pkg/surveyext"
2117
"github.com/spf13/cobra"
18+
"net/http"
19+
"sort"
20+
"strings"
2221
)
2322

2423
type EditOptions struct {
@@ -28,8 +27,9 @@ type EditOptions struct {
2827

2928
Edit func(string, string, string, *iostreams.IOStreams) (string, error)
3029

31-
Selector string
32-
Filename string
30+
Selector string
31+
EditFilename string
32+
AddFilename string
3333
}
3434

3535
func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Command {
@@ -48,7 +48,7 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
4848

4949
cmd := &cobra.Command{
5050
Use: "edit {<id> | <url>}",
51-
Short: "Edit one of your gists",
51+
Short: "Edit one of your gists or add new ones to it",
5252
Args: cmdutil.MinimumArgs(1, "cannot edit: gist argument required"),
5353
RunE: func(c *cobra.Command, args []string) error {
5454
opts.Selector = args[0]
@@ -60,7 +60,8 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
6060
return editRun(&opts)
6161
},
6262
}
63-
cmd.Flags().StringVarP(&opts.Filename, "filename", "f", "", "Select a file to edit")
63+
cmd.Flags().StringVarP(&opts.AddFilename, "add", "a", "", "Add a file")
64+
cmd.Flags().StringVarP(&opts.EditFilename, "filename", "f", "", "Select a file to edit")
6465

6566
return cmd
6667
}
@@ -99,89 +100,121 @@ func editRun(opts *EditOptions) error {
99100

100101
filesToUpdate := map[string]string{}
101102

102-
for {
103-
filename := opts.Filename
104-
candidates := []string{}
105-
for filename := range gist.Files {
106-
candidates = append(candidates, filename)
107-
}
108-
109-
sort.Strings(candidates)
110-
111-
if filename == "" {
112-
if len(candidates) == 1 {
113-
filename = candidates[0]
114-
} else {
115-
if !opts.IO.CanPrompt() {
116-
return errors.New("unsure what file to edit; either specify --filename or run interactively")
117-
}
118-
err = prompt.SurveyAskOne(&survey.Select{
119-
Message: "Edit which file?",
120-
Options: candidates,
121-
}, &filename)
122-
123-
if err != nil {
124-
return fmt.Errorf("could not prompt: %w", err)
125-
}
126-
}
127-
}
128-
129-
if _, ok := gist.Files[filename]; !ok {
130-
return fmt.Errorf("gist has no file %q", filename)
131-
}
103+
addFilename := opts.AddFilename
132104

105+
if addFilename != "" {
106+
//Add files to an existing gist
133107
editorCommand, err := cmdutil.DetermineEditor(opts.Config)
134108
if err != nil {
135109
return err
136110
}
137-
text, err := opts.Edit(editorCommand, filename, gist.Files[filename].Content, opts.IO)
138111

112+
text, err := opts.Edit(editorCommand, addFilename, "", opts.IO)
139113
if err != nil {
140114
return err
141115
}
142116

143-
if text != gist.Files[filename].Content {
144-
gistFile := gist.Files[filename]
145-
gistFile.Content = text // so it appears if they re-edit
146-
filesToUpdate[filename] = text
117+
if text == "" {
118+
return fmt.Errorf("Contents can't be empty")
147119
}
148120

149-
if !opts.IO.CanPrompt() {
150-
break
121+
filesToAdd := map[string]*shared.GistFile{
122+
addFilename: {
123+
Filename: addFilename,
124+
Content: text,
125+
},
151126
}
127+
gist.Files = filesToAdd
152128

153-
if len(candidates) == 1 {
154-
break
129+
err = updateGist(apiClient, ghinstance.OverridableDefault(), gist)
130+
if err != nil {
131+
return err
155132
}
133+
} else {
134+
for {
135+
filename := opts.EditFilename
136+
candidates := []string{}
137+
for filename := range gist.Files {
138+
candidates = append(candidates, filename)
139+
}
156140

157-
choice := ""
141+
sort.Strings(candidates)
142+
143+
if filename == "" {
144+
if len(candidates) == 1 {
145+
filename = candidates[0]
146+
} else {
147+
if !opts.IO.CanPrompt() {
148+
return errors.New("unsure what file to edit; either specify --filename or run interactively")
149+
}
150+
err = prompt.SurveyAskOne(&survey.Select{
151+
Message: "Edit which file?",
152+
Options: candidates,
153+
}, &filename)
154+
155+
if err != nil {
156+
return fmt.Errorf("could not prompt: %w", err)
157+
}
158+
}
159+
}
158160

159-
err = prompt.SurveyAskOne(&survey.Select{
160-
Message: "What next?",
161-
Options: []string{
162-
"Edit another file",
163-
"Submit",
164-
"Cancel",
165-
},
166-
}, &choice)
161+
if _, ok := gist.Files[filename]; !ok {
162+
return fmt.Errorf("gist has no file %q", filename)
163+
}
167164

168-
if err != nil {
169-
return fmt.Errorf("could not prompt: %w", err)
170-
}
165+
editorCommand, err := cmdutil.DetermineEditor(opts.Config)
166+
if err != nil {
167+
return err
168+
}
169+
text, err := opts.Edit(editorCommand, filename, gist.Files[filename].Content, opts.IO)
171170

172-
stop := false
171+
if err != nil {
172+
return err
173+
}
173174

174-
switch choice {
175-
case "Edit another file":
176-
continue
177-
case "Submit":
178-
stop = true
179-
case "Cancel":
180-
return cmdutil.SilentError
181-
}
175+
if text != gist.Files[filename].Content {
176+
gistFile := gist.Files[filename]
177+
gistFile.Content = text // so it appears if they re-edit
178+
filesToUpdate[filename] = text
179+
}
180+
181+
if !opts.IO.CanPrompt() {
182+
break
183+
}
182184

183-
if stop {
184-
break
185+
if len(candidates) == 1 {
186+
break
187+
}
188+
189+
choice := ""
190+
191+
err = prompt.SurveyAskOne(&survey.Select{
192+
Message: "What next?",
193+
Options: []string{
194+
"Edit another file",
195+
"Submit",
196+
"Cancel",
197+
},
198+
}, &choice)
199+
200+
if err != nil {
201+
return fmt.Errorf("could not prompt: %w", err)
202+
}
203+
204+
stop := false
205+
206+
switch choice {
207+
case "Edit another file":
208+
continue
209+
case "Submit":
210+
stop = true
211+
case "Cancel":
212+
return cmdutil.SilentError
213+
}
214+
215+
if stop {
216+
break
217+
}
185218
}
186219
}
187220

pkg/cmd/gist/edit/edit_test.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ func TestNewCmdEdit(t *testing.T) {
3535
cli: "123 --filename cool.md",
3636
wants: EditOptions{
3737
Selector: "123",
38-
Filename: "cool.md",
38+
EditFilename: "cool.md",
39+
},
40+
},
41+
{
42+
name: "add",
43+
cli: "123 --add cool.md",
44+
wants: EditOptions{
45+
Selector: "123",
46+
AddFilename: "cool.md",
3947
},
4048
},
4149
}
@@ -60,7 +68,8 @@ func TestNewCmdEdit(t *testing.T) {
6068
_, err = cmd.ExecuteC()
6169
assert.NoError(t, err)
6270

63-
assert.Equal(t, tt.wants.Filename, gotOpts.Filename)
71+
assert.Equal(t, tt.wants.EditFilename, gotOpts.EditFilename)
72+
assert.Equal(t, tt.wants.AddFilename, gotOpts.AddFilename)
6473
assert.Equal(t, tt.wants.Selector, gotOpts.Selector)
6574
})
6675
}
@@ -211,6 +220,19 @@ func Test_editRun(t *testing.T) {
211220
wantErr: true,
212221
wantStderr: "You do not own this gist.",
213222
},
223+
{
224+
name: "add file to existing gist",
225+
gist: &shared.Gist{
226+
ID: "1234",
227+
Files: map[string]*shared.GistFile{
228+
"foo.txt": {
229+
Filename: "foo.txt",
230+
Content: "bwhiizzzbwhuiiizzzz",
231+
Type: "text/plain",
232+
},
233+
},
234+
},
235+
},
214236
}
215237

216238
for _, tt := range tests {

0 commit comments

Comments
 (0)