-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
61 lines (51 loc) · 1.27 KB
/
files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package files
import (
"errors"
"os"
"strings"
"github.com/google/wire"
"github.com/spf13/viper"
"go.uber.org/zap"
)
// Options is log configuration struct
type Options struct {
Type string `yaml:"type"`
Base string `yaml:"base"`
}
type FileManager interface {
SetBase(base string)
GetBase() string
CreateFile(fileName string, bytes []byte) error
CreateDirectory(fileName string) error
FetchFile(fileName string) ([]byte, error)
FetchFileInfo(fileName string) (os.FileInfo, error)
IsFileExists(fileName string) (bool, error)
IsDirectoryExists(fileName string) (bool, error)
GetFilesAndDirs(dirname string) ([]string, []string, error)
ArchiveDirectory(fileName string) (file *os.File, err error)
}
func NewOptions(v *viper.Viper, logger *zap.Logger) (*Options, error) {
var (
err error
o = new(Options)
)
if err = v.UnmarshalKey("volumes", o); err != nil {
return nil, err
}
logger.Info("load file manager configuration success")
return o, err
}
// New for file library
func New(o *Options) (FileManager, error) {
switch strings.ToLower(o.Type) {
case "local":
{
fm := &LocalFileManager{}
fm.SetBase(o.Base)
return fm, nil
}
default:
return nil, errors.New("unknown store type: " + o.Type)
}
}
var ProviderSet = wire.NewSet(New, NewOptions)