From b8acc5f8af2c191360734280d44328d4307e90d8 Mon Sep 17 00:00:00 2001 From: Lennart Date: Fri, 30 Jul 2021 13:30:40 +0200 Subject: [PATCH] web/file2: add createfs.go --- web/file2/createfs.go | 59 +++++++++++++++++++++++++++++++++++++++++ web/file2/fs/fs_test.go | 7 ++--- web/file2/s3/fs_test.go | 10 +++---- 3 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 web/file2/createfs.go diff --git a/web/file2/createfs.go b/web/file2/createfs.go new file mode 100644 index 0000000..b605614 --- /dev/null +++ b/web/file2/createfs.go @@ -0,0 +1,59 @@ +package file + +import ( + "errors" + + "dev.sum7.eu/genofire/golang-lib/web/file2/fs" + "dev.sum7.eu/genofire/golang-lib/web/file2/s3" +) + +// fsType represents a type of file store. +type fsType int + +const ( + typeFS fsType = iota + typeS3 +) + +var stringToType = map[string]fsType{ + "fs": typeFS, + "s3": typeS3, +} + +func (t *fsType) UnmarshalText(input []byte) error { + val, ok := stringToType[string(input)] + if !ok { + return errors.New("invalid file store type") + } + *t = val + return nil +} + +// FSInfo is a TOML structure storing access information about a file store. +type FSInfo struct { + fstype fsType `toml:"type"` + // file system + root string `toml:",omitempty"` + // s3 + endpoint string `toml:",omitempty"` + secure bool `toml:",omitempty"` + id string `toml:",omitempty"` + secret string `toml:",omitempty"` + bucket string `toml:",omitempty"` + location string `toml:",omitempty"` +} + +// Create creates a file store from the information provided. +func (i *FSInfo) Create() (FS, error) { + switch i.fstype { + case typeFS: + if len(i.root) == 0 { + return nil, errors.New("no file store root") + } + return &fs.FS{Root: i.root}, nil + case typeS3: + return s3.New(i.endpoint, i.secure, i.id, i.secret, i.bucket, i.location) + default: + return nil, errors.New("FSInfo.Create not implemented for provided file store type") + } +} diff --git a/web/file2/fs/fs_test.go b/web/file2/fs/fs_test.go index 9a7c628..fd58242 100644 --- a/web/file2/fs/fs_test.go +++ b/web/file2/fs/fs_test.go @@ -1,4 +1,4 @@ -package fs +package fs_test import ( "io" @@ -10,11 +10,12 @@ import ( "github.com/stretchr/testify/assert" "dev.sum7.eu/genofire/golang-lib/web/file2" + "dev.sum7.eu/genofire/golang-lib/web/file2/fs" ) func TestOpenStat(t *testing.T) { assert := assert.New(t) - var fs file.FS = &FS{Root: "./testdata"} + var fs file.FS = &fs.FS{Root: "./testdata"} assert.NoError(fs.Check()) f, err := fs.Open("glenda") @@ -32,7 +33,7 @@ func TestOpenStat(t *testing.T) { func TestCreateOpenUUIDRead(t *testing.T) { assert := assert.New(t) - var fs file.FS = &FS{Root: "./testdata"} + var fs file.FS = &fs.FS{Root: "./testdata"} assert.NoError(fs.Check()) err := fs.Store(uuid.MustParse("f9375ccb-ee09-4ecf-917e-b88725efcb68"), "$name", "text/plain", strings.NewReader("hello, world\n")) diff --git a/web/file2/s3/fs_test.go b/web/file2/s3/fs_test.go index ccd4ee1..84e8c83 100644 --- a/web/file2/s3/fs_test.go +++ b/web/file2/s3/fs_test.go @@ -1,4 +1,4 @@ -package s3 +package s3_test import ( "testing" @@ -13,12 +13,12 @@ import ( func TestTypes(t *testing.T) { assert := assert.New(t) - var fs file.FS - fs, err := New("127.0.0.1", false, "", "", "", "") - _ = fs + var fstore file.FS + fstore, err := s3.New("127.0.0.1", false, "", "", "", "") + _ = fstore assert.Error(err) } func ExampleNew() { - New("play.min.io", true, "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", "file-store", "") + s3.New("play.min.io", true, "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", "file-store", "") }