Simplify looking up binary types in gist

This commit is contained in:
Mislav Marohnić 2021-03-02 14:40:43 +01:00
parent 973fbb0925
commit 77d9051d0e
5 changed files with 38 additions and 65 deletions

View file

@ -163,7 +163,6 @@ func processFiles(stdin io.ReadCloser, filenameOverride string, filenames []stri
var filename string
var content []byte
var err error
var isBinary bool
if f == "-" {
if filenameOverride != "" {
@ -176,19 +175,22 @@ func processFiles(stdin io.ReadCloser, filenameOverride string, filenames []stri
return fs, fmt.Errorf("failed to read from stdin: %w", err)
}
stdin.Close()
if shared.IsBinaryContents(content) {
return nil, fmt.Errorf("binary file contents not supported")
}
} else {
content, err = ioutil.ReadFile(f)
isBinary, err := shared.IsBinaryFile(f)
if err != nil {
return fs, fmt.Errorf("failed to read file %s: %w", f, err)
}
isBinary, err = shared.IsBinaryContents(content)
if err != nil {
return nil, err
if isBinary {
return nil, fmt.Errorf("failed to upload %s: binary file not supported", f)
}
if isBinary {
return nil, fmt.Errorf("binary file not supported")
content, err = ioutil.ReadFile(f)
if err != nil {
return fs, fmt.Errorf("failed to read file %s: %w", f, err)
}
filename = path.Base(f)

View file

@ -144,31 +144,25 @@ func editRun(opts *EditOptions) error {
}
}
if _, ok := gist.Files[filename]; !ok {
gistFile, found := gist.Files[filename]
if !found {
return fmt.Errorf("gist has no file %q", filename)
}
isBinary, err := shared.IsBinaryContents([]byte(gist.Files[filename].Content))
if err != nil {
return err
}
if isBinary {
return fmt.Errorf("Editing binary files not supported")
if shared.IsBinaryContents([]byte(gistFile.Content)) {
return fmt.Errorf("editing binary files not supported")
}
editorCommand, err := cmdutil.DetermineEditor(opts.Config)
if err != nil {
return err
}
text, err := opts.Edit(editorCommand, filename, gist.Files[filename].Content, opts.IO)
text, err := opts.Edit(editorCommand, filename, gistFile.Content, opts.IO)
if err != nil {
return err
}
if text != gist.Files[filename].Content {
gistFile := gist.Files[filename]
if text != gistFile.Content {
gistFile.Content = text // so it appears if they re-edit
filesToUpdate[filename] = text
}

View file

@ -4,17 +4,16 @@ import (
"context"
"errors"
"fmt"
"github.com/gabriel-vasile/mimetype"
"net/http"
"net/url"
"strings"
"time"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghinstance"
"github.com/gabriel-vasile/mimetype"
"github.com/shurcooL/githubv4"
"github.com/shurcooL/graphql"
"github.com/cli/cli/api"
)
type GistFile struct {
@ -156,26 +155,22 @@ func IsBinaryFile(file string) (bool, error) {
}
isBinary := true
for mime := detectedMime; mime != nil; mime = mime.Parent() {
if mime.Is("text/plain") {
isBinary = false
break
}
}
return isBinary, nil
}
func IsBinaryContents(contents []byte) (bool, error) {
detectedMime := mimetype.Detect(contents)
func IsBinaryContents(contents []byte) bool {
isBinary := true
for mime := detectedMime; mime != nil; mime = mime.Parent() {
for mime := mimetype.Detect(contents); mime != nil; mime = mime.Parent() {
if mime.Is("text/plain") {
isBinary = false
break
}
}
return isBinary, nil
return isBinary
}

View file

@ -60,6 +60,14 @@ func TestIsBinaryContents(t *testing.T) {
want: false,
fileContent: []byte("package main"),
},
{
want: false,
fileContent: []byte(""),
},
{
want: false,
fileContent: []byte(nil),
},
{
want: true,
fileContent: []byte{239, 191, 189, 239, 191, 189, 239, 191, 189, 239,
@ -74,10 +82,6 @@ func TestIsBinaryContents(t *testing.T) {
}
for _, tt := range tests {
isBinary, err := IsBinaryContents(tt.fileContent)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, tt.want, isBinary)
assert.Equal(t, tt.want, IsBinaryContents(tt.fileContent))
}
}

View file

@ -118,13 +118,12 @@ func viewRun(opts *ViewOptions) error {
defer opts.IO.StopPager()
render := func(gf *shared.GistFile) error {
isBinary, err := shared.IsBinaryContents([]byte(gf.Content))
if err != nil {
return err
}
if isBinary {
gf.Content = "Skipping rendering binary content..."
if shared.IsBinaryContents([]byte(gf.Content)) {
if len(gist.Files) == 1 || opts.Filename != "" {
return fmt.Errorf("error: file is binary")
}
_, err = fmt.Fprintln(opts.IO.Out, cs.Gray("(skipping rendering binary content)"))
return nil
}
if strings.Contains(gf.Type, "markdown") && !opts.Raw {
@ -152,16 +151,6 @@ func viewRun(opts *ViewOptions) error {
if !ok {
return fmt.Errorf("gist has no such file: %q", opts.Filename)
}
isBinary, err := shared.IsBinaryContents([]byte(gistFile.Content))
if err != nil {
return err
}
if isBinary {
return fmt.Errorf("Error: file contents is binary")
}
return render(gistFile)
}
@ -175,17 +164,6 @@ func viewRun(opts *ViewOptions) error {
filenames = append(filenames, fn)
}
if len(filenames) == 1 {
isBinary, err := shared.IsBinaryContents([]byte(gist.Files[filenames[0]].Content))
if err != nil {
return err
}
if isBinary {
return fmt.Errorf("Error: file contents is binary")
}
}
sort.Strings(filenames)
if opts.ListFiles {
for _, fn := range filenames {