Refactor gist create
This commit is contained in:
parent
f17d9672f5
commit
ef8f61c61d
3 changed files with 39 additions and 62 deletions
|
|
@ -1,6 +1,8 @@
|
|||
package create
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
|
@ -14,6 +16,7 @@ import (
|
|||
"github.com/MakeNowJust/heredoc"
|
||||
"github.com/cli/cli/api"
|
||||
"github.com/cli/cli/internal/ghinstance"
|
||||
"github.com/cli/cli/pkg/cmd/gist/shared"
|
||||
"github.com/cli/cli/pkg/cmdutil"
|
||||
"github.com/cli/cli/pkg/iostreams"
|
||||
"github.com/cli/cli/utils"
|
||||
|
|
@ -121,7 +124,7 @@ func createRun(opts *CreateOptions) error {
|
|||
return err
|
||||
}
|
||||
|
||||
gist, err := apiCreate(httpClient, ghinstance.OverridableDefault(), opts.Description, opts.Public, files)
|
||||
gist, err := createGist(httpClient, ghinstance.OverridableDefault(), opts.Description, opts.Public, files)
|
||||
if err != nil {
|
||||
var httpError api.HTTPError
|
||||
if errors.As(err, &httpError) {
|
||||
|
|
@ -139,8 +142,8 @@ func createRun(opts *CreateOptions) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func processFiles(stdin io.ReadCloser, filenameOverride string, filenames []string) (map[string]string, error) {
|
||||
fs := map[string]string{}
|
||||
func processFiles(stdin io.ReadCloser, filenameOverride string, filenames []string) (map[string]*shared.GistFile, error) {
|
||||
fs := map[string]*shared.GistFile{}
|
||||
|
||||
if len(filenames) == 0 {
|
||||
return nil, errors.New("no files passed")
|
||||
|
|
@ -169,13 +172,15 @@ func processFiles(stdin io.ReadCloser, filenameOverride string, filenames []stri
|
|||
filename = path.Base(f)
|
||||
}
|
||||
|
||||
fs[filename] = string(content)
|
||||
fs[filename] = &shared.GistFile{
|
||||
Content: string(content),
|
||||
}
|
||||
}
|
||||
|
||||
return fs, nil
|
||||
}
|
||||
|
||||
func guessGistName(files map[string]string) string {
|
||||
func guessGistName(files map[string]*shared.GistFile) string {
|
||||
filenames := make([]string, 0, len(files))
|
||||
gistName := ""
|
||||
|
||||
|
|
@ -193,3 +198,29 @@ func guessGistName(files map[string]string) string {
|
|||
|
||||
return gistName
|
||||
}
|
||||
|
||||
func createGist(client *http.Client, hostname, description string, public bool, files map[string]*shared.GistFile) (*shared.Gist, error) {
|
||||
path := "gists"
|
||||
|
||||
body := &shared.Gist{
|
||||
Description: description,
|
||||
Public: public,
|
||||
Files: files,
|
||||
}
|
||||
|
||||
result := shared.Gist{}
|
||||
|
||||
requestByte, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
requestBody := bytes.NewReader(requestByte)
|
||||
|
||||
apliClient := api.NewClientFromHTTP(client)
|
||||
err = apliClient.REST(hostname, "POST", path, requestBody, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,53 +0,0 @@
|
|||
package create
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/cli/cli/api"
|
||||
)
|
||||
|
||||
// Gist represents a GitHub's gist.
|
||||
type Gist struct {
|
||||
Description string `json:"description,omitempty"`
|
||||
Public bool `json:"public,omitempty"`
|
||||
Files map[GistFilename]GistFile `json:"files,omitempty"`
|
||||
HTMLURL string `json:"html_url,omitempty"`
|
||||
}
|
||||
|
||||
type GistFilename string
|
||||
|
||||
type GistFile struct {
|
||||
Content string `json:"content,omitempty"`
|
||||
}
|
||||
|
||||
func apiCreate(httpClient *http.Client, hostname string, description string, public bool, files map[string]string) (*Gist, error) {
|
||||
gistFiles := map[GistFilename]GistFile{}
|
||||
|
||||
for filename, content := range files {
|
||||
gistFiles[GistFilename(filename)] = GistFile{content}
|
||||
}
|
||||
|
||||
path := "gists"
|
||||
body := &Gist{
|
||||
Description: description,
|
||||
Public: public,
|
||||
Files: gistFiles,
|
||||
}
|
||||
result := Gist{}
|
||||
|
||||
requestByte, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
requestBody := bytes.NewReader(requestByte)
|
||||
|
||||
apiClient := api.NewClientFromHTTP(httpClient)
|
||||
err = apiClient.REST(hostname, "POST", path, requestBody, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
|
@ -10,13 +10,11 @@ import (
|
|||
"github.com/cli/cli/api"
|
||||
)
|
||||
|
||||
// TODO make gist create use this file
|
||||
|
||||
type GistFile struct {
|
||||
Filename string `json:"filename"`
|
||||
Filename string `json:"filename,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Language string `json:"language,omitempty"`
|
||||
Content string `json:"content"`
|
||||
Content string `json:"content,omitempty"`
|
||||
}
|
||||
|
||||
type Gist struct {
|
||||
|
|
@ -25,6 +23,7 @@ type Gist struct {
|
|||
Files map[string]*GistFile `json:"files"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Public bool `json:"public"`
|
||||
HTMLURL string `json:"html_url,omitempty"`
|
||||
}
|
||||
|
||||
func GetGist(client *http.Client, hostname, gistID string) (*Gist, error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue