summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2018-05-03 21:50:37 -0400
committerDave Henderson <dhenderson@gmail.com>2018-05-03 22:57:25 -0400
commit6124d3dfd274ac24e5da0817ae172aca58c22d4e (patch)
tree22acb5a6e7bbf59dd1337953445e036b06da94b5
parent195a6dc75ddbc6f4899176d006c6dfba39e08f1a (diff)
Adding datasourceReachable function
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
-rw-r--r--data/datasource.go11
-rw-r--r--data/datasource_test.go27
-rw-r--r--docs/content/functions/data.md13
-rw-r--r--funcs/data.go1
-rw-r--r--test/integration/datasources_file_test.go9
5 files changed, 60 insertions, 1 deletions
diff --git a/data/datasource.go b/data/datasource.go
index e4336fb5..6370bd5f 100644
--- a/data/datasource.go
+++ b/data/datasource.go
@@ -257,6 +257,17 @@ func (d *Data) Datasource(alias string, args ...string) (interface{}, error) {
return nil, errors.Errorf("Datasources of type %s not yet supported", source.Type)
}
+// DatasourceReachable - Determines if the named datasource is reachable with
+// the given arguments. Reads from the datasource, and discards the returned data.
+func (d *Data) DatasourceReachable(alias string, args ...string) bool {
+ source, ok := d.Sources[alias]
+ if !ok {
+ return false
+ }
+ _, err := d.ReadSource(source, args...)
+ return err == nil
+}
+
// Include -
func (d *Data) Include(alias string, args ...string) (string, error) {
source, ok := d.Sources[alias]
diff --git a/data/datasource_test.go b/data/datasource_test.go
index dbf1f0af..ab71a060 100644
--- a/data/datasource_test.go
+++ b/data/datasource_test.go
@@ -170,6 +170,33 @@ func TestDatasource(t *testing.T) {
assert.Equal(t, "", actual)
}
+func TestDatasourceReachable(t *testing.T) {
+ fname := "foo.json"
+ fs := memfs.Create()
+ _ = fs.Mkdir("/tmp", 0777)
+ f, _ := vfs.Create(fs, "/tmp/"+fname)
+ _, _ = f.Write([]byte("{}"))
+
+ sources := map[string]*Source{
+ "foo": {
+ Alias: "foo",
+ URL: &url.URL{Scheme: "file", Path: "/tmp/" + fname},
+ Ext: "json",
+ Type: "application/json",
+ FS: fs,
+ },
+ "bar": {
+ Alias: "bar",
+ URL: &url.URL{Scheme: "file", Path: "/bogus"},
+ FS: fs,
+ },
+ }
+ data := &Data{Sources: sources}
+
+ assert.True(t, data.DatasourceReachable("foo"))
+ assert.False(t, data.DatasourceReachable("bar"))
+}
+
func TestDatasourceExists(t *testing.T) {
sources := map[string]*Source{
"foo": {Alias: "foo"},
diff --git a/docs/content/functions/data.md b/docs/content/functions/data.md
index 654e45af..277daafb 100644
--- a/docs/content/functions/data.md
+++ b/docs/content/functions/data.md
@@ -319,13 +319,24 @@ defined.
Note: this does _not_ verify if the datasource is reachable.
-Useful when used in an `if`/`else` block
+Useful when used in an `if`/`else` block.
```console
$ echo '{{if (datasourceExists "test")}}{{datasource "test"}}{{else}}no worries{{end}}' | gomplate
no worries
```
+## `datasourceReachable`
+
+Tests whether or not a given datasource is defined and reachable, where the definition of "reachable" differs by datasource, but generally means the data is able to be read successfully.
+
+Useful when used in an `if`/`else` block.
+
+```console
+$ gomplate -i '{{if (datasourceReachable "test")}}{{datasource "test"}}{{else}}no worries{{end}}' -d test=https://bogus.example.com/wontwork.json
+no worries
+```
+
## `ds`
Alias to [`datasource`](#datasource)
diff --git a/funcs/data.go b/funcs/data.go
index b2c2b7eb..4b1e0526 100644
--- a/funcs/data.go
+++ b/funcs/data.go
@@ -23,6 +23,7 @@ func AddDataFuncs(f map[string]interface{}, d *data.Data) {
f["datasource"] = d.Datasource
f["ds"] = d.Datasource
f["datasourceExists"] = d.DatasourceExists
+ f["datasourceReachable"] = d.DatasourceReachable
f["include"] = d.Include
f["data"] = DataNS
diff --git a/test/integration/datasources_file_test.go b/test/integration/datasources_file_test.go
index 1c8d1530..5703217f 100644
--- a/test/integration/datasources_file_test.go
+++ b/test/integration/datasources_file_test.go
@@ -53,6 +53,15 @@ func (s *FileDatasourcesSuite) TestFileDatasources(c *C) {
result.Assert(c, icmd.Expected{ExitCode: 0, Out: "bar"})
result = icmd.RunCommand(GomplateBin,
+ "-d", "config="+s.tmpDir.Join("config2.yml"),
+ "-i", `{{ if (datasourceReachable "bogus") }}bogus!{{ end -}}
+{{ if (datasourceReachable "config") -}}
+{{ (ds "config").foo -}}
+{{ end }}`,
+ )
+ result.Assert(c, icmd.Expected{ExitCode: 0, Out: "bar"})
+
+ result = icmd.RunCommand(GomplateBin,
"-d", "csv="+s.tmpDir.Join("foo.csv"),
"-i", `{{ index (index (ds "csv") 2) 1 }}`,
)