-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
74 lines (61 loc) · 1.73 KB
/
config.go
File metadata and controls
74 lines (61 loc) · 1.73 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
package objstr
import (
mapstructure_hooks "github.com/agnosticeng/mapstructure-hooks"
"github.com/agnosticeng/objstr/backend/impl/fs"
"github.com/agnosticeng/objstr/backend/impl/git"
"github.com/agnosticeng/objstr/backend/impl/http"
"github.com/agnosticeng/objstr/backend/impl/memory"
"github.com/agnosticeng/objstr/backend/impl/redis"
"github.com/agnosticeng/objstr/backend/impl/s3"
"github.com/agnosticeng/objstr/backend/impl/sftp"
"github.com/iancoleman/strcase"
"github.com/mitchellh/mapstructure"
)
type BackendConfig struct {
Memory *memory.MemoryBackendConfig
Fs *fs.FSBackendConfig
Http *http.HTTPBackendConfig
S3 *s3.S3BackendConfig
Sftp *sftp.SFTPBackendConfig
Redis *redis.RedisBackendConfig
Git *git.GitBackendConfig
}
type Config struct {
CopyBufferSize int
DefaultBackend string
BackendConfig
Backends map[string]BackendConfig
}
func (c *Config) WithBackend(scheme string, backendConf BackendConfig) *Config {
c.Backends[scheme] = backendConf
return c
}
func (c *Config) WithCopyBufferSize(size int) *Config {
c.CopyBufferSize = size
return c
}
func decodeValues[T any](values map[string][]string) (*T, error) {
var (
m = make(map[string]any)
res T
)
for k, v := range values {
if len(v) > 0 {
m[strcase.ToCamel(k)] = v[0]
}
}
var hooks []mapstructure.DecodeHookFunc
hooks = append(hooks, mapstructure.StringToTimeDurationHookFunc())
hooks = append(hooks, mapstructure_hooks.All()...)
var mdc mapstructure.DecoderConfig
mdc.Metadata = nil
mdc.Result = &res
mdc.WeaklyTypedInput = true
mdc.Squash = true
mdc.DecodeHook = mapstructure.ComposeDecodeHookFunc(hooks...)
d, err := mapstructure.NewDecoder(&mdc)
if err != nil {
return nil, err
}
return &res, d.Decode(m)
}