-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconfig.go
71 lines (60 loc) · 1.33 KB
/
config.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
62
63
64
65
66
67
68
69
70
71
package main
import (
"errors"
"io"
"os"
"strings"
"time"
"github.com/pelletier/go-toml/v2"
)
const Delimiter = "/"
type Duration time.Duration
func (d *Duration) UnmarshalText(data []byte) error {
val, err := time.ParseDuration(string(data))
*d = Duration(val)
return err
}
type S3Credentials struct {
ID string
Secret string
Token string
}
type S3Config struct {
Region *string
Endpoint *string
Bucket string
BasePrefix string `toml:"base_prefix"`
RequestPresignExpiry Duration `toml:"request_presign_expiry"`
ForcePathStyle bool `toml:"force_path_style"`
Credentials *S3Credentials
}
type Config struct {
S3 S3Config
}
var errMissingBucket = errors.New("s3 bucket is required")
func newConfig(r io.Reader) (*Config, error) {
cfg := &Config{
S3: S3Config{
RequestPresignExpiry: Duration(2 * time.Hour),
},
}
dec := toml.NewDecoder(r)
if err := dec.Decode(cfg); err != nil {
return nil, err
}
if cfg.S3.Bucket == "" {
return nil, errMissingBucket
}
if cfg.S3.BasePrefix != "" && !strings.HasSuffix(cfg.S3.BasePrefix, Delimiter) {
cfg.S3.BasePrefix += Delimiter
}
return cfg, nil
}
func NewConfig(path string) (*Config, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return newConfig(f)
}