Compare commits

...

3 Commits

Author SHA1 Message Date
80462220e2 update import paths
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-10-26 23:00:25 +03:00
a4002ae8eb update for latest micro
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-09-30 01:28:03 +03:00
e0cbe28c8f add debug to init func
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-09-30 00:42:48 +03:00
6 changed files with 35 additions and 62 deletions

View File

@@ -1,4 +1,4 @@
package consul package consul // import "go.unistack.org/micro-config-consul/v3"
import ( import (
"context" "context"
@@ -6,8 +6,8 @@ import (
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/imdario/mergo" "github.com/imdario/mergo"
"github.com/unistack-org/micro/v3/config" "go.unistack.org/micro/v3/config"
rutil "github.com/unistack-org/micro/v3/util/reflect" rutil "go.unistack.org/micro/v3/util/reflect"
) )
var DefaultStructTag = "consul" var DefaultStructTag = "consul"
@@ -68,9 +68,12 @@ func (c *consulConfig) Init(opts ...config.Option) error {
} }
cli, err := api.NewClient(cfg) cli, err := api.NewClient(cfg)
if err != nil && !c.opts.AllowFail { if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul init err: %v", err)
if !c.opts.AllowFail {
return err return err
} }
}
c.cli = cli c.cli = cli
c.path = path c.path = path
@@ -79,38 +82,23 @@ func (c *consulConfig) Init(opts ...config.Option) error {
} }
func (c *consulConfig) Load(ctx context.Context, opts ...config.LoadOption) error { func (c *consulConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
for _, fn := range c.opts.BeforeLoad { if err := config.DefaultBeforeLoad(ctx, c); err != nil {
if err := fn(ctx, c); err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul load err: %v", err)
if !c.opts.AllowFail {
return err return err
} }
}
}
pair, _, err := c.cli.KV().Get(c.path, nil) pair, _, err := c.cli.KV().Get(c.path, nil)
if err != nil { if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul load path %s err: %v", c.path, err) err = fmt.Errorf("consul path %s load error: %w", c.path, err)
if !c.opts.AllowFail {
return fmt.Errorf("consul path %s load error: %v", c.path, err)
}
} else if pair == nil { } else if pair == nil {
c.opts.Logger.Errorf(c.opts.Context, "consul load path %s not found", c.path) err = fmt.Errorf("consul path %s load error: not found", c.path)
if !c.opts.AllowFail {
return fmt.Errorf("consul path %s not found", c.path)
}
} }
if err != nil || pair == nil { if err != nil {
for _, fn := range c.opts.AfterLoad { c.opts.Logger.Error(c.opts.Context, err)
if err := fn(ctx, c); err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul load err: %v", err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
} }
} return config.DefaultAfterLoad(ctx, c)
}
return nil
} }
options := config.NewLoadOptions(opts...) options := config.NewLoadOptions(opts...)
@@ -142,27 +130,17 @@ func (c *consulConfig) Load(ctx context.Context, opts ...config.LoadOption) erro
} }
} }
for _, fn := range c.opts.AfterLoad { if err := config.DefaultAfterLoad(ctx, c); err != nil {
if err := fn(ctx, c); err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul load err: %v", err)
if !c.opts.AllowFail {
return err return err
} }
}
}
return nil return nil
} }
func (c *consulConfig) Save(ctx context.Context, opts ...config.SaveOption) error { func (c *consulConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
for _, fn := range c.opts.BeforeSave { if err := config.DefaultBeforeSave(ctx, c); err != nil {
if err := fn(ctx, c); err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul save err: %v", err)
if !c.opts.AllowFail {
return err return err
} }
}
}
buf, err := c.opts.Codec.Marshal(c.opts.Struct) buf, err := c.opts.Codec.Marshal(c.opts.Struct)
if err == nil { if err == nil {
@@ -170,20 +148,15 @@ func (c *consulConfig) Save(ctx context.Context, opts ...config.SaveOption) erro
} }
if err != nil { if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul path %s save err: %v", c.path, err) c.opts.Logger.Errorf(c.opts.Context, "consul path %s save error: %v", c.path, err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return fmt.Errorf("consul path %s save error: %v", c.path, err) return fmt.Errorf("consul path %s save error: %v", c.path, err)
} }
} }
for _, fn := range c.opts.AfterSave { if err := config.DefaultAfterSave(ctx, c); err != nil {
if err := fn(ctx, c); err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul save err: %v", err)
if !c.opts.AllowFail {
return err return err
} }
}
}
return nil return nil
} }

4
go.mod
View File

@@ -1,4 +1,4 @@
module github.com/unistack-org/micro-config-consul/v3 module go.unistack.org/micro-config-consul/v3
go 1.16 go 1.16
@@ -16,5 +16,5 @@ require (
github.com/mitchellh/mapstructure v1.4.2 // indirect github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.7.0 // indirect github.com/stretchr/testify v1.7.0 // indirect
github.com/unistack-org/micro/v3 v3.7.5 go.unistack.org/micro/v3 v3.8.5
) )

8
go.sum
View File

@@ -162,10 +162,10 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/unistack-org/micro-proto v0.0.9 h1:KrWLS4FUX7UAWNAilQf70uad6ZPf/0EudeddCXllRVc= go.unistack.org/micro-proto/v3 v3.1.0 h1:q39FwjFiRZn+Ux/tt+d3bJTmDtsQQWa+3SLYVo1vLfA=
github.com/unistack-org/micro-proto v0.0.9/go.mod h1:Cckwmzd89gvS7ThxzZp9kQR/EOdksFQcsTAtDDyKwrg= go.unistack.org/micro-proto/v3 v3.1.0/go.mod h1:DpRhYCBXlmSJ/AAXTmntvlh7kQkYU6eFvlmYAx4BQS8=
github.com/unistack-org/micro/v3 v3.7.5 h1:ucNsxi6mApYRpYHbiNKqTLY3V8oGQDfnHn+AbtnFBdo= go.unistack.org/micro/v3 v3.8.5 h1:DIYWRsQF+NPhKZP45sCtNsUhaRw6u2+Ps7U+pKU7i3s=
github.com/unistack-org/micro/v3 v3.7.5/go.mod h1:Ke/8WJlNZi4ZYwL9HcsANAbQ66/HocTBEZM+od99/mM= go.unistack.org/micro/v3 v3.8.5/go.mod h1:KMMmOmbgo/D52/rCAbqeKbBsgEEbSKM69he54J3ZIuA=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

View File

@@ -9,7 +9,7 @@ import (
"os" "os"
hclog "github.com/hashicorp/go-hclog" hclog "github.com/hashicorp/go-hclog"
"github.com/unistack-org/micro/v3/logger" "go.unistack.org/micro/v3/logger"
) )
// to check implementation // to check implementation

View File

@@ -4,7 +4,7 @@ import (
"time" "time"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/unistack-org/micro/v3/config" "go.unistack.org/micro/v3/config"
) )
type configKey struct{} type configKey struct{}

View File

@@ -5,9 +5,9 @@ import (
"reflect" "reflect"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/unistack-org/micro/v3/config" "go.unistack.org/micro/v3/config"
"github.com/unistack-org/micro/v3/util/jitter" "go.unistack.org/micro/v3/util/jitter"
rutil "github.com/unistack-org/micro/v3/util/reflect" rutil "go.unistack.org/micro/v3/util/reflect"
) )
type consulWatcher struct { type consulWatcher struct {