summaryrefslogtreecommitdiff
path: root/data/datasource.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2017-11-18 09:33:49 -0500
committerDave Henderson <dhenderson@gmail.com>2017-11-18 10:06:48 -0500
commit01b39ee0955b59abdf1895b18435f1731226fcac (patch)
treef48a5a55cb82db2ef29fceab1b99e6d0df2f8f47 /data/datasource.go
parent59068e64a201941984630478dd26fc9e583222d7 (diff)
Adding support for stdin: scheme for datasources
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'data/datasource.go')
-rw-r--r--data/datasource.go20
1 files changed, 20 insertions, 0 deletions
diff --git a/data/datasource.go b/data/datasource.go
index 9824e41f..ef693761 100644
--- a/data/datasource.go
+++ b/data/datasource.go
@@ -3,6 +3,7 @@ package data
import (
"errors"
"fmt"
+ "io"
"io/ioutil"
"log"
"mime"
@@ -22,6 +23,9 @@ import (
// logFatal is defined so log.Fatal calls can be overridden for testing
var logFatalf = log.Fatalf
+// stdin - for overriding in tests
+var stdin io.Reader
+
func regExtension(ext, typ string) {
err := mime.AddExtensionType(ext, typ)
if err != nil {
@@ -43,6 +47,7 @@ func init() {
addSourceReader("http", readHTTP)
addSourceReader("https", readHTTP)
addSourceReader("file", readFile)
+ addSourceReader("stdin", readStdin)
addSourceReader("vault", readVault)
addSourceReader("consul", readConsul)
addSourceReader("consul+http", readConsul)
@@ -157,6 +162,9 @@ func ParseSource(value string) (*Source, error) {
srcURL = absURL(f)
} else if len(parts) == 2 {
alias = parts[0]
+ if parts[1] == "-" {
+ parts[1] = "stdin://"
+ }
var err error
srcURL, err = url.Parse(parts[1])
if err != nil {
@@ -296,6 +304,18 @@ func readFile(source *Source, args ...string) ([]byte, error) {
return b, nil
}
+func readStdin(source *Source, args ...string) ([]byte, error) {
+ if stdin == nil {
+ stdin = os.Stdin
+ }
+ b, err := ioutil.ReadAll(stdin)
+ if err != nil {
+ log.Printf("Can't read %v: %#v", stdin, err)
+ return nil, err
+ }
+ return b, nil
+}
+
func readHTTP(source *Source, args ...string) ([]byte, error) {
if source.HC == nil {
source.HC = &http.Client{Timeout: time.Second * 5}