-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from Shopify/seb-utils
Add utility functions
- Loading branch information
Showing
3 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
} |