X Tutup
Skip to content

Commit da2a732

Browse files
authored
Merge pull request cli#2997 from g14a/feature/add-files-to-gist
Feature/add files to gist
2 parents a496549 + 8f96e40 commit da2a732

File tree

3 files changed

+114
-19
lines changed

3 files changed

+114
-19
lines changed

pkg/cmd/gist/edit/edit.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"io/ioutil"
89
"net/http"
10+
"path/filepath"
911
"sort"
1012
"strings"
1113

@@ -28,8 +30,9 @@ type EditOptions struct {
2830

2931
Edit func(string, string, string, *iostreams.IOStreams) (string, error)
3032

31-
Selector string
32-
Filename string
33+
Selector string
34+
EditFilename string
35+
AddFilename string
3336
}
3437

3538
func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Command {
@@ -60,7 +63,9 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
6063
return editRun(&opts)
6164
},
6265
}
63-
cmd.Flags().StringVarP(&opts.Filename, "filename", "f", "", "Select a file to edit")
66+
67+
cmd.Flags().StringVarP(&opts.AddFilename, "add", "a", "", "Add a new file to the gist")
68+
cmd.Flags().StringVarP(&opts.EditFilename, "filename", "f", "", "Select a file to edit")
6469

6570
return cmd
6671
}
@@ -85,6 +90,9 @@ func editRun(opts *EditOptions) error {
8590

8691
gist, err := shared.GetGist(client, ghinstance.OverridableDefault(), gistID)
8792
if err != nil {
93+
if errors.Is(err, shared.NotFoundErr) {
94+
return fmt.Errorf("gist not found: %s", gistID)
95+
}
8896
return err
8997
}
9098

@@ -97,10 +105,20 @@ func editRun(opts *EditOptions) error {
97105
return fmt.Errorf("You do not own this gist.")
98106
}
99107

108+
if opts.AddFilename != "" {
109+
files, err := getFilesToAdd(opts.AddFilename)
110+
if err != nil {
111+
return err
112+
}
113+
114+
gist.Files = files
115+
return updateGist(apiClient, ghinstance.OverridableDefault(), gist)
116+
}
117+
100118
filesToUpdate := map[string]string{}
101119

102120
for {
103-
filename := opts.Filename
121+
filename := opts.EditFilename
104122
candidates := []string{}
105123
for filename := range gist.Files {
106124
candidates = append(candidates, filename)
@@ -222,3 +240,22 @@ func updateGist(apiClient *api.Client, hostname string, gist *shared.Gist) error
222240

223241
return nil
224242
}
243+
244+
func getFilesToAdd(file string) (map[string]*shared.GistFile, error) {
245+
content, err := ioutil.ReadFile(file)
246+
if err != nil {
247+
return nil, fmt.Errorf("failed to read file %s: %w", file, err)
248+
}
249+
250+
if len(content) == 0 {
251+
return nil, errors.New("file contents cannot be empty")
252+
}
253+
254+
filename := filepath.Base(file)
255+
return map[string]*shared.GistFile{
256+
filename: {
257+
Filename: filename,
258+
Content: string(content),
259+
},
260+
}, nil
261+
}

pkg/cmd/gist/edit/edit_test.go

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"io/ioutil"
77
"net/http"
8+
"path/filepath"
89
"testing"
910

1011
"github.com/cli/cli/internal/config"
@@ -15,8 +16,26 @@ import (
1516
"github.com/cli/cli/pkg/prompt"
1617
"github.com/google/shlex"
1718
"github.com/stretchr/testify/assert"
19+
"github.com/stretchr/testify/require"
1820
)
1921

22+
func Test_getFilesToAdd(t *testing.T) {
23+
fileToAdd := filepath.Join(t.TempDir(), "gist-test.txt")
24+
err := ioutil.WriteFile(fileToAdd, []byte("hello"), 0600)
25+
require.NoError(t, err)
26+
27+
gf, err := getFilesToAdd(fileToAdd)
28+
require.NoError(t, err)
29+
30+
filename := filepath.Base(fileToAdd)
31+
assert.Equal(t, map[string]*shared.GistFile{
32+
filename: {
33+
Filename: filename,
34+
Content: "hello",
35+
},
36+
}, gf)
37+
}
38+
2039
func TestNewCmdEdit(t *testing.T) {
2140
tests := []struct {
2241
name string
@@ -34,8 +53,16 @@ func TestNewCmdEdit(t *testing.T) {
3453
name: "filename",
3554
cli: "123 --filename cool.md",
3655
wants: EditOptions{
37-
Selector: "123",
38-
Filename: "cool.md",
56+
Selector: "123",
57+
EditFilename: "cool.md",
58+
},
59+
},
60+
{
61+
name: "add",
62+
cli: "123 --add cool.md",
63+
wants: EditOptions{
64+
Selector: "123",
65+
AddFilename: "cool.md",
3966
},
4067
},
4168
}
@@ -60,27 +87,31 @@ func TestNewCmdEdit(t *testing.T) {
6087
_, err = cmd.ExecuteC()
6188
assert.NoError(t, err)
6289

63-
assert.Equal(t, tt.wants.Filename, gotOpts.Filename)
90+
assert.Equal(t, tt.wants.EditFilename, gotOpts.EditFilename)
91+
assert.Equal(t, tt.wants.AddFilename, gotOpts.AddFilename)
6492
assert.Equal(t, tt.wants.Selector, gotOpts.Selector)
6593
})
6694
}
6795
}
6896

