2024-03-06 17:34:29 +03:00
|
|
|
package file
|
2020-12-11 02:11:49 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-20 23:04:26 +03:00
|
|
|
"fmt"
|
2020-12-11 02:11:49 +03:00
|
|
|
"io"
|
|
|
|
"os"
|
2024-04-17 14:57:30 +03:00
|
|
|
"regexp"
|
2020-12-11 02:11:49 +03:00
|
|
|
|
2024-03-06 17:34:29 +03:00
|
|
|
"dario.cat/mergo"
|
2023-05-05 19:15:16 +03:00
|
|
|
"go.unistack.org/micro/v4/codec"
|
|
|
|
"go.unistack.org/micro/v4/config"
|
2023-08-14 23:50:12 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
2023-05-05 19:15:16 +03:00
|
|
|
rutil "go.unistack.org/micro/v4/util/reflect"
|
2024-04-17 14:57:30 +03:00
|
|
|
"golang.org/x/text/transform"
|
2020-12-11 02:11:49 +03:00
|
|
|
)
|
|
|
|
|
2021-09-30 01:51:18 +03:00
|
|
|
var DefaultStructTag = "file"
|
2020-12-11 02:11:49 +03:00
|
|
|
|
|
|
|
type fileConfig struct {
|
2024-04-17 14:57:30 +03:00
|
|
|
opts config.Options
|
|
|
|
path string
|
|
|
|
reader io.Reader
|
|
|
|
transformer transform.Transformer
|
2020-12-11 02:11:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fileConfig) Options() config.Options {
|
|
|
|
return c.opts
|
|
|
|
}
|
|
|
|
|
2023-08-14 23:50:12 +03:00
|
|
|
func (c *fileConfig) Init(opts ...options.Option) error {
|
2024-04-17 14:57:30 +03:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
2023-05-05 19:15:16 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-11 02:11:49 +03:00
|
|
|
for _, o := range opts {
|
2024-04-17 14:57:30 +03:00
|
|
|
if err = o(&c.opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-11 02:11:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.opts.Context != nil {
|
|
|
|
if v, ok := c.opts.Context.Value(pathKey{}).(string); ok {
|
2021-07-29 16:35:32 +03:00
|
|
|
c.path = v
|
2020-12-11 02:11:49 +03:00
|
|
|
}
|
2024-04-17 14:57:30 +03:00
|
|
|
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
|
|
|
|
}
|
2020-12-11 02:11:49 +03:00
|
|
|
}
|
|
|
|
|
2024-04-17 14:57:30 +03:00
|
|
|
if c.opts.Codec == nil {
|
|
|
|
return fmt.Errorf("Codec must be specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.path == "" && c.reader == nil {
|
|
|
|
err := fmt.Errorf("Path or Reader must be specified")
|
2021-09-30 01:51:18 +03:00
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-13 13:37:12 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 19:15:16 +03:00
|
|
|
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-11 02:11:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-14 23:50:12 +03:00
|
|
|
func (c *fileConfig) Load(ctx context.Context, opts ...options.Option) error {
|
2024-01-15 01:59:42 +03:00
|
|
|
if c.opts.SkipLoad != nil && c.opts.SkipLoad(ctx, c) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 19:15:16 +03:00
|
|
|
if err := config.DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
2021-09-30 01:51:18 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-18 16:12:12 +03:00
|
|
|
path := c.path
|
|
|
|
options := config.NewLoadOptions(opts...)
|
|
|
|
if options.Context != nil {
|
|
|
|
if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" {
|
|
|
|
path = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-17 14:57:30 +03:00
|
|
|
var fp io.Reader
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if c.path != "" {
|
|
|
|
fp, err = os.OpenFile(path, os.O_RDONLY, os.FileMode(0o400))
|
|
|
|
} else if c.reader != nil {
|
|
|
|
fp = c.reader
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("Path or Reader must be specified")
|
|
|
|
}
|
|
|
|
|
2021-09-30 01:51:18 +03:00
|
|
|
if err != nil {
|
|
|
|
if !c.opts.AllowFail {
|
2024-04-17 14:57:30 +03:00
|
|
|
if c.path != "" {
|
|
|
|
return fmt.Errorf("file load path %s error: %w", path, err)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("file load error: %w", err)
|
|
|
|
}
|
2023-05-05 19:15:16 +03:00
|
|
|
}
|
|
|
|
if err = config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
2020-12-18 03:41:42 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-05-05 19:15:16 +03:00
|
|
|
|
|
|
|
return nil
|
2020-12-18 03:41:42 +03:00
|
|
|
}
|
|
|
|
|
2024-04-17 14:57:30 +03:00
|
|
|
if fpc, ok := fp.(io.Closer); ok {
|
|
|
|
defer fpc.Close()
|
|
|
|
}
|
2021-09-30 01:51:18 +03:00
|
|
|
|
2024-04-17 14:57:30 +03:00
|
|
|
var r io.Reader
|
|
|
|
if c.transformer != nil {
|
|
|
|
r = transform.NewReader(fp, c.transformer)
|
|
|
|
} else {
|
|
|
|
r = fp
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := io.ReadAll(io.LimitReader(r, int64(codec.DefaultMaxMsgSize)))
|
2021-09-30 01:51:18 +03:00
|
|
|
if err != nil {
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-05 19:15:16 +03:00
|
|
|
if err = config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dst := c.opts.Struct
|
|
|
|
if options.Struct != nil {
|
|
|
|
dst = options.Struct
|
2021-09-30 01:51:18 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 19:15:16 +03:00
|
|
|
src, err := rutil.Zero(dst)
|
2021-09-30 01:51:18 +03:00
|
|
|
if err == nil {
|
|
|
|
err = c.opts.Codec.Unmarshal(buf, src)
|
2020-12-19 23:31:14 +03:00
|
|
|
if err == nil {
|
2021-09-30 01:51:18 +03:00
|
|
|
options := config.NewLoadOptions(opts...)
|
|
|
|
mopts := []func(*mergo.Config){mergo.WithTypeCheck}
|
|
|
|
if options.Override {
|
|
|
|
mopts = append(mopts, mergo.WithOverride)
|
2020-12-20 00:36:07 +03:00
|
|
|
}
|
2021-09-30 01:51:18 +03:00
|
|
|
if options.Append {
|
|
|
|
mopts = append(mopts, mergo.WithAppendSlice)
|
|
|
|
}
|
2023-05-05 19:15:16 +03:00
|
|
|
err = mergo.Merge(dst, src, mopts...)
|
2020-12-19 23:31:14 +03:00
|
|
|
}
|
2020-12-18 03:41:42 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 19:15:16 +03:00
|
|
|
if err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
2020-12-18 03:41:42 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 19:15:16 +03:00
|
|
|
if err := config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
2021-09-30 01:51:18 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-18 03:41:42 +03:00
|
|
|
return nil
|
2020-12-11 02:11:49 +03:00
|
|
|
}
|
|
|
|
|
2023-08-14 23:50:12 +03:00
|
|
|
func (c *fileConfig) Save(ctx context.Context, opts ...options.Option) error {
|
2024-01-15 01:59:42 +03:00
|
|
|
if c.opts.SkipSave != nil && c.opts.SkipSave(ctx, c) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 19:15:16 +03:00
|
|
|
if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail {
|
2021-09-30 01:51:18 +03:00
|
|
|
return err
|
2020-12-18 03:41:42 +03:00
|
|
|
}
|
|
|
|
|
2021-11-18 16:12:12 +03:00
|
|
|
path := c.path
|
|
|
|
options := config.NewSaveOptions(opts...)
|
|
|
|
if options.Context != nil {
|
|
|
|
if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" {
|
|
|
|
path = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 19:15:16 +03:00
|
|
|
dst := c.opts.Struct
|
|
|
|
if options.Struct != nil {
|
|
|
|
dst = options.Struct
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := c.opts.Codec.Marshal(dst)
|
2021-09-30 01:51:18 +03:00
|
|
|
if err != nil {
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
2021-05-08 19:45:14 +03:00
|
|
|
}
|
2023-05-05 19:15:16 +03:00
|
|
|
if err = config.DefaultAfterSave(ctx, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-09-30 01:51:18 +03:00
|
|
|
}
|
2021-05-08 19:45:14 +03:00
|
|
|
|
2024-03-06 17:34:29 +03:00
|
|
|
fp, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0o600))
|
2021-09-30 01:51:18 +03:00
|
|
|
if err != nil {
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
2021-05-08 19:45:14 +03:00
|
|
|
}
|
2023-05-05 19:15:16 +03:00
|
|
|
if err = config.DefaultAfterSave(ctx, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-05-08 19:45:14 +03:00
|
|
|
}
|
2021-09-30 01:51:18 +03:00
|
|
|
defer fp.Close()
|
2021-05-08 19:45:14 +03:00
|
|
|
|
2021-09-30 01:51:18 +03:00
|
|
|
if _, err = fp.Write(buf); err == nil {
|
|
|
|
err = fp.Close()
|
2021-05-08 19:45:14 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 19:15:16 +03:00
|
|
|
if err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
2020-12-18 03:41:42 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 19:15:16 +03:00
|
|
|
if err := config.DefaultAfterSave(ctx, c); err != nil && !c.opts.AllowFail {
|
2021-09-30 01:51:18 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-11 02:11:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fileConfig) String() string {
|
|
|
|
return "file"
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:24:21 +03:00
|
|
|
func (c *fileConfig) Name() string {
|
|
|
|
return c.opts.Name
|
|
|
|
}
|
|
|
|
|
2023-08-14 23:50:12 +03:00
|
|
|
func (c *fileConfig) Watch(ctx context.Context, opts ...options.Option) (config.Watcher, error) {
|
2021-11-18 16:12:12 +03:00
|
|
|
path := c.path
|
|
|
|
options := config.NewWatchOptions(opts...)
|
|
|
|
if options.Context != nil {
|
|
|
|
if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" {
|
|
|
|
path = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 15:04:18 +03:00
|
|
|
w := &fileWatcher{
|
2021-11-18 16:12:12 +03:00
|
|
|
path: path,
|
2021-08-04 15:04:18 +03:00
|
|
|
opts: c.opts,
|
|
|
|
wopts: config.NewWatchOptions(opts...),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
vchan: make(chan map[string]interface{}),
|
|
|
|
echan: make(chan error),
|
|
|
|
}
|
|
|
|
|
|
|
|
go w.run()
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
2023-08-14 23:50:12 +03:00
|
|
|
func NewConfig(opts ...options.Option) config.Config {
|
2020-12-11 02:11:49 +03:00
|
|
|
options := config.NewOptions(opts...)
|
|
|
|
if len(options.StructTag) == 0 {
|
|
|
|
options.StructTag = DefaultStructTag
|
|
|
|
}
|
|
|
|
return &fileConfig{opts: options}
|
|
|
|
}
|
2024-04-17 14:57:30 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|