2023-05-05 19:15:16 +03:00
|
|
|
package file // import "go.unistack.org/micro-config-file/v4"
|
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"
|
|
|
|
|
2020-12-20 00:36:07 +03:00
|
|
|
"github.com/imdario/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"
|
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
|
|
|
|
}
|
|
|
|
|
2023-08-14 23:50:12 +03:00
|
|
|
func (c *fileConfig) Init(opts ...options.Option) error {
|
2023-05-05 19:15:16 +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-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 {
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-05 19:15:16 +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-05-05 19:15:16 +03:00
|
|
|
|
|
|
|
return nil
|
2020-12-18 03:41:42 +03:00
|
|
|
}
|
|
|
|
|
2021-09-30 01:51:18 +03:00
|
|
|
defer fp.Close()
|
|
|
|
|
2023-08-14 23:50:12 +03:00
|
|
|
buf, err := io.ReadAll(io.LimitReader(fp, 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 {
|
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
|
|
|
|
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-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}
|
|
|
|
}
|