Skip to content

Commit

Permalink
Add wrapper function for go-sockaddr templating
Browse files Browse the repository at this point in the history
  • Loading branch information
thevilledev committed Aug 10, 2019
1 parent ece8cee commit 5870f91
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,18 @@ minconns: "2"
Note: Consul stores all KV data as strings. Thus true is "true", 1 is "1", etc.
##### `sockaddr`
Takes a quote-escaped template string as an argument and passes it on to
[hashicorp/go-sockaddr](https://github.com/hashicorp/go-sockaddr) templating engine.
```liquid
{{ sockaddr "GetPrivateIP" }}
```
See [hashicorp/go-sockaddr documentation](https://godoc.org/github.com/hashicorp/go-sockaddr)
for more information.
---
#### Math Functions
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/hashicorp/go-msgpack v0.5.5 // indirect
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/go-rootcerts v1.0.1
github.com/hashicorp/go-sockaddr v1.0.2
github.com/hashicorp/go-syslog v1.0.0
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/hashicorp/hcl v1.0.0
Expand Down
11 changes: 11 additions & 0 deletions template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/BurntSushi/toml"
dep "github.com/hashicorp/consul-template/dependency"
socktmpl "github.com/hashicorp/go-sockaddr/template"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -1189,3 +1190,13 @@ func sandboxedPath(sandbox, s string) (string, error) {
}
return path, nil
}

// sockaddr wraps go-sockaddr templating
func sockaddr(args ...string) (string, error) {
t := fmt.Sprintf("{{ %s }} ", strings.Join(args, " "))
k, err := socktmpl.Parse(t)
if err != nil {
return "", err
}
return k, nil
}
1 change: 1 addition & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func funcMap(i *funcMapInput) template.FuncMap {
"toUpper": toUpper,
"toYAML": toYAML,
"split": split,
"sockaddr": sockaddr,

// Math functions
"add": add,
Expand Down
11 changes: 11 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,17 @@ func TestTemplate_Execute(t *testing.T) {
"hi",
false,
},
{
"helper_sockaddr",
&NewTemplateInput{
Contents: `{{ sockaddr "GetAllInterfaces | include \"type\" \"IPv4\"" | contains "127.0.0.1" }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"true",
false,
},
{
"math_add",
&NewTemplateInput{
Expand Down

0 comments on commit 5870f91

Please sign in to comment.