diff --git a/file.go b/file.go index b051cc1..464eab3 100644 --- a/file.go +++ b/file.go @@ -51,9 +51,17 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error return err } - fp, err := os.OpenFile(c.path, os.O_RDONLY, os.FileMode(0400)) + 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)) if err != nil { - c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", c.path, err) + c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", path, err) if !c.opts.AllowFail { return err } @@ -64,7 +72,7 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error buf, err := ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize))) if err != nil { - c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", c.path, err) + c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", path, err) if !c.opts.AllowFail { return err } @@ -88,7 +96,7 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error } if err != nil { - c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", c.path, err) + c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", path, err) if !c.opts.AllowFail { return err } @@ -106,18 +114,26 @@ func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error return err } + path := c.path + options := config.NewSaveOptions(opts...) + if options.Context != nil { + if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" { + path = v + } + } + buf, err := c.opts.Codec.Marshal(c.opts.Struct) if err != nil { - c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", c.path, err) + c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", path, err) if !c.opts.AllowFail { return err } return config.DefaultAfterSave(ctx, c) } - fp, err := os.OpenFile(c.path, os.O_WRONLY|os.O_CREATE, os.FileMode(0600)) + fp, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0600)) if err != nil { - c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", c.path, err) + c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", path, err) if !c.opts.AllowFail { return err } @@ -130,7 +146,7 @@ func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error } if err != nil { - c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", c.path, err) + c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", path, err) if !c.opts.AllowFail { return err } @@ -152,8 +168,16 @@ func (c *fileConfig) Name() string { } func (c *fileConfig) Watch(ctx context.Context, opts ...config.WatchOption) (config.Watcher, error) { + path := c.path + options := config.NewWatchOptions(opts...) + if options.Context != nil { + if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" { + path = v + } + } + w := &fileWatcher{ - path: c.path, + path: path, opts: c.opts, wopts: config.NewWatchOptions(opts...), done: make(chan struct{}), diff --git a/go.mod b/go.mod index 0ab601f..1bf9c2f 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.16 require ( github.com/imdario/mergo v0.3.12 - go.unistack.org/micro/v3 v3.8.7 + go.unistack.org/micro/v3 v3.8.9 ) diff --git a/go.sum b/go.sum index 195a70b..9f397dc 100644 --- a/go.sum +++ b/go.sum @@ -9,8 +9,8 @@ github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTK github.com/silas/dag v0.0.0-20210626123444-3804bac2d6d4/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I= go.unistack.org/micro-proto/v3 v3.1.0 h1:q39FwjFiRZn+Ux/tt+d3bJTmDtsQQWa+3SLYVo1vLfA= go.unistack.org/micro-proto/v3 v3.1.0/go.mod h1:DpRhYCBXlmSJ/AAXTmntvlh7kQkYU6eFvlmYAx4BQS8= -go.unistack.org/micro/v3 v3.8.7 h1:k1zOpJ3uS8MxdhK8annRsa5J/LW7MpqPjwYuekW61wE= -go.unistack.org/micro/v3 v3.8.7/go.mod h1:KMMmOmbgo/D52/rCAbqeKbBsgEEbSKM69he54J3ZIuA= +go.unistack.org/micro/v3 v3.8.9 h1:F+HAQSHI86F8Xx5D6XKvvuN0TtOuVvJM+OBV8aYTg0Y= +go.unistack.org/micro/v3 v3.8.9/go.mod h1:KMMmOmbgo/D52/rCAbqeKbBsgEEbSKM69he54J3ZIuA= golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= 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= diff --git a/options.go b/options.go index 8914d01..41ba592 100644 --- a/options.go +++ b/options.go @@ -9,3 +9,15 @@ type pathKey struct{} func Path(path string) config.Option { return config.SetOption(pathKey{}, path) } + +func LoadPath(path string) config.LoadOption { + return config.SetLoadOption(pathKey{}, path) +} + +func SavePath(path string) config.SaveOption { + return config.SetSaveOption(pathKey{}, path) +} + +func WatchPath(path string) config.WatchOption { + return config.SetWatchOption(pathKey{}, path) +}