From afb3ef068d729f8753e24aeb6b3d84bb048c4ba4 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sat, 8 May 2021 17:07:32 +0300 Subject: [PATCH] implement Save Signed-off-by: Vasiliy Tolstov --- consul.go | 9 +++++++++ options.go | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/consul.go b/consul.go index a54d39b..28bf9f6 100644 --- a/consul.go +++ b/consul.go @@ -127,6 +127,15 @@ func (c *consulConfig) Save(ctx context.Context) error { } } + buf, err := c.opts.Codec.Marshal(c.opts.Struct) + if err == nil { + _, err = c.cli.KV().Put(&api.KVPair{Key: c.path, Value: buf}, nil) + } + + if err != nil && !c.opts.AllowFail { + return fmt.Errorf("consul path save error: %v", err) + } + for _, fn := range c.opts.AfterSave { if err := fn(ctx, c); err != nil && !c.opts.AllowFail { return err diff --git a/options.go b/options.go index 267731a..51977d0 100644 --- a/options.go +++ b/options.go @@ -1,6 +1,8 @@ package consul import ( + "time" + "github.com/hashicorp/consul/api" "github.com/unistack-org/micro/v3/config" ) @@ -36,3 +38,9 @@ func TLSConfig(t *tls.Config) config.Option { return config.SetOption(tlsConfigKey{}, t) } */ + +type timeoutKey struct{} + +func Timeout(td time.Duration) config.Option { + return config.SetOption(timeoutKey{}, td) +}