From 0ca223237a0714eebe4e8f3da10e09031999537f Mon Sep 17 00:00:00 2001 From: jeevansai Date: Sat, 5 Feb 2022 19:52:00 +0530 Subject: Add function to list datasources (#1287) * Add function to list datasources * Sort datasources in ascending order and return in listDatasources * Fix lint error Signed-off-by: Dave Henderson Co-authored-by: Dave Henderson --- data/datasource.go | 11 +++++++++++ data/datasource_test.go | 10 ++++++++++ docs-src/content/functions/data.yml | 9 +++++++++ docs/content/functions/data.md | 18 ++++++++++++++++++ funcs/data.go | 1 + 5 files changed, 49 insertions(+) diff --git a/data/datasource.go b/data/datasource.go index 5f684a4c..79c29e5f 100644 --- a/data/datasource.go +++ b/data/datasource.go @@ -7,6 +7,7 @@ import ( "net/http" "net/url" "path/filepath" + "sort" "strings" "github.com/spf13/afero" @@ -382,3 +383,13 @@ func (d *Data) readSource(ctx context.Context, source *Source, args ...string) ( d.cache[cacheKey] = data return data, nil } + +// Show all datasources - +func (d *Data) ListDatasources() []string { + datasources := make([]string, 0, len(d.Sources)) + for source := range d.Sources { + datasources = append(datasources, source) + } + sort.Strings(datasources) + return datasources +} diff --git a/data/datasource_test.go b/data/datasource_test.go index 4cffc763..aadcef0e 100644 --- a/data/datasource_test.go +++ b/data/datasource_test.go @@ -412,3 +412,13 @@ func TestFromConfig(t *testing.T) { } assert.EqualValues(t, expected, FromConfig(ctx, cfg)) } + +func TestListDatasources(t *testing.T) { + sources := map[string]*Source{ + "foo": {Alias: "foo"}, + "bar": {Alias: "bar"}, + } + data := &Data{Sources: sources} + + assert.Equal(t, []string{"bar", "foo"}, data.ListDatasources()) +} diff --git a/docs-src/content/functions/data.yml b/docs-src/content/functions/data.yml index 15106372..8fa47736 100644 --- a/docs-src/content/functions/data.yml +++ b/docs-src/content/functions/data.yml @@ -62,6 +62,15 @@ funcs: - | $ gomplate -i '{{if (datasourceReachable "test")}}{{datasource "test"}}{{else}}no worries{{end}}' -d test=https://bogus.example.com/wontwork.json no worries + - name: listDatasources + description: | + Lists all the datasources defined, list returned will be sorted in ascending order. + pipeline: false + examples: + - | + $ gomplate -d person=env:///FOO -d bar=env:///BAR -i '{{range (listDatasources)}} Datasource-{{.}} {{end}}' + Datasource-bar + Datasource-person - name: defineDatasource description: | Define a datasource alias with target URL inside the template. Overridden by the [`--datasource/-d`](../../usage/#datasource-d) flag. diff --git a/docs/content/functions/data.md b/docs/content/functions/data.md index 4845de60..31d2b27c 100644 --- a/docs/content/functions/data.md +++ b/docs/content/functions/data.md @@ -97,6 +97,24 @@ $ gomplate -i '{{if (datasourceReachable "test")}}{{datasource "test"}}{{else}}n no worries ``` +## `listDatasources` + +Lists all the datasources defined, list returned will be sorted in ascending order. + +### Usage + +```go +listDatasources +``` + +### Examples + +```console +$ gomplate -d person=env:///FOO -d bar=env:///BAR -i '{{range (listDatasources)}} Datasource-{{.}} {{end}}' +Datasource-bar +Datasource-person +``` + ## `defineDatasource` Define a datasource alias with target URL inside the template. Overridden by the [`--datasource/-d`](../../usage/#datasource-d) flag. diff --git a/funcs/data.go b/funcs/data.go index a6e48ba3..f4ae7b32 100644 --- a/funcs/data.go +++ b/funcs/data.go @@ -30,6 +30,7 @@ func CreateDataFuncs(ctx context.Context, d *data.Data) map[string]interface{} { f["datasourceReachable"] = d.DatasourceReachable f["defineDatasource"] = d.DefineDatasource f["include"] = d.Include + f["listDatasources"] = d.ListDatasources ns := &DataFuncs{ctx} -- cgit v1.2.3