Merge pull request #2003 from cristiand391/refactor-gist-create
Refactor gist create to reuse existing structs
This commit is contained in:
commit
e24035681d
4 changed files with 59 additions and 69 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
package create
|
package create
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
@ -14,6 +16,7 @@ import (
|
||||||
"github.com/MakeNowJust/heredoc"
|
"github.com/MakeNowJust/heredoc"
|
||||||
"github.com/cli/cli/api"
|
"github.com/cli/cli/api"
|
||||||
"github.com/cli/cli/internal/ghinstance"
|
"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/cmdutil"
|
||||||
"github.com/cli/cli/pkg/iostreams"
|
"github.com/cli/cli/pkg/iostreams"
|
||||||
"github.com/cli/cli/utils"
|
"github.com/cli/cli/utils"
|
||||||
|
|
@ -121,7 +124,7 @@ func createRun(opts *CreateOptions) error {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
var httpError api.HTTPError
|
var httpError api.HTTPError
|
||||||
if errors.As(err, &httpError) {
|
if errors.As(err, &httpError) {
|
||||||
|
|
@ -139,8 +142,8 @@ func createRun(opts *CreateOptions) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func processFiles(stdin io.ReadCloser, filenameOverride string, filenames []string) (map[string]string, error) {
|
func processFiles(stdin io.ReadCloser, filenameOverride string, filenames []string) (map[string]*shared.GistFile, error) {
|
||||||
fs := map[string]string{}
|
fs := map[string]*shared.GistFile{}
|
||||||
|
|
||||||
if len(filenames) == 0 {
|
if len(filenames) == 0 {
|
||||||
return nil, errors.New("no files passed")
|
return nil, errors.New("no files passed")
|
||||||
|
|
@ -169,13 +172,15 @@ func processFiles(stdin io.ReadCloser, filenameOverride string, filenames []stri
|
||||||
filename = path.Base(f)
|
filename = path.Base(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
fs[filename] = string(content)
|
fs[filename] = &shared.GistFile{
|
||||||
|
Content: string(content),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return fs, nil
|
return fs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func guessGistName(files map[string]string) string {
|
func guessGistName(files map[string]*shared.GistFile) string {
|
||||||
filenames := make([]string, 0, len(files))
|
filenames := make([]string, 0, len(files))
|
||||||
gistName := ""
|
gistName := ""
|
||||||
|
|
||||||
|
|
@ -193,3 +198,29 @@ func guessGistName(files map[string]string) string {
|
||||||
|
|
||||||
return gistName
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/cli/cli/pkg/cmd/gist/shared"
|
||||||
"github.com/cli/cli/pkg/cmdutil"
|
"github.com/cli/cli/pkg/cmdutil"
|
||||||
"github.com/cli/cli/pkg/httpmock"
|
"github.com/cli/cli/pkg/httpmock"
|
||||||
"github.com/cli/cli/pkg/iostreams"
|
"github.com/cli/cli/pkg/iostreams"
|
||||||
|
|
@ -27,21 +28,23 @@ func Test_processFiles(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, 1, len(files))
|
assert.Equal(t, 1, len(files))
|
||||||
assert.Equal(t, "hey cool how is it going", files["gistfile0.txt"])
|
assert.Equal(t, "hey cool how is it going", files["gistfile0.txt"].Content)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_guessGistName_stdin(t *testing.T) {
|
func Test_guessGistName_stdin(t *testing.T) {
|
||||||
files := map[string]string{"gistfile0.txt": "sample content"}
|
files := map[string]*shared.GistFile{
|
||||||
|
"gistfile0.txt": {Content: "sample content"},
|
||||||
|
}
|
||||||
|
|
||||||
gistName := guessGistName(files)
|
gistName := guessGistName(files)
|
||||||
assert.Equal(t, "", gistName)
|
assert.Equal(t, "", gistName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_guessGistName_userFiles(t *testing.T) {
|
func Test_guessGistName_userFiles(t *testing.T) {
|
||||||
files := map[string]string{
|
files := map[string]*shared.GistFile{
|
||||||
"fig.txt": "I am a fig.",
|
"fig.txt": {Content: "I am a fig"},
|
||||||
"apple.txt": "I am an apple.",
|
"apple.txt": {Content: "I am an apple"},
|
||||||
"gistfile0.txt": "sample content",
|
"gistfile0.txt": {Content: "sample content"},
|
||||||
}
|
}
|
||||||
|
|
||||||
gistName := guessGistName(files)
|
gistName := guessGistName(files)
|
||||||
|
|
@ -178,7 +181,9 @@ func Test_createRun(t *testing.T) {
|
||||||
wantStderr: "- Creating gist fixture.txt\n✓ Created gist fixture.txt\n",
|
wantStderr: "- Creating gist fixture.txt\n✓ Created gist fixture.txt\n",
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
wantParams: map[string]interface{}{
|
wantParams: map[string]interface{}{
|
||||||
"public": true,
|
"description": "",
|
||||||
|
"updated_at": "0001-01-01T00:00:00Z",
|
||||||
|
"public": true,
|
||||||
"files": map[string]interface{}{
|
"files": map[string]interface{}{
|
||||||
"fixture.txt": map[string]interface{}{
|
"fixture.txt": map[string]interface{}{
|
||||||
"content": "{}",
|
"content": "{}",
|
||||||
|
|
@ -197,6 +202,8 @@ func Test_createRun(t *testing.T) {
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
wantParams: map[string]interface{}{
|
wantParams: map[string]interface{}{
|
||||||
"description": "an incredibly interesting gist",
|
"description": "an incredibly interesting gist",
|
||||||
|
"updated_at": "0001-01-01T00:00:00Z",
|
||||||
|
"public": false,
|
||||||
"files": map[string]interface{}{
|
"files": map[string]interface{}{
|
||||||
"fixture.txt": map[string]interface{}{
|
"fixture.txt": map[string]interface{}{
|
||||||
"content": "{}",
|
"content": "{}",
|
||||||
|
|
@ -214,6 +221,9 @@ func Test_createRun(t *testing.T) {
|
||||||
wantStderr: "- Creating gist with multiple files\n✓ Created gist fixture.txt\n",
|
wantStderr: "- Creating gist with multiple files\n✓ Created gist fixture.txt\n",
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
wantParams: map[string]interface{}{
|
wantParams: map[string]interface{}{
|
||||||
|
"description": "",
|
||||||
|
"updated_at": "0001-01-01T00:00:00Z",
|
||||||
|
"public": false,
|
||||||
"files": map[string]interface{}{
|
"files": map[string]interface{}{
|
||||||
"fixture.txt": map[string]interface{}{
|
"fixture.txt": map[string]interface{}{
|
||||||
"content": "{}",
|
"content": "{}",
|
||||||
|
|
@ -234,6 +244,9 @@ func Test_createRun(t *testing.T) {
|
||||||
wantStderr: "- Creating gist...\n✓ Created gist\n",
|
wantStderr: "- Creating gist...\n✓ Created gist\n",
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
wantParams: map[string]interface{}{
|
wantParams: map[string]interface{}{
|
||||||
|
"description": "",
|
||||||
|
"updated_at": "0001-01-01T00:00:00Z",
|
||||||
|
"public": false,
|
||||||
"files": map[string]interface{}{
|
"files": map[string]interface{}{
|
||||||
"gistfile0.txt": map[string]interface{}{
|
"gistfile0.txt": map[string]interface{}{
|
||||||
"content": "cool stdin content",
|
"content": "cool stdin content",
|
||||||
|
|
|
||||||
|
|
@ -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"
|
"github.com/cli/cli/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO make gist create use this file
|
|
||||||
|
|
||||||
type GistFile struct {
|
type GistFile struct {
|
||||||
Filename string `json:"filename"`
|
Filename string `json:"filename,omitempty"`
|
||||||
Type string `json:"type,omitempty"`
|
Type string `json:"type,omitempty"`
|
||||||
Language string `json:"language,omitempty"`
|
Language string `json:"language,omitempty"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Gist struct {
|
type Gist struct {
|
||||||
|
|
@ -25,6 +23,7 @@ type Gist struct {
|
||||||
Files map[string]*GistFile `json:"files"`
|
Files map[string]*GistFile `json:"files"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
Public bool `json:"public"`
|
Public bool `json:"public"`
|
||||||
|
HTMLURL string `json:"html_url,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGist(client *http.Client, hostname, gistID string) (*Gist, error) {
|
func GetGist(client *http.Client, hostname, gistID string) (*Gist, error) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue