refactor: storage interface
This commit is contained in:
+16
-13
@@ -1,27 +1,30 @@
|
||||
// fs implements Storage on filesystem
|
||||
// fs implements galaxy/storage.Storage with filesystem
|
||||
package fs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"galaxy/util"
|
||||
)
|
||||
|
||||
type fs struct {
|
||||
rootPath string
|
||||
type fsStorage struct {
|
||||
path string
|
||||
}
|
||||
|
||||
// NewStorage returns on-filesystem implementation of the "galaxy/storage.Storage" with root located at rootPath.
|
||||
// NewFS returns on-filesystem implementation of the "galaxy/storage.Storage" with root located at rootPath.
|
||||
// rootPath must me a directory and has write access to the current user. If initial checks failed, return nil and non-nil error.
|
||||
func NewFS(rootPath string) (*fs, error) {
|
||||
ok, err := util.Writable(rootPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
func NewStorage(path string) (*fsStorage, error) {
|
||||
if ok, err := util.PathExists(path, true); err != nil {
|
||||
return nil, fmt.Errorf("new storage: check path %q exists: %w", path, err)
|
||||
} else if !ok {
|
||||
return nil, fmt.Errorf("new storage: path %q does not exists", path)
|
||||
}
|
||||
if !ok {
|
||||
return nil, errors.New("user does not have write permissions to the storage root")
|
||||
if ok, err := util.Writable(path); err != nil {
|
||||
return nil, fmt.Errorf("new storage: check path %q writable: %w", path, err)
|
||||
} else if !ok {
|
||||
return nil, fmt.Errorf("new storage: path %q is not writable", path)
|
||||
}
|
||||
s := &fs{
|
||||
rootPath: rootPath,
|
||||
s := &fsStorage{
|
||||
path: path,
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user