From 992a98e3f831c0df5d00804ccc7e1ce0ac6f7ca3 Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 22 Apr 2020 21:04:59 -0700 Subject: [PATCH] refactored/tested Read/WriteBinary --- dpipes/execute.go | 33 ++------------------------------- dpipes/gopen.go | 30 +++++++++++++++++++++++++++++- dpipes/gopen_test.go | 27 +++++++++++++++++++++++++++ tarp/create.go | 3 ++- 4 files changed, 60 insertions(+), 33 deletions(-) diff --git a/dpipes/execute.go b/dpipes/execute.go index c9e8138..534f574 100644 --- a/dpipes/execute.go +++ b/dpipes/execute.go @@ -1,9 +1,7 @@ package dpipes import ( - "bytes" "fmt" - "io" "io/ioutil" "os" "os/exec" @@ -11,34 +9,6 @@ import ( "path/filepath" ) -// WriteBinary writes the bytes to disk at fname. -func WriteBinary(fname string, data []byte) { - stream, err := GCreate(fname) - if err != nil { - panic(err) - } - defer stream.Close() - _, err = stream.Write(data) - if err != nil { - panic(err) - } -} - -// ReadBinary reads an entire file and returns a byte array. -func ReadBinary(fname string) []byte { - stream, err := GOpen(fname) - if err != nil { - panic(err) - } - // defer stream.Close() - buffer := bytes.NewBuffer(make([]byte, 0, 1000)) - _, err = io.Copy(buffer, stream) - if err != nil { - panic(err) - } - return buffer.Bytes() -} - // UnpackInDir unpacks a sample into a directory/prefix // given by fprefix. You need to append the "." in fprefix // since this function just concatenates fprefix with each @@ -60,7 +30,8 @@ func PackDir(dir, fprefix string) Sample { Handle(err) var sample Sample = make(Sample) for _, match := range matches { - data := ReadBinary(match) + data, err := ReadBinary(match) + Handle(err) _, suffix := FnameSplit(match) sample[suffix] = data } diff --git a/dpipes/gopen.go b/dpipes/gopen.go index 473248e..3adc17e 100644 --- a/dpipes/gopen.go +++ b/dpipes/gopen.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "strings" + "bytes" ) // TODO @@ -14,11 +15,12 @@ import ( // - support common protocols directly via Go libraries type waitCloser struct { - io.Reader + io.ReadCloser cmd *exec.Cmd } func (c waitCloser) Close() error { + c.ReadCloser.Close() return c.cmd.Wait() } @@ -63,3 +65,29 @@ func GCreate(fname string) (io.WriteCloser, error) { } return os.Create(fname) } + +// WriteBinary writes the bytes to disk at fname. +func WriteBinary(fname string, data []byte) error { + stream, err := GCreate(fname) + if err != nil { + return err + } + defer stream.Close() + _, err = stream.Write(data) + return err +} + +// ReadBinary reads an entire file and returns a byte array. +func ReadBinary(fname string) ([]byte, error) { + stream, err := GOpen(fname) + if err != nil { + return make([]byte, 0), err + } + buffer := bytes.NewBuffer(make([]byte, 0, 1000)) + _, err = io.Copy(buffer, stream) + if err != nil { + return buffer.Bytes(), err + } + err = stream.Close() + return buffer.Bytes(), err +} diff --git a/dpipes/gopen_test.go b/dpipes/gopen_test.go index 9a62f45..91705b2 100644 --- a/dpipes/gopen_test.go +++ b/dpipes/gopen_test.go @@ -62,3 +62,30 @@ func TestGopenPipe(t *testing.T) { data = data[:n] assert.Equal(t, data, []byte("abcdef\n")) } + +func TestGopenPipeFalse(t *testing.T) { + stream, err := GOpen("pipe:/bin/false") + assert.Nil(t, err) + data := make([]byte, 10000) + _, err = stream.Read(data) + err = stream.Close() + assert.NotNil(t, err) +} + +func TestReadBinary(t *testing.T) { + data, err := ReadBinary("/dev/null") + assert.Nil(t, err) + assert.Equal(t, len(data), 0) +} + +func TestReadBinaryPipe(t *testing.T) { + data, err := ReadBinary("pipe:echo abc") + assert.Nil(t, err) + assert.Equal(t, len(data), 4) + assert.Equal(t, []byte("abc\n"), data) +} + +func TestReadBinaryPipeFalse(t *testing.T) { + _, err := ReadBinary("pipe:/bin/false") + assert.NotNil(t, err) +} diff --git a/tarp/create.go b/tarp/create.go index c2e9a0c..edc4c72 100644 --- a/tarp/create.go +++ b/tarp/create.go @@ -40,7 +40,8 @@ func createcmd() { infolog.Println(fields) Validate(len(fields) == 2, "bad input line at", lineno) output, source := fields[0], fields[1] - contents := dpipes.ReadBinary(source) + contents, err := dpipes.ReadBinary(source) + Handle(err) outch <- dpipes.Raw{output, contents} } close(outch)