summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2020-05-21 20:36:57 -0400
committerDave Henderson <dhenderson@gmail.com>2020-05-21 20:36:57 -0400
commitac32d560614d2fbff910335d40cea3ac8cfae00f (patch)
tree675a6f75e017a77ab44ccd07cc84eefe054c6037
parentda66c3437d3f4bca4423b07a6009f5d060a61839 (diff)
refactoring: extract stdin and boltdb readers
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
-rw-r--r--data/datasource.go34
-rw-r--r--data/datasource_boltdb.go27
-rw-r--r--data/datasource_stdin.go19
-rw-r--r--data/datasource_stdin_test.go22
-rw-r--r--data/datasource_test.go15
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{}