From 36e84cdde91475c328a49c799352760bf73f8f26 Mon Sep 17 00:00:00 2001 From: Yasss Date: Thu, 9 May 2019 17:35:04 +0200 Subject: [PATCH] Remove boolean argument and add method to create optional file --- backend/file/file.go | 32 ++++++++++++++++++-------------- backend/file/file_test.go | 13 ++++++------- config.go | 5 +---- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/backend/file/file.go b/backend/file/file.go index dc1d988..18592ae 100644 --- a/backend/file/file.go +++ b/backend/file/file.go @@ -3,25 +3,15 @@ package file import ( "context" "encoding/json" - "fmt" "os" "path/filepath" "github.com/BurntSushi/toml" + "github.com/heetch/confita/backend" "github.com/pkg/errors" "gopkg.in/yaml.v2" ) -// ErrOpenOptionalFile is returned when opening an optional file returns an error. -type ErrOpenOptionalFile struct { - path string - err error -} - -func (e *ErrOpenOptionalFile) Error() string { - return fmt.Sprintf("failed to open optional file at path \"%s\": %s", e.path, e.err.Error()) -} - // Backend that loads a configuration from a file. // It supports json and yaml formats. type Backend struct { @@ -33,7 +23,21 @@ type Backend struct { // NewBackend creates a configuration loader that loads from a file. // The content will get decoded based on the file extension. // If optional parameter is set to true, calling Unmarshal won't return an error if the file doesn't exist. -func NewBackend(path string, optional bool) *Backend { +func NewBackend(path string) *Backend { + name := filepath.Ext(path) + if name != "" { + name = name[1:] + } + + return &Backend{ + path: path, + name: name, + } +} + +// NewOptionalBackend implementation is exactly the same as NewBackend except that +// if the file is not found, backend.ErrNotFound will be returned. +func NewOptionalBackend(path string) *Backend { name := filepath.Ext(path) if name != "" { name = name[1:] @@ -42,7 +46,7 @@ func NewBackend(path string, optional bool) *Backend { return &Backend{ path: path, name: name, - optional: optional, + optional: true, } } @@ -52,7 +56,7 @@ func (b *Backend) Unmarshal(ctx context.Context, to interface{}) error { f, err := os.Open(b.path) if err != nil { if b.optional { - return &ErrOpenOptionalFile{path: b.path, err: err} + return backend.ErrNotFound } return errors.Wrapf(err, "failed to open file at path \"%s\"", b.path) } diff --git a/backend/file/file_test.go b/backend/file/file_test.go index cc2fddf..9ad8b7f 100644 --- a/backend/file/file_test.go +++ b/backend/file/file_test.go @@ -9,6 +9,7 @@ import ( "testing" "time" + "github.com/heetch/confita/backend" "github.com/heetch/confita/backend/file" "github.com/stretchr/testify/require" ) @@ -41,7 +42,7 @@ func TestFileBackend(t *testing.T) { testLoad := func(t *testing.T, path string) { var c config - b := file.NewBackend(path, false) + b := file.NewBackend(path) err := b.Unmarshal(context.Background(), &c) require.NoError(t, err) @@ -90,7 +91,7 @@ timeout = 10 defer cleanup() var c config - b := file.NewBackend(path, false) + b := file.NewBackend(path) err := b.Unmarshal(context.Background(), &c) require.Error(t, err) @@ -98,7 +99,7 @@ timeout = 10 t.Run("Required file not found", func(t *testing.T) { var c config - b := file.NewBackend("some path", false) + b := file.NewBackend("some path") err := b.Unmarshal(context.Background(), &c) require.Error(t, err) @@ -106,11 +107,9 @@ timeout = 10 t.Run("Optional file not found", func(t *testing.T) { var c config - b := file.NewBackend("some path", true) + b := file.NewOptionalBackend("some path") err := b.Unmarshal(context.Background(), &c) - require.Error(t, err) - _, ok := err.(*file.ErrOpenOptionalFile) - require.True(t, ok) + require.EqualError(t, err, backend.ErrNotFound.Error()) }) } diff --git a/config.go b/config.go index ec819f7..1cc1727 100644 --- a/config.go +++ b/config.go @@ -14,7 +14,6 @@ import ( "github.com/heetch/confita/backend" "github.com/heetch/confita/backend/env" - "github.com/heetch/confita/backend/file" ) // Loader loads configuration keys from backends and stores them is a struct. @@ -185,8 +184,7 @@ func (l *Loader) resolve(ctx context.Context, s *StructConfig) error { if u, ok := b.(Unmarshaler); ok { err := u.Unmarshal(ctx, s.S) if err != nil { - if uerr, ok := err.(*file.ErrOpenOptionalFile); ok { - l.log.Println(uerr.Error()) + if err == backend.ErrNotFound { continue } return err @@ -218,7 +216,6 @@ func (l *Loader) resolve(ctx context.Context, s *StructConfig) error { if err == backend.ErrNotFound { continue } - return err }