Skip to content

Commit 77d9051

Browse files
committed
Simplify looking up binary types in gist
1 parent 973fbb0 commit 77d9051

File tree

5 files changed

+38
-65
lines changed

5 files changed

+38
-65
lines changed

pkg/cmd/gist/create/create.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ func processFiles(stdin io.ReadCloser, filenameOverride string, filenames []stri
163163
var filename string
164164
var content []byte
165165
var err error
166-
var isBinary bool
167166

168167
if f == "-" {
169168
if filenameOverride != "" {
@@ -176,19 +175,22 @@ func processFiles(stdin io.ReadCloser, filenameOverride string, filenames []stri
176175
return fs, fmt.Errorf("failed to read from stdin: %w", err)
177176
}
178177
stdin.Close()
178+
179+
if shared.IsBinaryContents(content) {
180+
return nil, fmt.Errorf("binary file contents not supported")
181+
}
179182
} else {
180-
content, err = ioutil.ReadFile(f)
183+
isBinary, err := shared.IsBinaryFile(f)
181184
if err != nil {
182185
return fs, fmt.Errorf("failed to read file %s: %w", f, err)
183186
}
184-
185-
isBinary, err = shared.IsBinaryContents(content)
186-
if err != nil {
187-
return nil, err
187+
if isBinary {
188+
return nil, fmt.Errorf("failed to upload %s: binary file not supported", f)
188189
}
189190

190-
if isBinary {
191-
return nil, fmt.Errorf("binary file not supported")
191+
content, err = ioutil.ReadFile(f)
192+
if err != nil {
193+
return fs, fmt.Errorf("failed to read file %s: %w", f, err)
192194
}
193195

194196
filename = path.Base(f)

pkg/cmd/gist/edit/edit.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,31 +144,25 @@ func editRun(opts *EditOptions) error {
144144
}
145145
}
146146

147-
if _, ok := gist.Files[filename]; !ok {
147+
gistFile, found := gist.Files[filename]
148+
if !found {
148149
return fmt.Errorf("gist has no file %q", filename)
149150
}
150-
151-
isBinary, err := shared.IsBinaryContents([]byte(gist.Files[filename].Content))
152-
if err != nil {
153-
return err
154-
}
155-
156-
if isBinary {
157-
return fmt.Errorf("Editing binary files not supported")
151+
if shared.IsBinaryContents([]byte(gistFile.Content)) {
152+
return fmt.Errorf("editing binary files not supported")
158153
}
159154

160155
editorCommand, err := cmdutil.DetermineEditor(opts.Config)
161156
if err != nil {
162157
return err
163158
}
164-
text, err := opts.Edit(editorCommand, filename, gist.Files[filename].Content, opts.IO)
159+
text, err := opts.Edit(editorCommand, filename, gistFile.Content, opts.IO)
165160

166161
if err != nil {
167162
return err
168163
}
169164

170-
if text != gist.Files[filename].Content {
171-
gistFile := gist.Files[filename]
165+
if text != gistFile.Content {
172166
gistFile.Content = text // so it appears if they re-edit
173167
filesToUpdate[filename] = text
174168
}

pkg/cmd/gist/shared/shared.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"github.com/gabriel-vasile/mimetype"
87
"net/http"
98
"net/url"
109
"strings"
1110
"time"
1211

12+
"github.com/cli/cli/api"
1313
"github.com/cli/cli/internal/ghinstance"
14+
"github.com/gabriel-vasile/mimetype"
1415
"github.com/shurcooL/githubv4"
1516
"github.com/shurcooL/graphql"
16-
17-
"github.com/cli/cli/api"
1817
)
1918

2019
type GistFile struct {
@@ -156,26 +155,22 @@ func IsBinaryFile(file string) (bool, error) {
156155
}
157156

158157
isBinary := true
159-
160158
for mime := detectedMime; mime != nil; mime = mime.Parent() {
161159
if mime.Is("text/plain") {
162160
isBinary = false
161+
break
163162
}
164163
}
165-
166164
return isBinary, nil
167165
}
168166

169-
func IsBinaryContents(contents []byte) (bool, error) {
170-
detectedMime := mimetype.Detect(contents)
171-
167+
func IsBinaryContents(contents []byte) bool {
172168
isBinary := true
173-
174-
for mime := detectedMime; mime != nil; mime = mime.Parent() {
169+
for mime := mimetype.Detect(contents); mime != nil; mime = mime.Parent() {
175170
if mime.Is("text/plain") {
176171
isBinary = false
172+
break
177173
}
178174
}
179-
180-
return isBinary, nil
175+
return isBinary
181176
}

pkg/cmd/gist/shared/shared_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ func TestIsBinaryContents(t *testing.T) {
6060
want: false,
6161
fileContent: []byte("package main"),
6262
},
63+
{
64+
want: false,
65+
fileContent: []byte(""),
66+
},
67+
{
68+
want: false,
69+
fileContent: []byte(nil),
70+
},
6371
{
6472
want: true,
6573
fileContent: []byte{239, 191, 189, 239, 191, 189, 239, 191, 189, 239,
@@ -74,10 +82,6 @@ func TestIsBinaryContents(t *testing.T) {
7482
}
7583

7684
for _, tt := range tests {
77-
isBinary, err := IsBinaryContents(tt.fileContent)
78-
if err != nil {
79-
t.Fatal(err)
80-
}
81-
assert.Equal(t, tt.want, isBinary)
85+
assert.Equal(t, tt.want, IsBinaryContents(tt.fileContent))
8286
}
8387
}

pkg/cmd/gist/view/view.go

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,12 @@ func viewRun(opts *ViewOptions) error {
118118
defer opts.IO.StopPager()
119119

120120
render := func(gf *shared.GistFile) error {
121-
isBinary, err := shared.IsBinaryContents([]byte(gf.Content))
122-
if err != nil {
123-
return err
124-
}
125-
126-
if isBinary {
127-
gf.Content = "Skipping rendering binary content..."
121+
if shared.IsBinaryContents([]byte(gf.Content)) {
122+
if len(gist.Files) == 1 || opts.Filename != "" {
123+
return fmt.Errorf("error: file is binary")
124+
}
125+
_, err = fmt.Fprintln(opts.IO.Out, cs.Gray("(skipping rendering binary content)"))
126+
return nil
128127
}
129128

130129
if strings.Contains(gf.Type, "markdown") && !opts.Raw {
@@ -152,16 +151,6 @@ func viewRun(opts *ViewOptions) error {
152151
if !ok {
153152
return fmt.Errorf("gist has no such file: %q", opts.Filename)
154153
}
155-
156-
isBinary, err := shared.IsBinaryContents([]byte(gistFile.Content))
157-
if err != nil {
158-
return err
159-
}
160-
161-
if isBinary {
162-
return fmt.Errorf("Error: file contents is binary")
163-
}
164-
165154
return render(gistFile)
166155
}
167156

@@ -175,17 +164,6 @@ func viewRun(opts *ViewOptions) error {
175164
filenames = append(filenames, fn)
176165
}
177166

178-
if len(filenames) == 1 {
179-
isBinary, err := shared.IsBinaryContents([]byte(gist.Files[filenames[0]].Content))
180-
if err != nil {
181-
return err
182-
}
183-
184-
if isBinary {
185-
return fmt.Errorf("Error: file contents is binary")
186-
}
187-
}
188-
189167
sort.Strings(filenames)
190168
if opts.ListFiles {
191169
for _, fn := range filenames {

0 commit comments

Comments
 (0)