Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@ package configfile

import (
"io"
"os"
"strconv"
"strings"

"github.com/acoshift/configfile/internal/reader"
)

// NewReader creates new config reader with custom base path
// NewReader creates new config reader
func NewReader(base string) *Reader {
return &Reader{&reader.Dir{Base: base}}
stats, _ := os.Stat(base)
if stats != nil && !stats.IsDir() {
return &Reader{reader.NewYAML(base)}
}
return &Reader{reader.NewDir(base)}
}

// NewDirReader creates new config dir reader
func NewDirReader(base string) *Reader {
return &Reader{reader.NewDir(base)}
}

// NewYAMLReader creates new yaml reader
func NewYAMLReader(filename string) *Reader {
return &Reader{reader.NewYAML(filename)}
}

type intlReader interface {
Expand Down
18 changes: 15 additions & 3 deletions configfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestConfigfile(t *testing.T) {
c := configfile.NewReader("testdata")

func testReader(t *testing.T, c *configfile.Reader) {
t.Run("NotFound", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -256,3 +254,17 @@ func TestConfigfile(t *testing.T) {
})
})
}

func TestDirReader(t *testing.T) {
t.Parallel()

testReader(t, configfile.NewDirReader("testdata"))
testReader(t, configfile.NewReader("testdata"))
}

func TestYAMLReader(t *testing.T) {
t.Parallel()

testReader(t, configfile.NewYAMLReader("testdata/config.yaml"))
testReader(t, configfile.NewReader("testdata/config.yaml"))
}
5 changes: 5 additions & 0 deletions internal/reader/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"path/filepath"
)

// NewDir creates new dir reader
func NewDir(dir string) *Dir {
return &Dir{dir}
}

// Dir reads config from directory
type Dir struct {
Base string
Expand Down
7 changes: 7 additions & 0 deletions internal/reader/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package reader

import "errors"

var (
errNotFound = errors.New("reader: not found")
)
31 changes: 31 additions & 0 deletions internal/reader/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package reader

import (
"os"

yaml "gopkg.in/yaml.v2"
)

// NewYAML creates new yaml reader
func NewYAML(filename string) *YAML {
var r YAML
fs, _ := os.Open(filename)
if fs != nil {
yaml.NewDecoder(fs).Decode(&r.d)
}
return &r
}

// YAML reads config from yaml file
type YAML struct {
d map[string]string
}

// Read reads a config
func (r *YAML) Read(name string) ([]byte, error) {
p, ok := r.d[name]
if !ok {
return nil, errNotFound
}
return []byte(p), nil
}
5 changes: 5 additions & 0 deletions testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data1: "true"
data2: "false"
data3: "9"
data4: "0"
empty: