diff options
| -rw-r--r-- | data/datasource.go | 34 | ||||
| -rw-r--r-- | data/datasource_boltdb.go | 27 | ||||
| -rw-r--r-- | data/datasource_stdin.go | 19 | ||||
| -rw-r--r-- | data/datasource_stdin_test.go | 22 | ||||
| -rw-r--r-- | data/datasource_test.go | 15 |
5 files changed, 68 insertions, 49 deletions
diff --git a/data/datasource.go b/data/datasource.go index 04cf6c8a..2e50258e 100644 --- a/data/datasource.go +++ b/data/datasource.go @@ -3,11 +3,9 @@ package data import ( "fmt" "io" - "io/ioutil" "mime" "net/http" "net/url" - "os" "path/filepath" "strings" @@ -372,35 +370,3 @@ func (d *Data) readSource(source *Source, args ...string) ([]byte, error) { d.cache[cacheKey] = data return data, nil } - -func readStdin(source *Source, args ...string) ([]byte, error) { - if stdin == nil { - stdin = os.Stdin - } - b, err := ioutil.ReadAll(stdin) - if err != nil { - return nil, errors.Wrapf(err, "Can't read %s", stdin) - } - return b, nil -} - -func readBoltDB(source *Source, args ...string) (data []byte, err error) { - if source.kv == nil { - source.kv, err = libkv.NewBoltDB(source.URL) - if err != nil { - return nil, err - } - } - - if len(args) != 1 { - return nil, errors.New("missing key") - } - p := args[0] - - data, err = source.kv.Read(p) - if err != nil { - return nil, err - } - - return data, nil -} diff --git a/data/datasource_boltdb.go b/data/datasource_boltdb.go new file mode 100644 index 00000000..81682e04 --- /dev/null +++ b/data/datasource_boltdb.go @@ -0,0 +1,27 @@ +package data + +import ( + "github.com/hairyhenderson/gomplate/v3/libkv" + "github.com/pkg/errors" +) + +func readBoltDB(source *Source, args ...string) (data []byte, err error) { + if source.kv == nil { + source.kv, err = libkv.NewBoltDB(source.URL) + if err != nil { + return nil, err + } + } + + if len(args) != 1 { + return nil, errors.New("missing key") + } + p := args[0] + + data, err = source.kv.Read(p) + if err != nil { + return nil, err + } + + return data, nil +} diff --git a/data/datasource_stdin.go b/data/datasource_stdin.go new file mode 100644 index 00000000..f23905d2 --- /dev/null +++ b/data/datasource_stdin.go @@ -0,0 +1,19 @@ +package data + +import ( + "io/ioutil" + "os" + + "github.com/pkg/errors" +) + +func readStdin(source *Source, args ...string) ([]byte, error) { + if stdin == nil { + stdin = os.Stdin + } + b, err := ioutil.ReadAll(stdin) + if err != nil { + return nil, errors.Wrapf(err, "Can't read %s", stdin) + } + return b, nil +} diff --git a/data/datasource_stdin_test.go b/data/datasource_stdin_test.go new file mode 100644 index 00000000..7fc02318 --- /dev/null +++ b/data/datasource_stdin_test.go @@ -0,0 +1,22 @@ +package data + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestReadStdin(t *testing.T) { + defer func() { + stdin = nil + }() + stdin = strings.NewReader("foo") + out, err := readStdin(nil) + assert.NoError(t, err) + assert.Equal(t, []byte("foo"), out) + + stdin = errorReader{} + _, err = readStdin(nil) + assert.Error(t, err) +} diff --git a/data/datasource_test.go b/data/datasource_test.go index 62e1770e..83a59496 100644 --- a/data/datasource_test.go +++ b/data/datasource_test.go @@ -5,7 +5,6 @@ import ( "net/http" "net/url" "runtime" - "strings" "testing" "github.com/hairyhenderson/gomplate/v3/internal/config" @@ -173,20 +172,6 @@ func (e errorReader) Read(p []byte) (n int, err error) { return 0, fmt.Errorf("error") } -func TestReadStdin(t *testing.T) { - defer func() { - stdin = nil - }() - stdin = strings.NewReader("foo") - out, err := readStdin(nil) - assert.NoError(t, err) - assert.Equal(t, []byte("foo"), out) - - stdin = errorReader{} - _, err = readStdin(nil) - assert.Error(t, err) -} - // nolint: megacheck func TestDefineDatasource(t *testing.T) { d := &Data{} |
