implement Save

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-05-08 17:07:32 +03:00
parent cd518afea3
commit afb3ef068d
2 changed files with 17 additions and 0 deletions

View File

@ -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 { for _, fn := range c.opts.AfterSave {
if err := fn(ctx, c); err != nil && !c.opts.AllowFail { if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
return err return err

View File

@ -1,6 +1,8 @@
package consul package consul
import ( import (
"time"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/unistack-org/micro/v3/config" "github.com/unistack-org/micro/v3/config"
) )
@ -36,3 +38,9 @@ func TLSConfig(t *tls.Config) config.Option {
return config.SetOption(tlsConfigKey{}, t) return config.SetOption(tlsConfigKey{}, t)
} }
*/ */
type timeoutKey struct{}
func Timeout(td time.Duration) config.Option {
return config.SetOption(timeoutKey{}, td)
}