Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
8762cb5a71 | |||
ddc561f85f |
30
file.go
30
file.go
@@ -2,7 +2,6 @@ package file
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@@ -16,7 +15,6 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
DefaultStructTag = "file"
|
DefaultStructTag = "file"
|
||||||
ErrPathNotExist = errors.New("path is not exist")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type fileConfig struct {
|
type fileConfig struct {
|
||||||
@@ -33,24 +31,20 @@ func (c *fileConfig) Init(opts ...config.Option) error {
|
|||||||
o(&c.opts)
|
o(&c.opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
path := ""
|
|
||||||
|
|
||||||
if c.opts.Context != nil {
|
if c.opts.Context != nil {
|
||||||
if v, ok := c.opts.Context.Value(pathKey{}).(string); ok {
|
if v, ok := c.opts.Context.Value(pathKey{}).(string); ok {
|
||||||
path = v
|
c.path = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if path == "" {
|
if c.path == "" {
|
||||||
return ErrPathNotExist
|
return fmt.Errorf("file path not exists: %v", c.path)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.path = path
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fileConfig) Load(ctx context.Context) error {
|
func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
|
||||||
for _, fn := range c.opts.BeforeLoad {
|
for _, fn := range c.opts.BeforeLoad {
|
||||||
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
@@ -59,7 +53,7 @@ func (c *fileConfig) Load(ctx context.Context) error {
|
|||||||
|
|
||||||
fp, err := os.OpenFile(c.path, os.O_RDONLY, os.FileMode(0400))
|
fp, err := os.OpenFile(c.path, os.O_RDONLY, os.FileMode(0400))
|
||||||
if err != nil && !c.opts.AllowFail {
|
if err != nil && !c.opts.AllowFail {
|
||||||
return fmt.Errorf("failed to open: %s, error: %w", c.path, ErrPathNotExist)
|
return fmt.Errorf("failed to open: %s, error: %w", c.path, err)
|
||||||
} else if err == nil {
|
} else if err == nil {
|
||||||
defer fp.Close()
|
defer fp.Close()
|
||||||
var buf []byte
|
var buf []byte
|
||||||
@@ -69,7 +63,15 @@ func (c *fileConfig) Load(ctx context.Context) error {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
err = c.opts.Codec.Unmarshal(buf, src)
|
err = c.opts.Codec.Unmarshal(buf, src)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = mergo.Merge(c.opts.Struct, src, mergo.WithOverride, mergo.WithTypeCheck, mergo.WithAppendSlice)
|
options := config.NewLoadOptions(opts...)
|
||||||
|
mopts := []func(*mergo.Config){mergo.WithTypeCheck}
|
||||||
|
if options.Override {
|
||||||
|
mopts = append(mopts, mergo.WithOverride)
|
||||||
|
}
|
||||||
|
if options.Append {
|
||||||
|
mopts = append(mopts, mergo.WithAppendSlice)
|
||||||
|
}
|
||||||
|
err = mergo.Merge(c.opts.Struct, src, mopts...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -87,7 +89,7 @@ func (c *fileConfig) Load(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fileConfig) Save(ctx context.Context) error {
|
func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
|
||||||
for _, fn := range c.opts.BeforeSave {
|
for _, fn := range c.opts.BeforeSave {
|
||||||
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
@@ -101,7 +103,7 @@ func (c *fileConfig) Save(ctx context.Context) error {
|
|||||||
if err != nil && c.opts.AllowFail {
|
if err != nil && c.opts.AllowFail {
|
||||||
return nil
|
return nil
|
||||||
} else if err != nil && !c.opts.AllowFail {
|
} else if err != nil && !c.opts.AllowFail {
|
||||||
return fmt.Errorf("failed to open: %s, error: %w", c.path, ErrPathNotExist)
|
return fmt.Errorf("failed to open: %s, error: %w", c.path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, werr := fp.Write(buf); werr == nil {
|
if _, werr := fp.Write(buf); werr == nil {
|
||||||
|
2
go.mod
2
go.mod
@@ -4,5 +4,5 @@ go 1.16
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/imdario/mergo v0.3.12
|
github.com/imdario/mergo v0.3.12
|
||||||
github.com/unistack-org/micro/v3 v3.3.17
|
github.com/unistack-org/micro/v3 v3.4.0
|
||||||
)
|
)
|
||||||
|
6
go.sum
6
go.sum
@@ -5,9 +5,9 @@ github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
|
|||||||
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||||
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
||||||
github.com/unistack-org/micro/v3 v3.3.17 h1:WcyS7InP0DlS/JpRQGLh5sG6VstkdHJbgpMp+gmHmwg=
|
github.com/unistack-org/micro/v3 v3.4.0 h1:z9F3lgAb2j4cZ1ib5qBj66JPYUAzR4sNIJqUDjVwyVQ=
|
||||||
github.com/unistack-org/micro/v3 v3.3.17/go.mod h1:022EOEZZ789hZY3yB5ZSMXU6jLiadBgcNB/cpediV3c=
|
github.com/unistack-org/micro/v3 v3.4.0/go.mod h1:LXmPfbJnJNvL0kQs8HfnkV3Wya2Wb+C7keVq++RCZnk=
|
||||||
golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
|
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
Reference in New Issue
Block a user