|
| 1 | +package edit |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "sort" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/AlecAivazis/survey/v2" |
| 14 | + "github.com/cli/cli/api" |
| 15 | + "github.com/cli/cli/internal/config" |
| 16 | + "github.com/cli/cli/internal/ghinstance" |
| 17 | + "github.com/cli/cli/pkg/cmd/gist/shared" |
| 18 | + "github.com/cli/cli/pkg/cmdutil" |
| 19 | + "github.com/cli/cli/pkg/iostreams" |
| 20 | + "github.com/cli/cli/pkg/prompt" |
| 21 | + "github.com/cli/cli/pkg/surveyext" |
| 22 | + "github.com/spf13/cobra" |
| 23 | +) |
| 24 | + |
| 25 | +type EditOptions struct { |
| 26 | + IO *iostreams.IOStreams |
| 27 | + HttpClient func() (*http.Client, error) |
| 28 | + Config func() (config.Config, error) |
| 29 | + |
| 30 | + Edit func(string, string, string, *iostreams.IOStreams) (string, error) |
| 31 | + |
| 32 | + Selector string |
| 33 | + Filename string |
| 34 | +} |
| 35 | + |
| 36 | +func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Command { |
| 37 | + opts := EditOptions{ |
| 38 | + IO: f.IOStreams, |
| 39 | + HttpClient: f.HttpClient, |
| 40 | + Config: f.Config, |
| 41 | + Edit: func(editorCmd, filename, defaultContent string, io *iostreams.IOStreams) (string, error) { |
| 42 | + return surveyext.Edit( |
| 43 | + editorCmd, |
| 44 | + "*."+filename, |
| 45 | + defaultContent, |
| 46 | + io.In, io.Out, io.ErrOut, nil) |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + cmd := &cobra.Command{ |
| 51 | + Use: "edit {<gist ID> | <gist URL>}", |
| 52 | + Short: "Edit one of your gists", |
| 53 | + Args: cobra.ExactArgs(1), |
| 54 | + RunE: func(c *cobra.Command, args []string) error { |
| 55 | + opts.Selector = args[0] |
| 56 | + |
| 57 | + if runF != nil { |
| 58 | + return runF(&opts) |
| 59 | + } |
| 60 | + |
| 61 | + return editRun(&opts) |
| 62 | + }, |
| 63 | + } |
| 64 | + cmd.Flags().StringVarP(&opts.Filename, "filename", "f", "", "a specific file to edit") |
| 65 | + |
| 66 | + return cmd |
| 67 | +} |
| 68 | + |
| 69 | +func editRun(opts *EditOptions) error { |
| 70 | + gistID := opts.Selector |
| 71 | + |
| 72 | + u, err := url.Parse(opts.Selector) |
| 73 | + if err == nil { |
| 74 | + if strings.HasPrefix(u.Path, "/") { |
| 75 | + gistID = u.Path[1:] |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + client, err := opts.HttpClient() |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + gist, err := shared.GetGist(client, ghinstance.OverridableDefault(), gistID) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + filesToUpdate := map[string]string{} |
| 90 | + |
| 91 | + for { |
| 92 | + filename := opts.Filename |
| 93 | + candidates := []string{} |
| 94 | + for filename := range gist.Files { |
| 95 | + candidates = append(candidates, filename) |
| 96 | + } |
| 97 | + |
| 98 | + sort.Strings(candidates) |
| 99 | + |
| 100 | + if filename == "" { |
| 101 | + if len(candidates) == 1 { |
| 102 | + filename = candidates[0] |
| 103 | + } else { |
| 104 | + if !opts.IO.CanPrompt() { |
| 105 | + return errors.New("unsure what file to edit; either specify --filename or run interactively") |
| 106 | + } |
| 107 | + err = prompt.SurveyAskOne(&survey.Select{ |
| 108 | + Message: "Edit which file?", |
| 109 | + Options: candidates, |
| 110 | + }, &filename) |
| 111 | + |
| 112 | + if err != nil { |
| 113 | + return fmt.Errorf("could not prompt: %w", err) |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if _, ok := gist.Files[filename]; !ok { |
| 119 | + return fmt.Errorf("gist has no file %q", filename) |
| 120 | + } |
| 121 | + |
| 122 | + editorCommand, err := cmdutil.DetermineEditor(opts.Config) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + text, err := opts.Edit(editorCommand, filename, gist.Files[filename].Content, opts.IO) |
| 127 | + |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + if text != gist.Files[filename].Content { |
| 133 | + gistFile := gist.Files[filename] |
| 134 | + gistFile.Content = text // so it appears if they re-edit |
| 135 | + filesToUpdate[filename] = text |
| 136 | + } |
| 137 | + |
| 138 | + if !opts.IO.CanPrompt() { |
| 139 | + break |
| 140 | + } |
| 141 | + |
| 142 | + if len(candidates) == 1 { |
| 143 | + break |
| 144 | + } |
| 145 | + |
| 146 | + choice := "" |
| 147 | + |
| 148 | + err = prompt.SurveyAskOne(&survey.Select{ |
| 149 | + Message: "What next?", |
| 150 | + Options: []string{ |
| 151 | + "Edit another file", |
| 152 | + "Submit", |
| 153 | + "Cancel", |
| 154 | + }, |
| 155 | + }, &choice) |
| 156 | + |
| 157 | + if err != nil { |
| 158 | + return fmt.Errorf("could not prompt: %w", err) |
| 159 | + } |
| 160 | + |
| 161 | + stop := false |
| 162 | + |
| 163 | + switch choice { |
| 164 | + case "Edit another file": |
| 165 | + continue |
| 166 | + case "Submit": |
| 167 | + stop = true |
| 168 | + case "Cancel": |
| 169 | + return cmdutil.SilentError |
| 170 | + } |
| 171 | + |
| 172 | + if stop { |
| 173 | + break |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + err = updateGist(client, ghinstance.OverridableDefault(), gist) |
| 178 | + if err != nil { |
| 179 | + return err |
| 180 | + } |
| 181 | + |
| 182 | + return nil |
| 183 | +} |
| 184 | + |
| 185 | +func updateGist(client *http.Client, hostname string, gist *shared.Gist) error { |
| 186 | + body := shared.Gist{ |
| 187 | + Description: gist.Description, |
| 188 | + Files: gist.Files, |
| 189 | + } |
| 190 | + |
| 191 | + path := "gists/" + gist.ID |
| 192 | + |
| 193 | + requestByte, err := json.Marshal(body) |
| 194 | + if err != nil { |
| 195 | + return err |
| 196 | + } |
| 197 | + |
| 198 | + requestBody := bytes.NewReader(requestByte) |
| 199 | + |
| 200 | + result := shared.Gist{} |
| 201 | + |
| 202 | + apiClient := api.NewClientFromHTTP(client) |
| 203 | + err = apiClient.REST(hostname, "POST", path, requestBody, &result) |
| 204 | + |
| 205 | + if err != nil { |
| 206 | + return err |
| 207 | + } |
| 208 | + |
| 209 | + return nil |
| 210 | +} |
0 commit comments