From 44024bcd6a0ace6e6d68d3dda497ae392381ed2e Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sat, 19 Dec 2020 23:31:14 +0300 Subject: [PATCH] update for micro changes Signed-off-by: Vasiliy Tolstov --- file.go | 30 +++++++++++++++--------------- go.mod | 2 +- go.sum | 2 ++ 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/file.go b/file.go index 0727fc0..9dff097 100644 --- a/file.go +++ b/file.go @@ -49,28 +49,28 @@ 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 { + if err := fn(ctx, c); err != nil && !c.opts.AllowFail { return err } } fp, err := os.OpenFile(c.path, os.O_RDONLY, os.FileMode(0400)) - if err != nil { + if err != nil && !c.opts.AllowFail { return ErrPathNotExist - } - defer fp.Close() - - buf, err := ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize))) - if err != nil { - return err - } - - if err = c.opts.Codec.Unmarshal(buf, c.opts.Struct); err != nil { - return err + } else if err == nil { + defer fp.Close() + var buf []byte + buf, err = ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize))) + if err == nil { + err = c.opts.Codec.Unmarshal(buf, c.opts.Struct) + } + if err != nil && !c.opts.AllowFail { + return err + } } for _, fn := range c.opts.AfterLoad { - if err := fn(ctx, c); err != nil { + if err := fn(ctx, c); err != nil && !c.opts.AllowFail { return err } } @@ -80,13 +80,13 @@ func (c *fileConfig) Load(ctx context.Context) error { func (c *fileConfig) Save(ctx context.Context) error { for _, fn := range c.opts.BeforeSave { - if err := fn(ctx, c); err != nil { + if err := fn(ctx, c); err != nil && !c.opts.AllowFail { return err } } for _, fn := range c.opts.AfterSave { - if err := fn(ctx, c); err != nil { + if err := fn(ctx, c); err != nil && !c.opts.AllowFail { return err } } diff --git a/go.mod b/go.mod index 08d0fb9..a1429a7 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/unistack-org/micro-config-file go 1.15 -require github.com/unistack-org/micro/v3 v3.0.2-0.20201213102644-92aec349c3d4 +require github.com/unistack-org/micro/v3 v3.0.2-0.20201219202205-150e8ad698bb diff --git a/go.sum b/go.sum index 2785182..50d3b5c 100644 --- a/go.sum +++ b/go.sum @@ -241,6 +241,8 @@ github.com/transip/gotransip v0.0.0-20190812104329-6d8d9179b66f/go.mod h1:i0f4R4 github.com/uber-go/atomic v1.3.2/go.mod h1:/Ct5t2lcmbJ4OSe/waGBoaVvVqtO0bmtfVNex1PFV8g= github.com/unistack-org/micro/v3 v3.0.2-0.20201213102644-92aec349c3d4 h1:SmwEx/EYb7iUOdDZHiamtwRFfnhiZjtzmVdLZq+KQM0= github.com/unistack-org/micro/v3 v3.0.2-0.20201213102644-92aec349c3d4/go.mod h1:0DgOy4OdJxQCDER8YSKitZugd2+1bddrRSNfeooTHDc= +github.com/unistack-org/micro/v3 v3.0.2-0.20201219202205-150e8ad698bb h1:cJGF8j30idReDyjRCvEJN5waWCh/AKuiKEqLA8OLMlY= +github.com/unistack-org/micro/v3 v3.0.2-0.20201219202205-150e8ad698bb/go.mod h1:0DgOy4OdJxQCDER8YSKitZugd2+1bddrRSNfeooTHDc= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/vultr/govultr v0.1.4/go.mod h1:9H008Uxr/C4vFNGLqKx232C206GL0PBHzOP0809bGNA= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=