From ef8f61c61ddbaa9e602178eb4a5f095ffd72039e Mon Sep 17 00:00:00 2001 From: Cristian Dominguez Date: Mon, 28 Sep 2020 02:36:01 -0300 Subject: [PATCH] Refactor gist create --- pkg/cmd/gist/create/create.go | 41 +++++++++++++++++++++++---- pkg/cmd/gist/create/http.go | 53 ----------------------------------- pkg/cmd/gist/shared/shared.go | 7 ++--- 3 files changed, 39 insertions(+), 62 deletions(-) delete mode 100644 pkg/cmd/gist/create/http.go diff --git a/pkg/cmd/gist/create/create.go b/pkg/cmd/gist/create/create.go index 8024cfc24..85fcb158f 100644 --- a/pkg/cmd/gist/create/create.go +++ b/pkg/cmd/gist/create/create.go @@ -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 +} diff --git a/pkg/cmd/gist/create/http.go b/pkg/cmd/gist/create/http.go deleted file mode 100644 index 42b970221..000000000 --- a/pkg/cmd/gist/create/http.go +++ /dev/null @@ -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 -} diff --git a/pkg/cmd/gist/shared/shared.go b/pkg/cmd/gist/shared/shared.go index f285621ab..d8a753dfe 100644 --- a/pkg/cmd/gist/shared/shared.go +++ b/pkg/cmd/gist/shared/shared.go @@ -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) {