This commit is contained in:
A.Unger
2020-01-31 13:09:07 +01:00
parent 29f4ba30cb
commit 351694d22d
6 changed files with 183 additions and 10 deletions
+20 -9
View File
@@ -4,24 +4,34 @@ package store
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
mstore "github.com/micro/go-micro/v2/store"
olog "github.com/owncloud/ocis-pkg/log"
)
// DefaultPath assumes UNIX
var DefaultPath string = "/var/tmp/ocis-store"
// Logger is a global logger
var Logger olog.Logger
// Store interacts with the filesystem to manage account information
type Store struct {
logger olog.Logger
mountPath string
}
// New returns a new file system store manager
// TODO add mountPath as a flag
// TODO add mountPath as a flag. Accept a *config argument
func New() Store {
// default to the current working directory if not configured
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
// TODO log error
// TODO deal with this
}
return Store{
mountPath: DefaultPath,
mountPath: dir,
logger: olog.NewLogger(),
}
}
@@ -42,16 +52,17 @@ func (s *Store) Read(key string, opts ...mstore.ReadOption) ([]*mstore.Record, e
func (s *Store) Write(rec *mstore.Record) error {
path := filepath.Join(s.mountPath, rec.Key)
if filepath.IsAbs(path) {
// TODO WARN, storage is relative to the service directory
}
if len(rec.Key) < 1 {
// TODO log error: empty key
return fmt.Errorf("%v", "key is empty")
}
return ioutil.WriteFile(path, rec.Value, 0644)
if err := ioutil.WriteFile(path, rec.Value, 0644); err != nil {
return err
}
s.logger.Info().Msgf("%v bytes written to %v", len(rec.Value), path)
return nil
}
// Delete implements the store interface