Skip to content

Commit

Permalink
Merge pull request #59 from heetch/add-optional-parameter
Browse files Browse the repository at this point in the history
Add optional parameter for file backend
  • Loading branch information
Yasss authored Aug 27, 2019
2 parents 20131ab + 93b83c5 commit 31dcf3b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
25 changes: 23 additions & 2 deletions backend/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import (
"path/filepath"

"github.com/BurntSushi/toml"
"github.com/heetch/confita/backend"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)

// Backend that loads a configuration from a file.
// It supports json and yaml formats.
type Backend struct {
path string
name string
path string
name string
optional bool
}

// 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) *Backend {
name := filepath.Ext(path)
if name != "" {
Expand All @@ -32,11 +35,29 @@ func NewBackend(path string) *Backend {
}
}

// 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:]
}

return &Backend{
path: path,
name: name,
optional: true,
}
}

// Unmarshal takes a struct pointer and unmarshals the file into it,
// using either json or yaml based on the file extention.
func (b *Backend) Unmarshal(ctx context.Context, to interface{}) error {
f, err := os.Open(b.path)
if err != nil {
if b.optional {
return backend.ErrNotFound
}
return errors.Wrapf(err, "failed to open file at path \"%s\"", b.path)
}
defer f.Close()
Expand Down
11 changes: 10 additions & 1 deletion 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 @@ -96,11 +97,19 @@ timeout = 10
require.Error(t, err)
})

t.Run("File not found", func(t *testing.T) {
t.Run("Required file not found", func(t *testing.T) {
var c config
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.NewOptionalBackend("some path")

err := b.Unmarshal(context.Background(), &c)
require.EqualError(t, err, backend.ErrNotFound.Error())
})
}
4 changes: 3 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ 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 err == backend.ErrNotFound {
continue
}
return err
}

Expand Down Expand Up @@ -201,7 +204,6 @@ func (l *Loader) resolve(ctx context.Context, s *StructConfig) error {
if err == backend.ErrNotFound {
continue
}

return err
}

Expand Down

0 comments on commit 31dcf3b

Please sign in to comment.