X Tutup
Skip to content

Commit ef8f61c

Browse files
committed
Refactor gist create
1 parent f17d967 commit ef8f61c

File tree

3 files changed

+39
-62
lines changed

3 files changed

+39
-62
lines changed

pkg/cmd/gist/create/create.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package create
22

33
import (
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+
}

pkg/cmd/gist/create/http.go

Lines changed: 0 additions & 53 deletions
This file was deleted.

pkg/cmd/gist/shared/shared.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ import (
1010
"github.com/cli/cli/api"
1111
)
1212

13-
// TODO make gist create use this file
14-
1513
type GistFile struct {
16-
Filename string `json:"filename"`
14+
Filename string `json:"filename,omitempty"`
1715
Type string `json:"type,omitempty"`
1816
Language string `json:"language,omitempty"`
19-
Content string `json:"content"`
17+
Content string `json:"content,omitempty"`
2018
}
2119

2220
type Gist struct {
@@ -25,6 +23,7 @@ type Gist struct {
2523
Files map[string]*GistFile `json:"files"`
2624
UpdatedAt time.Time `json:"updated_at"`
2725
Public bool `json:"public"`
26+
HTMLURL string `json:"html_url,omitempty"`
2827
}
2928

3029
func GetGist(client *http.Client, hostname, gistID string) (*Gist, error) {

0 commit comments

Comments
 (0)
X Tutup