2020-12-11 02:11:49 +03:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
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"
|
2020-12-11 02:11:49 +03:00
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
|
|
|
"github.com/unistack-org/micro/v3/config"
|
2021-01-19 03:16:37 +03:00
|
|
|
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
2020-12-11 02:11:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
DefaultStructTag = "file"
|
|
|
|
ErrPathNotExist = errors.New("path is not exist")
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&c.opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
path := ""
|
|
|
|
|
|
|
|
if c.opts.Context != nil {
|
|
|
|
if v, ok := c.opts.Context.Value(pathKey{}).(string); ok {
|
|
|
|
path = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 13:37:12 +03:00
|
|
|
if path == "" {
|
|
|
|
return ErrPathNotExist
|
|
|
|
}
|
|
|
|
|
2020-12-11 02:11:49 +03:00
|
|
|
c.path = path
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fileConfig) Load(ctx context.Context) error {
|
2020-12-18 03:41:42 +03:00
|
|
|
for _, fn := range c.opts.BeforeLoad {
|
2020-12-19 23:31:14 +03:00
|
|
|
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
2020-12-18 03:41:42 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 02:11:49 +03:00
|
|
|
fp, err := os.OpenFile(c.path, os.O_RDONLY, os.FileMode(0400))
|
2020-12-19 23:31:14 +03:00
|
|
|
if err != nil && !c.opts.AllowFail {
|
2020-12-20 23:04:26 +03:00
|
|
|
return fmt.Errorf("failed to open: %s, error: %w", c.path, ErrPathNotExist)
|
2020-12-19 23:31:14 +03:00
|
|
|
} else if err == nil {
|
|
|
|
defer fp.Close()
|
|
|
|
var buf []byte
|
|
|
|
buf, err = ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize)))
|
|
|
|
if err == nil {
|
2021-01-19 03:16:37 +03:00
|
|
|
src, err := rutil.Zero(c.opts.Struct)
|
2020-12-20 00:36:07 +03:00
|
|
|
if err == nil {
|
|
|
|
err = c.opts.Codec.Unmarshal(buf, src)
|
|
|
|
if err == nil {
|
|
|
|
err = mergo.Merge(c.opts.Struct, src, mergo.WithOverride, mergo.WithTypeCheck, mergo.WithAppendSlice)
|
|
|
|
}
|
|
|
|
}
|
2020-12-19 23:31:14 +03:00
|
|
|
}
|
|
|
|
if err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 03:41:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, fn := range c.opts.AfterLoad {
|
2020-12-19 23:31:14 +03:00
|
|
|
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
2020-12-18 03:41:42 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-12-11 02:11:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fileConfig) Save(ctx context.Context) error {
|
2020-12-18 03:41:42 +03:00
|
|
|
for _, fn := range c.opts.BeforeSave {
|
2020-12-19 23:31:14 +03:00
|
|
|
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
2020-12-18 03:41:42 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fn := range c.opts.AfterSave {
|
2020-12-19 23:31:14 +03:00
|
|
|
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
2020-12-18 03:41:42 +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
|
|
|
|
}
|
|
|
|
|
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}
|
|
|
|
}
|