11package create
22
33import (
4+ "bytes"
5+ "encoding/json"
46 "errors"
57 "fmt"
68 "io"
@@ -14,6 +16,7 @@ import (
1416 "github.com/MakeNowJust/heredoc"
1517 "github.com/cli/cli/api"
1618 "github.com/cli/cli/internal/ghinstance"
19+ "github.com/cli/cli/pkg/cmd/gist/shared"
1720 "github.com/cli/cli/pkg/cmdutil"
1821 "github.com/cli/cli/pkg/iostreams"
1922 "github.com/cli/cli/utils"
@@ -121,7 +124,7 @@ func createRun(opts *CreateOptions) error {
121124 return err
122125 }
123126
124- gist , err := apiCreate (httpClient , ghinstance .OverridableDefault (), opts .Description , opts .Public , files )
127+ gist , err := createGist (httpClient , ghinstance .OverridableDefault (), opts .Description , opts .Public , files )
125128 if err != nil {
126129 var httpError api.HTTPError
127130 if errors .As (err , & httpError ) {
@@ -139,8 +142,8 @@ func createRun(opts *CreateOptions) error {
139142 return nil
140143}
141144
142- func processFiles (stdin io.ReadCloser , filenameOverride string , filenames []string ) (map [string ]string , error ) {
143- fs := map [string ]string {}
145+ func processFiles (stdin io.ReadCloser , filenameOverride string , filenames []string ) (map [string ]* shared. GistFile , error ) {
146+ fs := map [string ]* shared. GistFile {}
144147
145148 if len (filenames ) == 0 {
146149 return nil , errors .New ("no files passed" )
@@ -169,13 +172,15 @@ func processFiles(stdin io.ReadCloser, filenameOverride string, filenames []stri
169172 filename = path .Base (f )
170173 }
171174
172- fs [filename ] = string (content )
175+ fs [filename ] = & shared.GistFile {
176+ Content : string (content ),
177+ }
173178 }
174179
175180 return fs , nil
176181}
177182
178- func guessGistName (files map [string ]string ) string {
183+ func guessGistName (files map [string ]* shared. GistFile ) string {
179184 filenames := make ([]string , 0 , len (files ))
180185 gistName := ""
181186
@@ -193,3 +198,29 @@ func guessGistName(files map[string]string) string {
193198
194199 return gistName
195200}
201+
202+ func createGist (client * http.Client , hostname , description string , public bool , files map [string ]* shared.GistFile ) (* shared.Gist , error ) {
203+ path := "gists"
204+
205+ body := & shared.Gist {
206+ Description : description ,
207+ Public : public ,
208+ Files : files ,
209+ }
210+
211+ result := shared.Gist {}
212+
213+ requestByte , err := json .Marshal (body )
214+ if err != nil {
215+ return nil , err
216+ }
217+ requestBody := bytes .NewReader (requestByte )
218+
219+ apliClient := api .NewClientFromHTTP (client )
220+ err = apliClient .REST (hostname , "POST" , path , requestBody , & result )
221+ if err != nil {
222+ return nil , err
223+ }
224+
225+ return & result , nil
226+ }
0 commit comments