Compare commits
30 Commits
Author | SHA1 | Date | |
---|---|---|---|
d77095fb4c | |||
4dc1cb3e1c | |||
38851a57bf | |||
1d81cb3c80 | |||
6061155190 | |||
632868864e | |||
a23c45515a | |||
5f74e48ef4 | |||
|
19cb6cf800 | ||
|
c0e0529819 | ||
3f6f91808b | |||
|
efd880b2ea | ||
69dec86aaa | |||
|
f1f442339d | ||
89d4f83671 | |||
|
9a608532a8 | ||
ef412b165f | |||
|
3c7e3ed5b8 | ||
8d35254abe | |||
|
c78878a918 | ||
443bc44da1 | |||
|
9ec934cc15 | ||
ae6cde7614 | |||
|
ba9fa173de | ||
5e4627d676 | |||
|
bc31a3f7db | ||
58a48621f1 | |||
|
8af739f80f | ||
2e91975c6b | |||
|
d0a93944e5 |
167
file.go
167
file.go
@@ -1,35 +1,30 @@
|
|||||||
package file // import "go.unistack.org/micro-config-file/v3"
|
package file // import "go.unistack.org/micro-config-file/v4"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
|
||||||
|
|
||||||
"dario.cat/mergo"
|
"github.com/imdario/mergo"
|
||||||
"go.unistack.org/micro/v3/config"
|
"go.unistack.org/micro/v4/codec"
|
||||||
rutil "go.unistack.org/micro/v3/util/reflect"
|
"go.unistack.org/micro/v4/config"
|
||||||
"golang.org/x/text/transform"
|
"go.unistack.org/micro/v4/options"
|
||||||
|
rutil "go.unistack.org/micro/v4/util/reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var DefaultStructTag = "file"
|
||||||
DefaultStructTag = "file"
|
|
||||||
MaxFileSize int64 = 1 * 1024 * 1024
|
|
||||||
)
|
|
||||||
|
|
||||||
type fileConfig struct {
|
type fileConfig struct {
|
||||||
opts config.Options
|
opts config.Options
|
||||||
path string
|
path string
|
||||||
reader io.Reader
|
|
||||||
transformer transform.Transformer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fileConfig) Options() config.Options {
|
func (c *fileConfig) Options() config.Options {
|
||||||
return c.opts
|
return c.opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fileConfig) Init(opts ...config.Option) error {
|
func (c *fileConfig) Init(opts ...options.Option) error {
|
||||||
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -42,16 +37,13 @@ func (c *fileConfig) Init(opts ...config.Option) error {
|
|||||||
if v, ok := c.opts.Context.Value(pathKey{}).(string); ok {
|
if v, ok := c.opts.Context.Value(pathKey{}).(string); ok {
|
||||||
c.path = v
|
c.path = v
|
||||||
}
|
}
|
||||||
if v, ok := c.opts.Context.Value(transformerKey{}).(transform.Transformer); ok {
|
|
||||||
c.transformer = v
|
|
||||||
}
|
|
||||||
if v, ok := c.opts.Context.Value(readerKey{}).(io.Reader); ok {
|
|
||||||
c.reader = v
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.opts.Codec == nil {
|
if c.path == "" {
|
||||||
return fmt.Errorf("Codec must be specified")
|
err := fmt.Errorf("file path not exists: %v", c.path)
|
||||||
|
if !c.opts.AllowFail {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
||||||
@@ -61,7 +53,7 @@ func (c *fileConfig) Init(opts ...config.Option) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
|
func (c *fileConfig) Load(ctx context.Context, opts ...options.Option) error {
|
||||||
if c.opts.SkipLoad != nil && c.opts.SkipLoad(ctx, c) {
|
if c.opts.SkipLoad != nil && c.opts.SkipLoad(ctx, c) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -71,40 +63,17 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
path := c.path
|
path := c.path
|
||||||
transformer := c.transformer
|
|
||||||
reader := c.reader
|
|
||||||
|
|
||||||
options := config.NewLoadOptions(opts...)
|
options := config.NewLoadOptions(opts...)
|
||||||
if options.Context != nil {
|
if options.Context != nil {
|
||||||
if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" {
|
if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" {
|
||||||
path = v
|
path = v
|
||||||
}
|
}
|
||||||
if v, ok := c.opts.Context.Value(transformerKey{}).(transform.Transformer); ok {
|
|
||||||
transformer = v
|
|
||||||
}
|
|
||||||
if v, ok := c.opts.Context.Value(readerKey{}).(io.Reader); ok {
|
|
||||||
reader = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var fp io.Reader
|
|
||||||
var err error
|
|
||||||
|
|
||||||
if path != "" {
|
|
||||||
fp, err = os.OpenFile(path, os.O_RDONLY, os.FileMode(0o400))
|
|
||||||
} else if c.reader != nil {
|
|
||||||
fp = reader
|
|
||||||
} else {
|
|
||||||
err = fmt.Errorf("Path or Reader must be specified")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fp, err := os.OpenFile(path, os.O_RDONLY, os.FileMode(0400))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !c.opts.AllowFail {
|
if !c.opts.AllowFail {
|
||||||
if c.path != "" {
|
return fmt.Errorf("file load path %s error: %w", path, err)
|
||||||
return fmt.Errorf("file load path %s error: %w", path, err)
|
|
||||||
} else {
|
|
||||||
return fmt.Errorf("file load error: %w", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if err = config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
if err = config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
@@ -113,18 +82,9 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if fpc, ok := fp.(io.Closer); ok {
|
defer fp.Close()
|
||||||
defer fpc.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
var r io.Reader
|
buf, err := io.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize)))
|
||||||
if transformer != nil {
|
|
||||||
r = transform.NewReader(fp, c.transformer)
|
|
||||||
} else {
|
|
||||||
r = fp
|
|
||||||
}
|
|
||||||
|
|
||||||
buf, err := io.ReadAll(io.LimitReader(r, MaxFileSize))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !c.opts.AllowFail {
|
if !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
@@ -168,7 +128,7 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
|
func (c *fileConfig) Save(ctx context.Context, opts ...options.Option) error {
|
||||||
if c.opts.SkipSave != nil && c.opts.SkipSave(ctx, c) {
|
if c.opts.SkipSave != nil && c.opts.SkipSave(ctx, c) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -202,7 +162,7 @@ func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
fp, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0o600))
|
fp, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0600))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !c.opts.AllowFail {
|
if !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
@@ -238,7 +198,7 @@ func (c *fileConfig) Name() string {
|
|||||||
return c.opts.Name
|
return c.opts.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fileConfig) Watch(ctx context.Context, opts ...config.WatchOption) (config.Watcher, error) {
|
func (c *fileConfig) Watch(ctx context.Context, opts ...options.Option) (config.Watcher, error) {
|
||||||
path := c.path
|
path := c.path
|
||||||
options := config.NewWatchOptions(opts...)
|
options := config.NewWatchOptions(opts...)
|
||||||
if options.Context != nil {
|
if options.Context != nil {
|
||||||
@@ -261,89 +221,10 @@ func (c *fileConfig) Watch(ctx context.Context, opts ...config.WatchOption) (con
|
|||||||
return w, nil
|
return w, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig(opts ...config.Option) config.Config {
|
func NewConfig(opts ...options.Option) config.Config {
|
||||||
options := config.NewOptions(opts...)
|
options := config.NewOptions(opts...)
|
||||||
if len(options.StructTag) == 0 {
|
if len(options.StructTag) == 0 {
|
||||||
options.StructTag = DefaultStructTag
|
options.StructTag = DefaultStructTag
|
||||||
}
|
}
|
||||||
return &fileConfig{opts: options}
|
return &fileConfig{opts: options}
|
||||||
}
|
}
|
||||||
|
|
||||||
type EnvTransformer struct {
|
|
||||||
maxMatchSize int
|
|
||||||
Regexp *regexp.Regexp
|
|
||||||
TransformerFunc TransformerFunc
|
|
||||||
overflow []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ transform.Transformer = (*EnvTransformer)(nil)
|
|
||||||
|
|
||||||
// Transform implements golang.org/x/text/transform#Transformer
|
|
||||||
func (t *EnvTransformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
|
|
||||||
t.maxMatchSize = 1024
|
|
||||||
var n int
|
|
||||||
// copy any overflow from the last call
|
|
||||||
if len(t.overflow) > 0 {
|
|
||||||
n, err = fullcopy(dst, t.overflow)
|
|
||||||
nDst += n
|
|
||||||
if err != nil {
|
|
||||||
t.overflow = t.overflow[n:]
|
|
||||||
return
|
|
||||||
}
|
|
||||||
t.overflow = nil
|
|
||||||
}
|
|
||||||
for _, index := range t.Regexp.FindAllSubmatchIndex(src, -1) {
|
|
||||||
// copy everything up to the match
|
|
||||||
n, err = fullcopy(dst[nDst:], src[nSrc:index[0]])
|
|
||||||
nSrc += n
|
|
||||||
nDst += n
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// skip the match if it ends at the end the src buffer.
|
|
||||||
// it could potentially match more
|
|
||||||
if index[1] == len(src) && !atEOF {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
// copy the replacement
|
|
||||||
rep := t.TransformerFunc(src, index)
|
|
||||||
n, err = fullcopy(dst[nDst:], rep)
|
|
||||||
nDst += n
|
|
||||||
nSrc = index[1]
|
|
||||||
if err != nil {
|
|
||||||
t.overflow = rep[n:]
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// if we're at the end, tack on any remaining bytes
|
|
||||||
if atEOF {
|
|
||||||
n, err = fullcopy(dst[nDst:], src[nSrc:])
|
|
||||||
nDst += n
|
|
||||||
nSrc += n
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// skip any bytes which exceede the max match size
|
|
||||||
if skip := len(src[nSrc:]) - t.maxMatchSize; skip > 0 {
|
|
||||||
n, err = fullcopy(dst[nDst:], src[nSrc:nSrc+skip])
|
|
||||||
nSrc += n
|
|
||||||
nDst += n
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err = transform.ErrShortSrc
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset resets the state and allows a Transformer to be reused.
|
|
||||||
func (t *EnvTransformer) Reset() {
|
|
||||||
t.overflow = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func fullcopy(dst, src []byte) (n int, err error) {
|
|
||||||
n = copy(dst, src)
|
|
||||||
if n < len(src) {
|
|
||||||
err = transform.ErrShortDst
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
59
file_test.go
59
file_test.go
@@ -1,59 +0,0 @@
|
|||||||
package file
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/codec"
|
|
||||||
"go.unistack.org/micro/v3/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
type jsoncodec struct{}
|
|
||||||
|
|
||||||
func (*jsoncodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
|
||||||
return json.Marshal(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*jsoncodec) Unmarshal(buf []byte, v interface{}, opts ...codec.Option) error {
|
|
||||||
return json.Unmarshal(buf, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*jsoncodec) String() string {
|
|
||||||
return "json"
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLoadReplace(t *testing.T) {
|
|
||||||
type Config struct {
|
|
||||||
Key string
|
|
||||||
Pass string
|
|
||||||
}
|
|
||||||
os.Setenv("PLACEHOLDER", "test")
|
|
||||||
cfg := &Config{}
|
|
||||||
ctx := context.TODO()
|
|
||||||
buf := bytes.NewReader([]byte(`{"key":"val","pass":"${PLACEHOLDER}"}`))
|
|
||||||
tr, err := NewEnvTransformer(`(?s)\$\{.*?\}`, 2, 1)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
c := NewConfig(config.Codec(
|
|
||||||
&jsoncodec{}),
|
|
||||||
config.Struct(cfg),
|
|
||||||
Reader(buf),
|
|
||||||
Transformer(tr),
|
|
||||||
)
|
|
||||||
|
|
||||||
if err := c.Init(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := c.Load(ctx); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if cfg.Pass != "test" {
|
|
||||||
t.Fatalf("not works %#+v\n", cfg)
|
|
||||||
}
|
|
||||||
}
|
|
17
go.mod
17
go.mod
@@ -1,18 +1,13 @@
|
|||||||
module go.unistack.org/micro-config-file/v3
|
module go.unistack.org/micro-config-file/v4
|
||||||
|
|
||||||
go 1.22
|
go 1.20
|
||||||
|
|
||||||
toolchain go1.23.1
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
dario.cat/mergo v1.0.1
|
github.com/imdario/mergo v0.3.16
|
||||||
go.unistack.org/micro/v3 v3.10.100
|
go.unistack.org/micro/v4 v4.0.15
|
||||||
golang.org/x/text v0.20.0
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
dario.cat/mergo v1.0.0 // indirect
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1 // indirect
|
github.com/google/uuid v1.5.0 // indirect
|
||||||
google.golang.org/protobuf v1.35.2 // indirect
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
|
||||||
)
|
)
|
||||||
|
37
go.sum
37
go.sum
@@ -1,25 +1,18 @@
|
|||||||
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
|
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
|
||||||
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
|
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
|
||||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
|
github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM=
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
|
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
|
||||||
go.unistack.org/micro/v3 v3.10.97 h1:8l7fv+i06/PjPrBBhRC/ZQkWGIOuHPg3jJN0vktYE78=
|
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
|
||||||
go.unistack.org/micro/v3 v3.10.97/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g=
|
go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo=
|
||||||
go.unistack.org/micro/v3 v3.10.100 h1:yWOaU0ImCGm5k5MUzlIobJUOr+KLfrR/BoDZvcHyKxM=
|
go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs=
|
||||||
go.unistack.org/micro/v3 v3.10.100/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g=
|
go.unistack.org/micro/v4 v4.0.6 h1:YFWvTh3VwyOd6NHYTQcf47n2TF5+p/EhpnbuBQX3qhk=
|
||||||
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
go.unistack.org/micro/v4 v4.0.6/go.mod h1:bVEYTlPi0EsdgZZt311bIroDg9ict7ky3C87dSCCAGk=
|
||||||
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
go.unistack.org/micro/v4 v4.0.15 h1:o4++RYX5guVPxfcJpP7VCj2Xr9BPFo6fj4+b5FStdEI=
|
||||||
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
|
go.unistack.org/micro/v4 v4.0.15/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
|
||||||
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
|
|
||||||
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
|
||||||
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
|
|
||||||
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
59
options.go
59
options.go
@@ -1,64 +1,11 @@
|
|||||||
package file
|
package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"go.unistack.org/micro/v4/options"
|
||||||
"os"
|
|
||||||
"regexp"
|
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/config"
|
|
||||||
"golang.org/x/text/transform"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type pathKey struct{}
|
type pathKey struct{}
|
||||||
|
|
||||||
func Path(path string) config.Option {
|
func Path(path string) options.Option {
|
||||||
return config.SetOption(pathKey{}, path)
|
return options.ContextOption(pathKey{}, path)
|
||||||
}
|
|
||||||
|
|
||||||
func LoadPath(path string) config.LoadOption {
|
|
||||||
return config.SetLoadOption(pathKey{}, path)
|
|
||||||
}
|
|
||||||
|
|
||||||
func SavePath(path string) config.SaveOption {
|
|
||||||
return config.SetSaveOption(pathKey{}, path)
|
|
||||||
}
|
|
||||||
|
|
||||||
func WatchPath(path string) config.WatchOption {
|
|
||||||
return config.SetWatchOption(pathKey{}, path)
|
|
||||||
}
|
|
||||||
|
|
||||||
type readerKey struct{}
|
|
||||||
|
|
||||||
func Reader(r io.Reader) config.Option {
|
|
||||||
return config.SetOption(readerKey{}, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
type transformerKey struct{}
|
|
||||||
|
|
||||||
type TransformerFunc func(src []byte, index []int) []byte
|
|
||||||
|
|
||||||
func Transformer(t transform.Transformer) config.Option {
|
|
||||||
return config.SetOption(transformerKey{}, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewEnvTransformer(rs string, trimLeft, trimRight int) (*EnvTransformer, error) {
|
|
||||||
re, err := regexp.Compile(rs)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &EnvTransformer{
|
|
||||||
Regexp: re,
|
|
||||||
TransformerFunc: func(src []byte, index []int) []byte {
|
|
||||||
var envKey string
|
|
||||||
if len(src) > index[1]-trimRight {
|
|
||||||
envKey = string(src[index[0]+trimLeft : index[1]-trimRight])
|
|
||||||
}
|
|
||||||
|
|
||||||
if envVal, ok := os.LookupEnv(envKey); ok {
|
|
||||||
return []byte(envVal)
|
|
||||||
}
|
|
||||||
|
|
||||||
return src[index[0]:index[1]]
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
@@ -7,9 +7,10 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/config"
|
"go.unistack.org/micro/v4/codec"
|
||||||
"go.unistack.org/micro/v3/util/jitter"
|
"go.unistack.org/micro/v4/config"
|
||||||
rutil "go.unistack.org/micro/v3/util/reflect"
|
"go.unistack.org/micro/v4/util/jitter"
|
||||||
|
rutil "go.unistack.org/micro/v4/util/reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fileWatcher struct {
|
type fileWatcher struct {
|
||||||
@@ -43,7 +44,7 @@ func (w *fileWatcher) run() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err = ioutil.ReadAll(io.LimitReader(fp, MaxFileSize))
|
buf, err = ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize)))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = w.opts.Codec.Unmarshal(buf, dst)
|
err = w.opts.Codec.Unmarshal(buf, dst)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user