Skip to content

Commit

Permalink
refactored/tested Read/WriteBinary
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom committed Apr 23, 2020
1 parent 57fe1ec commit 992a98e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
33 changes: 2 additions & 31 deletions dpipes/execute.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,14 @@
package dpipes

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"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
Expand All @@ -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
}
Expand Down
30 changes: 29 additions & 1 deletion dpipes/gopen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"strings"
"bytes"
)

// TODO
Expand All @@ -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()
}

Expand Down Expand Up @@ -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
}
27 changes: 27 additions & 0 deletions dpipes/gopen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion tarp/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 992a98e

Please sign in to comment.