2021-10-26 23:23:49 +03:00
|
|
|
package file // import "go.unistack.org/micro-config-file/v3"
|
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"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2020-12-20 00:36:07 +03:00
|
|
|
"github.com/imdario/mergo"
|
2021-10-26 23:23:49 +03:00
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
"go.unistack.org/micro/v3/config"
|
|
|
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
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 {
|
|
|
|
opts config.Options
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fileConfig) Options() config.Options {
|
|
|
|
return c.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fileConfig) Init(opts ...config.Option) error {
|
2023-03-15 00:16:13 +03:00
|
|
|
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-11 02:11:49 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&c.opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 16:35:32 +03:00
|
|
|
if c.path == "" {
|
2021-09-30 01:51:18 +03:00
|
|
|
err := fmt.Errorf("file path not exists: %v", c.path)
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-13 13:37:12 +03:00
|
|
|
}
|
|
|
|
|
2023-03-15 00:16:13 +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
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:18:05 +03:00
|
|
|
func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
|
2024-01-15 02:00:34 +03:00
|
|
|
if c.opts.SkipLoad != nil && c.opts.SkipLoad(ctx, c) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-15 00:16:13 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fp, err := os.OpenFile(path, os.O_RDONLY, os.FileMode(0400))
|
2021-09-30 01:51:18 +03:00
|
|
|
if err != nil {
|
|
|
|
if !c.opts.AllowFail {
|
2023-03-15 00:16:13 +03:00
|
|
|
return fmt.Errorf("file load path %s error: %w", path, err)
|
|
|
|
}
|
|
|
|
if err = config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
2020-12-18 03:41:42 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-03-15 00:16:13 +03:00
|
|
|
|
|
|
|
return nil
|
2020-12-18 03:41:42 +03:00
|
|
|
}
|
|
|
|
|
2021-09-30 01:51:18 +03:00
|
|
|
defer fp.Close()
|
|
|
|
|
|
|
|
buf, err := ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize)))
|
|
|
|
if err != nil {
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-15 00:16:13 +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-03-15 00:16:13 +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-03-15 00:16:13 +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-03-15 00:16:13 +03:00
|
|
|
if err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
2020-12-18 03:41:42 +03:00
|
|
|
}
|
|
|
|
|
2023-03-15 00:16:13 +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
|
|
|
}
|
|
|
|
|
2021-06-21 16:18:05 +03:00
|
|
|
func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
|
2024-01-15 02:00:34 +03:00
|
|
|
if c.opts.SkipSave != nil && c.opts.SkipSave(ctx, c) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-15 00:16:13 +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-03-15 00:16:13 +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-03-15 00:16:13 +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
|
|
|
|
2021-11-18 16:12:12 +03:00
|
|
|
fp, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0600))
|
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-03-15 00:16:13 +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-03-15 00:16:13 +03:00
|
|
|
if err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
2020-12-18 03:41:42 +03:00
|
|
|
}
|
|
|
|
|
2023-03-15 00:16:13 +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
|
|
|
|
}
|
|
|
|
|
2021-08-04 15:04:18 +03:00
|
|
|
func (c *fileConfig) Watch(ctx context.Context, opts ...config.WatchOption) (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
|
|
|
|
}
|
|
|
|
|
2020-12-11 02:11:49 +03:00
|
|
|
func NewConfig(opts ...config.Option) config.Config {
|
|
|
|
options := config.NewOptions(opts...)
|
|
|
|
if len(options.StructTag) == 0 {
|
|
|
|
options.StructTag = DefaultStructTag
|
|
|
|
}
|
|
|
|
return &fileConfig{opts: options}
|
|
|
|
}
|