/
option.go
76 lines (63 loc) · 1.58 KB
/
option.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
72
73
74
75
76
package maleos3
import (
"github.com/aws/aws-sdk-go-v2/service/s3"
)
type Option interface {
apply(*S3)
}
type OptionFunc func(*S3)
func (f OptionFunc) apply(s *S3) {
f(s)
}
// WithClient sets the S3 client to use.
func WithClient(client *s3.Client) Option {
return OptionFunc(func(s *S3) {
s.Client = client
})
}
// WithRegion sets the region to use.
func WithRegion(region string) Option {
return OptionFunc(func(s *S3) {
s.region = region
})
}
// WithBucket sets the bucket name to use.
func WithBucket(name string) Option {
return OptionFunc(func(s *S3) {
s.bucket = name
})
}
// WithSecure sets to use https or not.
func WithSecure(secure bool) Option {
return OptionFunc(func(s *S3) {
s.secure = secure
})
}
// WithURLBuilder sets the url builder to use.
func WithURLBuilder(builder URLBuilder) Option {
return OptionFunc(func(s *S3) {
s.urlBuilder = builder
})
}
type StringerFunc func() string
func (f StringerFunc) String() string {
return f()
}
// WithFilenamePretext sets the pretext to use for the filename to be uploaded to the bucket.
func WithFilenamePretext(pretext string) Option {
return OptionFunc(func(s *S3) {
s.pretext = StringerFunc(func() string {
return pretext
})
})
}
// WithFilenameDynamicPretext sets the pretext to use for the filename to be uploaded to the bucket.
//
// example of creating yearly folder in bucket:
//
// WithFilenameDynamicPretext(func() string { return time.Now().Format("2006") + "/" })
func WithFilenameDynamicPretext(pretext func() string) Option {
return OptionFunc(func(s *S3) {
s.pretext = StringerFunc(pretext)
})
}