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 (
"context"
@@ -6,8 +6,8 @@ import (
"github.com/hashicorp/consul/api"
"github.com/imdario/mergo"
"github.com/unistack-org/micro/v3/config"
rutil "github.com/unistack-org/micro/v3/util/reflect"
"go.unistack.org/micro/v3/config"
rutil "go.unistack.org/micro/v3/util/reflect"
)
var DefaultStructTag = "consul"
@@ -68,8 +68,11 @@ func (c *consulConfig) Init(opts ...config.Option) error {
}
cli, err := api.NewClient(cfg)
if err != nil && !c.opts.AllowFail {
return err
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul init err: %v", err)
if !c.opts.AllowFail {
return err
}
}
c.cli = cli
@@ -79,38 +82,23 @@ func (c *consulConfig) Init(opts ...config.Option) error {
}
func (c *consulConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
for _, fn := range c.opts.BeforeLoad {
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
}
}
if err := config.DefaultBeforeLoad(ctx, c); err != nil {
return err
}
pair, _, err := c.cli.KV().Get(c.path, nil)
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul load path %s err: %v", c.path, err)
if !c.opts.AllowFail {
return fmt.Errorf("consul path %s load error: %v", c.path, err)
}
err = fmt.Errorf("consul path %s load error: %w", c.path, err)
} else if pair == nil {
c.opts.Logger.Errorf(c.opts.Context, "consul load path %s not found", c.path)
if !c.opts.AllowFail {
return fmt.Errorf("consul path %s not found", c.path)
}
err = fmt.Errorf("consul path %s load error: not found", c.path)
}
if err != nil || pair == nil {
for _, fn := range c.opts.AfterLoad {
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
}
}
if err != nil {
c.opts.Logger.Error(c.opts.Context, err)
if !c.opts.AllowFail {
return err
}
return nil
return config.DefaultAfterLoad(ctx, c)
}
options := config.NewLoadOptions(opts...)
@@ -142,26 +130,16 @@ func (c *consulConfig) Load(ctx context.Context, opts ...config.LoadOption) erro
}
}
for _, fn := range c.opts.AfterLoad {
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
}
}
if err := config.DefaultAfterLoad(ctx, c); err != nil {
return err
}
return nil
}
func (c *consulConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
for _, fn := range c.opts.BeforeSave {
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
}
}
if err := config.DefaultBeforeSave(ctx, c); err != nil {
return err
}
buf, err := c.opts.Codec.Marshal(c.opts.Struct)
@@ -170,19 +148,14 @@ func (c *consulConfig) Save(ctx context.Context, opts ...config.SaveOption) erro
}
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 {
return fmt.Errorf("consul path %s save error: %v", c.path, err)
}
}
for _, fn := range c.opts.AfterSave {
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
}
}
if err := config.DefaultAfterSave(ctx, c); err != nil {
return err
}
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
@@ -16,5 +16,5 @@ require (
github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/pkg/errors v0.9.1 // 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/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
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=
github.com/unistack-org/micro-proto v0.0.9/go.mod h1:Cckwmzd89gvS7ThxzZp9kQR/EOdksFQcsTAtDDyKwrg=
github.com/unistack-org/micro/v3 v3.7.5 h1:ucNsxi6mApYRpYHbiNKqTLY3V8oGQDfnHn+AbtnFBdo=
github.com/unistack-org/micro/v3 v3.7.5/go.mod h1:Ke/8WJlNZi4ZYwL9HcsANAbQ66/HocTBEZM+od99/mM=
go.unistack.org/micro-proto/v3 v3.1.0 h1:q39FwjFiRZn+Ux/tt+d3bJTmDtsQQWa+3SLYVo1vLfA=
go.unistack.org/micro-proto/v3 v3.1.0/go.mod h1:DpRhYCBXlmSJ/AAXTmntvlh7kQkYU6eFvlmYAx4BQS8=
go.unistack.org/micro/v3 v3.8.5 h1:DIYWRsQF+NPhKZP45sCtNsUhaRw6u2+Ps7U+pKU7i3s=
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-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

View File

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

View File

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

View File

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