Skip to content

Commit 3ccdd70

Browse files
committed
Add options pattern for storage api
1 parent 97b9c1f commit 3ccdd70

File tree

6 files changed

+30
-6
lines changed

6 files changed

+30
-6
lines changed

pbm/storage/azure/azure.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (*Blob) Type() storage.Type {
128128
return storage.Azure
129129
}
130130

131-
func (b *Blob) Save(name string, data io.Reader, sizeb int64) error {
131+
func (b *Blob) Save(name string, data io.Reader, sizeb int64, options ...storage.Option) error {
132132
bufsz := defaultUploadBuff
133133
if sizeb > 0 {
134134
ps := int(sizeb / maxBlocks * 11 / 10) // add 10% just in case

pbm/storage/blackhole/blackhole.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (*Blackhole) Type() storage.Type {
1818
return storage.Blackhole
1919
}
2020

21-
func (*Blackhole) Save(_ string, data io.Reader, _ int64) error {
21+
func (*Blackhole) Save(_ string, data io.Reader, _ int64, _ ...storage.Option) error {
2222
_, err := io.Copy(io.Discard, data)
2323
return err
2424
}

pbm/storage/fs/fs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func writeSync(finalpath string, data io.Reader) (err error) {
151151
return nil
152152
}
153153

154-
func (fs *FS) Save(name string, data io.Reader, _ int64) error {
154+
func (fs *FS) Save(name string, data io.Reader, _ int64, _ ...storage.Option) error {
155155
return writeSync(path.Join(fs.root, name), data)
156156
}
157157

pbm/storage/gcs/gcs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (*GCS) Type() storage.Type {
142142
return storage.GCS
143143
}
144144

145-
func (g *GCS) Save(name string, data io.Reader, size int64) error {
145+
func (g *GCS) Save(name string, data io.Reader, size int64, _ ...storage.Option) error {
146146
ctx := context.Background()
147147

148148
w := g.bucketHandle.Object(path.Join(g.opts.Prefix, name)).NewWriter(ctx)

pbm/storage/s3/s3.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func (*S3) Type() storage.Type {
314314
return storage.S3
315315
}
316316

317-
func (s *S3) Save(name string, data io.Reader, sizeb int64) error {
317+
func (s *S3) Save(name string, data io.Reader, sizeb int64, options ...storage.Option) error {
318318
cc := runtime.NumCPU() / 2
319319
if cc == 0 {
320320
cc = 1

pbm/storage/storage.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type FileInfo struct {
3737

3838
type Storage interface {
3939
Type() Type
40-
Save(name string, data io.Reader, size int64) error
40+
Save(name string, data io.Reader, size int64, opts ...Option) error
4141
SourceReader(name string) (io.ReadCloser, error)
4242
// FileStat returns file info. It returns error if file is empty or not exists.
4343
FileStat(name string) (FileInfo, error)
@@ -69,6 +69,30 @@ func ParseType(s string) Type {
6969
}
7070
}
7171

72+
// Opts represents storage options
73+
type Opts struct {
74+
UseLogger bool
75+
}
76+
77+
// GetDefaultOpts creates default options.
78+
func GetDefaultOpts() *Opts {
79+
return &Opts{
80+
UseLogger: true,
81+
}
82+
}
83+
84+
// Option is function for setting the storage option
85+
type Option func(*Opts) error
86+
87+
// UseLogger option enables/disables logger when working with storage.
88+
// Logger is enabled by default.
89+
func UseLogger(useLogger bool) Option {
90+
return func(o *Opts) error {
91+
o.UseLogger = useLogger
92+
return nil
93+
}
94+
}
95+
7296
// IsInitialized checks if there is PBM init file on the storage.
7397
func IsInitialized(ctx context.Context, stg Storage) (bool, error) {
7498
_, err := stg.FileStat(defs.StorInitFile)

0 commit comments

Comments
 (0)