Skip to content

Commit

Permalink
Merge pull request #15 from Shopify/seb-utils
Browse files Browse the repository at this point in the history
Add utility functions
  • Loading branch information
lavoiesl authored Jan 22, 2020
2 parents fa73ce4 + 2d11097 commit 8daffa4
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ type isNotExister interface {
// IsNotExist returns a boolean indicating whether the error is known to report that
// a path does not exist.
func IsNotExist(err error) bool {
e, ok := err.(isNotExister)
return ok && e.isNotExist()
var e isNotExister
if err != nil && errors.As(err, &e) {
return e.isNotExist()
}
return false
}

// notExistError is returned from FS.Open implementations when a requested
Expand Down
54 changes: 54 additions & 0 deletions pkg/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package storage

import (
"context"
"fmt"
"io"
"io/ioutil"
)

func Read(ctx context.Context, fs FS, path string, options *ReaderOptions) ([]byte, error) {
var file *File
var err error

if file, err = fs.Open(ctx, path, options); err != nil {
return nil, fmt.Errorf("unable to open %s: %w", path, err)
}

var data []byte
if data, err = ioutil.ReadAll(file); err != nil {
_ = file.Close() // Best effort at cleaning up
return nil, fmt.Errorf("unable to read %s: %w", path, err)
}

if err = file.Close(); err != nil {
return data, fmt.Errorf("unable to close %s: %w", path, err)
}

return data, nil
}

func Write(ctx context.Context, fs FS, path string, data []byte, options *WriterOptions) error {
var w io.WriteCloser
var err error

if w, err = fs.Create(ctx, path, options); err != nil {
return fmt.Errorf("unable to create %s: %w", path, err)
}

if _, err = w.Write(data); err != nil {
_ = w.Close() // Best effort at cleaning up
return fmt.Errorf("unable to write %s: %w", path, err)
}

if err = w.Close(); err != nil {
return fmt.Errorf("unable to close %s: %w", path, err)
}

return nil
}

func Exists(ctx context.Context, fs FS, path string) bool {
attrs, _ := fs.Attributes(ctx, path, nil)
return attrs != nil
}
39 changes: 39 additions & 0 deletions pkg/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package storage_test

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/Shopify/go-storage/pkg"
)

func TestExists(t *testing.T) {
ctx := context.Background()

withMem(func(fs storage.FS) {
assert.False(t, storage.Exists(ctx, fs, "foo"))
testCreate(t, fs, "foo", "bar")
assert.True(t, storage.Exists(ctx, fs, "foo"))
})
}

func TestRead(t *testing.T) {
ctx := context.Background()
withMem(func(fs storage.FS) {
testCreate(t, fs, "foo", "bar")

data, err := storage.Read(ctx, fs, "foo", nil)
assert.NoError(t, err)
assert.Equal(t, []byte("bar"), data)
})
}

func TestWrite(t *testing.T) {
ctx := context.Background()
withMem(func(fs storage.FS) {
assert.NoError(t, storage.Write(ctx, fs, "foo", []byte("bar"), nil))
testOpenExists(t, fs, "foo", "bar")
})
}

0 comments on commit 8daffa4

Please sign in to comment.