-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring: extract stdin and boltdb readers
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
- Loading branch information
Dave Henderson
committed
May 22, 2020
1 parent
da66c34
commit ac32d56
Showing
5 changed files
with
68 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters