From c0076a7869ce2872b62c2756e48a70b218991dac Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Fri, 3 Feb 2017 07:49:32 -0500 Subject: [PATCH] Adding datasourceExists function (#94) Signed-off-by: Dave Henderson --- README.md | 17 +++++++++++++++++ data.go | 11 ++++++++++- data_test.go | 9 +++++++++ main.go | 33 +++++++++++++++++---------------- 4 files changed, 53 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index a9cb0165b..12c488f57 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ Gomplate is an alternative that will let you process templates which also includ - [Basic usage](#basic-usage) - [Usage with HTTP data](#usage-with-http-data) - [Usage with Vault data](#usage-with-vault-data) + - [`datasourceExists`](#datasourceexists) - [`ec2meta`](#ec2meta) - [Example](#example) - [`ec2dynamic`](#ec2dynamic) @@ -368,6 +369,22 @@ $ echo 'db_password={{(datasource "vault" "db/pass").value}}' \ db_password=prodsecret ``` +#### `datasourceExists` + +Tests whether or not a given datasource was defined on the commandline (with the +[`--datasource/-d`](#--datasource-d) argument). This is intended mainly to allow +a template to be rendered differently whether or not a given datasource was +defined. + +Note: this does _not_ verify if the datasource is reachable. + +Useful when used in an `if`/`else` block + +```console +$ echo '{{if (datasourceExists "test")}}{{datasource "test"}}{{else}}no worries{{end}}' | gomplate +no worries +``` + #### `ec2meta` Queries AWS [EC2 Instance Metadata](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) for information. This only retrieves data in the `meta-data` path -- for data in the `dynamic` path use `ec2dynamic`. diff --git a/data.go b/data.go index 8bf24ddff..3c4793090 100644 --- a/data.go +++ b/data.go @@ -152,9 +152,18 @@ func absURL(value string) *url.URL { return baseURL.ResolveReference(relURL) } +// DatasourceExists - +func (d *Data) DatasourceExists(alias string) bool { + _, ok := d.Sources[alias] + return ok +} + // Datasource - func (d *Data) Datasource(alias string, args ...string) map[string]interface{} { - source := d.Sources[alias] + source, ok := d.Sources[alias] + if !ok { + log.Fatalf("Undefined datasource '%s'", alias) + } b, err := d.ReadSource(source.FS, source, args...) if err != nil { log.Fatalf("Couldn't read datasource '%s': %s", alias, err) diff --git a/data_test.go b/data_test.go index 0f63a5840..4df252a6a 100644 --- a/data_test.go +++ b/data_test.go @@ -101,6 +101,15 @@ func TestDatasource(t *testing.T) { test("yml", "application/yaml", `hello: world`) } +func TestDatasourceExists(t *testing.T) { + sources := map[string]*Source{ + "foo": {Alias: "foo"}, + } + data := &Data{Sources: sources} + assert.True(t, data.DatasourceExists("foo")) + assert.False(t, data.DatasourceExists("bar")) +} + func setupHTTP(code int, mimetype string, body string) (*httptest.Server, *http.Client) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", mimetype) diff --git a/main.go b/main.go index e31e882cd..bd423e8f6 100644 --- a/main.go +++ b/main.go @@ -48,22 +48,23 @@ func NewGomplate(data *Data) *Gomplate { ec2info := aws.NewEc2Info() return &Gomplate{ funcMap: template.FuncMap{ - "getenv": env.Getenv, - "bool": typeconv.Bool, - "json": typeconv.JSON, - "jsonArray": typeconv.JSONArray, - "yaml": typeconv.YAML, - "yamlArray": typeconv.YAMLArray, - "slice": typeconv.Slice, - "join": typeconv.Join, - "ec2meta": ec2meta.Meta, - "ec2dynamic": ec2meta.Dynamic, - "ec2tag": ec2info.Tag, - "ec2region": ec2meta.Region, - "title": strings.Title, - "toUpper": strings.ToUpper, - "toLower": strings.ToLower, - "datasource": data.Datasource, + "getenv": env.Getenv, + "bool": typeconv.Bool, + "json": typeconv.JSON, + "jsonArray": typeconv.JSONArray, + "yaml": typeconv.YAML, + "yamlArray": typeconv.YAMLArray, + "slice": typeconv.Slice, + "join": typeconv.Join, + "ec2meta": ec2meta.Meta, + "ec2dynamic": ec2meta.Dynamic, + "ec2tag": ec2info.Tag, + "ec2region": ec2meta.Region, + "title": strings.Title, + "toUpper": strings.ToUpper, + "toLower": strings.ToLower, + "datasource": data.Datasource, + "datasourceExists": data.DatasourceExists, }, } }