6997
func Test_editRun(t *testing.T) {
98+
fileToAdd := filepath.Join(t.TempDir(), "gist-test.txt")
99+
err := ioutil.WriteFile(fileToAdd, []byte("hello"), 0600)
100+
require.NoError(t, err)
101+
70102
tests := []struct {
71103
name string
72104
opts *EditOptions
73105
gist *shared.Gist
74106
httpStubs func(*httpmock.Registry)
75107
askStubs func(*prompt.AskStubber)
76108
nontty bool
77-
wantErr bool
78-
wantStderr string
109+
wantErr string
79110
wantParams map[string]interface{}
80111
}{
81112
{
82113
name: "no such gist",
83-
wantErr: true,
114+
wantErr: "gist not found: 1234",
84115
},
85116
{
86117
name: "one file",
@@ -163,7 +194,7 @@ func Test_editRun(t *testing.T) {
163194
as.StubOne("unix.md")
164195
as.StubOne("Cancel")
165196
},
166-
wantErr: true,
197+
wantErr: "SilentError",
167198
gist: &shared.Gist{
168199
ID: "1234",
169200
Files: map[string]*shared.GistFile{
@@ -208,8 +239,28 @@ func Test_editRun(t *testing.T) {
208239
},
209240
Owner: &shared.GistOwner{Login: "octocat2"},
210241
},
211-
wantErr: true,
212-
wantStderr: "You do not own this gist.",
242+
wantErr: "You do not own this gist.",
243+
},
244+
{
245+
name: "add file to existing gist",
246+
gist: &shared.Gist{
247+
ID: "1234",
248+
Files: map[string]*shared.GistFile{
249+
"sample.txt": {
250+
Filename: "sample.txt",
251+
Content: "bwhiizzzbwhuiiizzzz",
252+
Type: "text/plain",
253+
},
254+
},
255+
Owner: &shared.GistOwner{Login: "octocat"},
256+
},
257+
httpStubs: func(reg *httpmock.Registry) {
258+
reg.Register(httpmock.REST("POST", "gists/1234"),
259+
httpmock.StatusStringResponse(201, "{}"))
260+
},
261+
opts: &EditOptions{
262+
AddFilename: fileToAdd,
263+
},
213264
},
214265
}
215266

@@ -246,7 +297,7 @@ func Test_editRun(t *testing.T) {
246297
tt.opts.HttpClient = func() (*http.Client, error) {
247298
return &http.Client{Transport: reg}, nil
248299
}
249-
io, _, _, _ := iostreams.Test()
300+
io, _, stdout, stderr := iostreams.Test()
250301
io.SetStdoutTTY(!tt.nontty)
251302
io.SetStdinTTY(!tt.nontty)
252303
tt.opts.IO = io
@@ -260,11 +311,8 @@ func Test_editRun(t *testing.T) {
260311
t.Run(tt.name, func(t *testing.T) {
261312
err := editRun(tt.opts)
262313
reg.Verify(t)
263-
if tt.wantErr {
264-
assert.Error(t, err)
265-
if tt.wantStderr != "" {
266-
assert.EqualError(t, err, tt.wantStderr)
267-
}
314+
if tt.wantErr != "" {
315+
assert.EqualError(t, err, tt.wantErr)
268316
return
269317
}
270318
assert.NoError(t, err)
@@ -278,6 +326,9 @@ func Test_editRun(t *testing.T) {
278326
}
279327
assert.Equal(t, tt.wantParams, reqBody)
280328
}
329+
330+
assert.Equal(t, "", stdout.String())
331+
assert.Equal(t, "", stderr.String())
281332
})
282333
}
283334
}

pkg/cmd/gist/shared/shared.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package shared
22

33
import (
4+
"errors"
45
"fmt"
56
"net/http"
67
"net/url"
@@ -31,13 +32,19 @@ type Gist struct {
3132
Owner *GistOwner `json:"owner,omitempty"`
3233
}
3334

35+
var NotFoundErr = errors.New("not found")
36+
3437
func GetGist(client *http.Client, hostname, gistID string) (*Gist, error) {
3538
gist := Gist{}
3639
path := fmt.Sprintf("gists/%s", gistID)
3740

3841
apiClient := api.NewClientFromHTTP(client)
3942
err := apiClient.REST(hostname, "GET", path, nil, &gist)
4043
if err != nil {
44+
var httpErr api.HTTPError
45+
if errors.As(err, &httpErr) && httpErr.StatusCode == 404 {
46+
return nil, NotFoundErr
47+
}
4148
return nil, err
4249
}
4350

0 commit comments

Comments
 (0)
X Tutup