4 Commits

Author SHA1 Message Date
2fa6be8f5a update deps
Some checks failed
build / test (push) Failing after 1m8s
codeql / analyze (go) (push) Failing after 1m53s
build / lint (push) Successful in 9m37s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-06 17:34:29 +03:00
d77095fb4c fixup conditions
Some checks failed
build / test (push) Has been cancelled
build / lint (push) Has been cancelled
codeql / analyze (go) (push) Has been cancelled
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-01-15 01:59:42 +03:00
4dc1cb3e1c Merge pull request 'update for latest micro changes' (#96) from microv4 into master
Some checks failed
build / test (push) Failing after 1m28s
build / lint (push) Failing after 2m32s
codeql / analyze (go) (push) Failing after 3m7s
Reviewed-on: #96
2023-08-14 23:50:39 +03:00
38851a57bf update for latest micro changes
Some checks failed
dependabot-automerge / automerge (pull_request) Has been skipped
prbuild / test (pull_request) Failing after 1m29s
prbuild / lint (pull_request) Failing after 2m37s
autoapprove / autoapprove (pull_request) Failing after 1m25s
automerge / automerge (pull_request) Failing after 5s
codeql / analyze (go) (pull_request) Failing after 3m8s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-14 23:50:12 +03:00
4 changed files with 32 additions and 32 deletions

30
file.go
View File

@@ -1,15 +1,15 @@
package file // import "go.unistack.org/micro-config-file/v4" package file
import ( import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"github.com/imdario/mergo" "dario.cat/mergo"
"go.unistack.org/micro/v4/codec" "go.unistack.org/micro/v4/codec"
"go.unistack.org/micro/v4/config" "go.unistack.org/micro/v4/config"
"go.unistack.org/micro/v4/options"
rutil "go.unistack.org/micro/v4/util/reflect" rutil "go.unistack.org/micro/v4/util/reflect"
) )
@@ -24,7 +24,7 @@ func (c *fileConfig) Options() config.Options {
return c.opts return c.opts
} }
func (c *fileConfig) Init(opts ...config.Option) error { func (c *fileConfig) Init(opts ...options.Option) error {
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail { if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
return err return err
} }
@@ -53,7 +53,11 @@ func (c *fileConfig) Init(opts ...config.Option) error {
return nil return nil
} }
func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error { func (c *fileConfig) Load(ctx context.Context, opts ...options.Option) error {
if c.opts.SkipLoad != nil && c.opts.SkipLoad(ctx, c) {
return nil
}
if err := config.DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail { if err := config.DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail {
return err return err
} }
@@ -66,7 +70,7 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
} }
} }
fp, err := os.OpenFile(path, os.O_RDONLY, os.FileMode(0400)) fp, err := os.OpenFile(path, os.O_RDONLY, os.FileMode(0o400))
if err != nil { if err != nil {
if !c.opts.AllowFail { if !c.opts.AllowFail {
return fmt.Errorf("file load path %s error: %w", path, err) return fmt.Errorf("file load path %s error: %w", path, err)
@@ -80,7 +84,7 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
defer fp.Close() defer fp.Close()
buf, err := ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize))) buf, err := io.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize)))
if err != nil { if err != nil {
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
@@ -124,7 +128,11 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
return nil return nil
} }
func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error { func (c *fileConfig) Save(ctx context.Context, opts ...options.Option) error {
if c.opts.SkipSave != nil && c.opts.SkipSave(ctx, c) {
return nil
}
if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail { if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail {
return err return err
} }
@@ -154,7 +162,7 @@ func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error
return nil return nil
} }
fp, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0600)) fp, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0o600))
if err != nil { if err != nil {
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
@@ -190,7 +198,7 @@ func (c *fileConfig) Name() string {
return c.opts.Name return c.opts.Name
} }
func (c *fileConfig) Watch(ctx context.Context, opts ...config.WatchOption) (config.Watcher, error) { func (c *fileConfig) Watch(ctx context.Context, opts ...options.Option) (config.Watcher, error) {
path := c.path path := c.path
options := config.NewWatchOptions(opts...) options := config.NewWatchOptions(opts...)
if options.Context != nil { if options.Context != nil {
@@ -213,7 +221,7 @@ func (c *fileConfig) Watch(ctx context.Context, opts ...config.WatchOption) (con
return w, nil return w, nil
} }
func NewConfig(opts ...config.Option) config.Config { func NewConfig(opts ...options.Option) config.Config {
options := config.NewOptions(opts...) options := config.NewOptions(opts...)
if len(options.StructTag) == 0 { if len(options.StructTag) == 0 {
options.StructTag = DefaultStructTag options.StructTag = DefaultStructTag

6
go.mod
View File

@@ -3,6 +3,8 @@ module go.unistack.org/micro-config-file/v4
go 1.20 go 1.20
require ( require (
github.com/imdario/mergo v0.3.14 dario.cat/mergo v1.0.0
go.unistack.org/micro/v4 v4.0.1 go.unistack.org/micro/v4 v4.0.17
) )
require github.com/google/uuid v1.6.0 // indirect

10
go.sum
View File

@@ -1,7 +1,9 @@
github.com/imdario/mergo v0.3.14 h1:fOqeC1+nCuuk6PKQdg9YmosXX7Y7mHX6R/0ZldI9iHo= dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
github.com/imdario/mergo v0.3.14/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
go.unistack.org/micro/v4 v4.0.17 h1:mF7uM+J4ILdG+1fcwzKYCwDlxhdbF/e1WnGzKKLnIXc=
go.unistack.org/micro/v4 v4.0.17/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -1,23 +1,11 @@
package file package file
import ( import (
"go.unistack.org/micro/v4/config" "go.unistack.org/micro/v4/options"
) )
type pathKey struct{} type pathKey struct{}
func Path(path string) config.Option { func Path(path string) options.Option {
return config.SetOption(pathKey{}, path) return options.ContextOption(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)
} }