diff options
| author | Stuart Clark <stuart.clark@Jahingo.com> | 2017-08-03 01:23:08 +0100 |
|---|---|---|
| committer | Dave Henderson <dhenderson@gmail.com> | 2017-08-02 20:23:08 -0400 |
| commit | ab59ea091a2374653ea8aeb5ed7675de61c08461 (patch) | |
| tree | d9f6da8b3cb65c620adf0604ed8633f5cdc3be5f /data.go | |
| parent | c0e706dc97240446494f7cdb5b907d358697b285 (diff) | |
Add Consul & BoltDB datasource support (#178)
* Add libkv support
* Add vendoring
Diffstat (limited to 'data.go')
| -rw-r--r-- | data.go | 42 |
1 files changed, 41 insertions, 1 deletions
@@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "io/ioutil" "log" @@ -14,6 +15,7 @@ import ( "time" "github.com/blang/vfs" + "github.com/hairyhenderson/gomplate/libkv" "github.com/hairyhenderson/gomplate/vault" ) @@ -42,6 +44,10 @@ func init() { addSourceReader("https", readHTTP) addSourceReader("file", readFile) addSourceReader("vault", readVault) + addSourceReader("consul", readLibKV) + addSourceReader("consul+http", readLibKV) + addSourceReader("consul+https", readLibKV) + addSourceReader("boltdb", readLibKV) } var sourceReaders map[string]func(*Source, ...string) ([]byte, error) @@ -85,6 +91,7 @@ type Source struct { FS vfs.Filesystem // used for file: URLs, nil otherwise HC *http.Client // used for http[s]: URLs, nil otherwise VC *vault.Client //used for vault: URLs, nil otherwise + KV *libkv.LibKV // used for consul:, etcd:, zookeeper: & boltdb: URLs, nil otherwise Header http.Header // used for http[s]: URLs, nil otherwise } @@ -98,7 +105,7 @@ func NewSource(alias string, URL *url.URL) (s *Source) { Ext: ext, } - if ext != "" { + if ext != "" && URL.Scheme != "boltdb" { mediatype := mime.TypeByExtension(ext) t, params, err := mime.ParseMediaType(mediatype) if err != nil { @@ -194,6 +201,9 @@ func (d *Data) Datasource(alias string, args ...string) interface{} { if source.Type == "application/toml" { return ty.TOML(s) } + if source.Type == "text/plain" { + return s + } log.Fatalf("Datasources of type %s not yet supported", source.Type) return nil } @@ -326,6 +336,36 @@ func readVault(source *Source, args ...string) ([]byte, error) { return data, nil } +func readLibKV(source *Source, args ...string) ([]byte, error) { + if source.KV == nil { + source.KV = libkv.New(source.URL) + err := source.KV.Login() + addCleanupHook(source.KV.Logout) + if err != nil { + return nil, err + } + } + + p := source.URL.Path + + if source.URL.Scheme == "boltdb" { + if len(args) != 1 { + return nil, errors.New("missing key") + } + p = args[0] + } else if len(args) == 1 { + p = p + "/" + args[0] + } + + data, err := source.KV.Read(p) + if err != nil { + return nil, err + } + source.Type = "text/plain" + + return data, nil +} + func parseHeaderArgs(headerArgs []string) map[string]http.Header { headers := make(map[string]http.Header) for _, v := range headerArgs { |
