@@ -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
2423type 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
3535func 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
0 commit comments