Skip to content

Commit

Permalink
Remove boolean argument and add method to create optional file
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasss committed Aug 27, 2019
1 parent fc3f62e commit 36e84cd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
32 changes: 18 additions & 14 deletions backend/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:]
Expand All @@ -42,7 +46,7 @@ func NewBackend(path string, optional bool) *Backend {
return &Backend{
path: path,
name: name,
optional: optional,
optional: true,
}
}

Expand All @@ -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)
}
Expand Down
13 changes: 6 additions & 7 deletions backend/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"
"time"

"github.com/heetch/confita/backend"
"github.com/heetch/confita/backend/file"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -90,27 +91,25 @@ 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)
})

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)
})

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())
})
}
5 changes: 1 addition & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -218,7 +216,6 @@ func (l *Loader) resolve(ctx context.Context, s *StructConfig) error {
if err == backend.ErrNotFound {
continue
}

return err
}

Expand Down

0 comments on commit 36e84cd

Please sign in to comment.