Skip to content

Commit

Permalink
coreutils, services: Refactor Open related logic (beyondstorage#264)
Browse files Browse the repository at this point in the history
* coreutils, services: Refactor Open related logic

Signed-off-by: Xuanwo <github@xuanwo.io>

* services: Move op name to const

Signed-off-by: Xuanwo <github@xuanwo.io>

* services/qingstor: Fix unit test for new storage

Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo authored Apr 23, 2020
1 parent d47a773 commit ac45862
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
8 changes: 4 additions & 4 deletions services/fs/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions services/fs/storager.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *Storage) Metadata(pairs ...*types.Pair) (m metadata.StorageMeta, err er
// ListDir implements Storager.ListDir
func (s *Storage) ListDir(path string, pairs ...*types.Pair) (err error) {
defer func() {
err = s.formatError("list_dir", err, path)
err = s.formatError(services.OpListDir, err, path)
}()

opt, err := s.parsePairListDir(pairs...)
Expand Down Expand Up @@ -95,7 +95,7 @@ func (s *Storage) ListDir(path string, pairs ...*types.Pair) (err error) {
// Read implements Storager.Read
func (s *Storage) Read(path string, pairs ...*types.Pair) (r io.ReadCloser, err error) {
defer func() {
err = s.formatError("read", err, path)
err = s.formatError(services.OpRead, err, path)
}()

opt, err := s.parsePairRead(pairs...)
Expand Down Expand Up @@ -138,7 +138,7 @@ func (s *Storage) Read(path string, pairs ...*types.Pair) (r io.ReadCloser, err
// WriteFile implements Storager.WriteFile
func (s *Storage) Write(path string, r io.Reader, pairs ...*types.Pair) (err error) {
defer func() {
err = s.formatError("write", err, path)
err = s.formatError(services.OpWrite, err, path)
}()

opt, err := s.parsePairWrite(pairs...)
Expand Down Expand Up @@ -183,7 +183,7 @@ func (s *Storage) Write(path string, r io.Reader, pairs ...*types.Pair) (err err
// Stat implements Storager.Stat
func (s *Storage) Stat(path string, pairs ...*types.Pair) (o *types.Object, err error) {
defer func() {
err = s.formatError("stat", err, path)
err = s.formatError(services.OpStat, err, path)
}()

if path == "-" {
Expand Down Expand Up @@ -235,7 +235,7 @@ func (s *Storage) Stat(path string, pairs ...*types.Pair) (o *types.Object, err
// Delete implements Storager.Delete
func (s *Storage) Delete(path string, pairs ...*types.Pair) (err error) {
defer func() {
err = s.formatError("delete", err, path)
err = s.formatError(services.OpDelete, err, path)
}()

rp := s.getAbsPath(path)
Expand All @@ -250,7 +250,7 @@ func (s *Storage) Delete(path string, pairs ...*types.Pair) (err error) {
// Copy implements Storager.Copy
func (s *Storage) Copy(src, dst string, pairs ...*types.Pair) (err error) {
defer func() {
err = s.formatError("copy", err, src, dst)
err = s.formatError(services.OpCopy, err, src, dst)
}()

rs := s.getAbsPath(src)
Expand Down Expand Up @@ -284,7 +284,7 @@ func (s *Storage) Copy(src, dst string, pairs ...*types.Pair) (err error) {
// Move implements Storager.Move
func (s *Storage) Move(src, dst string, pairs ...*types.Pair) (err error) {
defer func() {
err = s.formatError("move", err, src, dst)
err = s.formatError(services.OpMove, err, src, dst)
}()

rs := s.getAbsPath(src)
Expand All @@ -305,7 +305,7 @@ func (s *Storage) Move(src, dst string, pairs ...*types.Pair) (err error) {

func (s *Storage) createDir(path string) (err error) {
defer func() {
err = s.formatError("create dir", err, path)
err = s.formatError("create_dir", err, path)
}()

rp := s.getDirPath(path)
Expand Down
18 changes: 11 additions & 7 deletions services/fs/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ import (
"github.com/Xuanwo/storage/types"
)

// New will create a fs client.
func New(pairs ...*types.Pair) (_ storage.Servicer, _ storage.Storager, err error) {
// NewStorager will create Storager only.
func NewStorager(pairs ...*types.Pair) (storage.Storager, error) {
return newStorager(pairs...)
}

// newStorager will create a fs client.
func newStorager(pairs ...*types.Pair) (store *Storage, err error) {
defer func() {
if err != nil {
err = &services.InitError{Type: Type, Err: err, Pairs: pairs}
err = &services.InitError{Op: services.OpNewStorager, Type: Type, Err: err, Pairs: pairs}
}
}()

opt, err := parseStoragePairNew(pairs...)
if err != nil {
return nil, nil, err
return
}

store := &Storage{
store = &Storage{
ioCopyBuffer: io.CopyBuffer,
ioCopyN: io.CopyN,
ioutilReadDir: ioutil.ReadDir,
Expand All @@ -41,7 +45,7 @@ func New(pairs ...*types.Pair) (_ storage.Servicer, _ storage.Storager, err erro
if opt.HasWorkDir {
store.workDir = opt.WorkDir
}
return nil, store, nil
return
}

func formatError(err error) error {
Expand Down
3 changes: 1 addition & 2 deletions services/fs/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
)

func TestNewClient(t *testing.T) {
srv, c, err := New()
assert.Nil(t, srv)
c, err := NewStorager()
assert.NotNil(t, c)
assert.NoError(t, err)
}
Expand Down

0 comments on commit ac45862

Please sign in to comment.