Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding datasourceExists function #94

Merged
merged 1 commit into from
Feb 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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`.
Expand Down
11 changes: 10 additions & 1 deletion data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 17 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
Expand Down