display path on error
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
ddc561f85f
commit
8762cb5a71
16
file.go
16
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,20 +31,16 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,7 +53,7 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) 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
|
||||||
@ -109,7 +103,7 @@ func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) 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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user