From 80bec09a07bddb43d1c6e3fce3c58bdd18658e4f Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 18 Dec 2020 03:41:42 +0300 Subject: [PATCH] before/after config stuff handled by implementations Signed-off-by: Vasiliy Tolstov --- file.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/file.go b/file.go index c87767a..0727fc0 100644 --- a/file.go +++ b/file.go @@ -48,6 +48,12 @@ func (c *fileConfig) Init(opts ...config.Option) error { } func (c *fileConfig) Load(ctx context.Context) error { + for _, fn := range c.opts.BeforeLoad { + if err := fn(ctx, c); err != nil { + return err + } + } + fp, err := os.OpenFile(c.path, os.O_RDONLY, os.FileMode(0400)) if err != nil { return ErrPathNotExist @@ -59,10 +65,32 @@ func (c *fileConfig) Load(ctx context.Context) error { return err } - return c.opts.Codec.Unmarshal(buf, c.opts.Struct) + if err = c.opts.Codec.Unmarshal(buf, c.opts.Struct); err != nil { + return err + } + + for _, fn := range c.opts.AfterLoad { + if err := fn(ctx, c); err != nil { + return err + } + } + + return nil } func (c *fileConfig) Save(ctx context.Context) error { + for _, fn := range c.opts.BeforeSave { + if err := fn(ctx, c); err != nil { + return err + } + } + + for _, fn := range c.opts.AfterSave { + if err := fn(ctx, c); err != nil { + return err + } + } + return nil }