Skip to content

Commit

Permalink
fix readme->docs/ migration and template conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
eikenb committed Apr 24, 2021
2 parents 557cbba + fa9a28e commit fe2520f
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 76 deletions.
66 changes: 33 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,29 @@ this functionality might prove useful.
- [Quick Example](#quick-example)
- [Learn Guides](#learn-guides)
- [Configuration](docs/configuration.md)
- [Command Line Flags](docs/configuration.md#command-line-flags)
- [Configuration File](docs/configuration.md#configuration-file)
- [Command Line Flags](docs/configuration.md#command-line-flags)
- [Configuration File](docs/configuration.md#configuration-file)
- [Reload Configuration and Templates](#reload-configuration-and-templates)
- [Templating Language](docs/templating-language.md)
- [API Functions](docs/templating-language.md#api-functions)
- [Scratch](docs/templating-language.md#scratch)
- [Helper Functions](docs/templating-language.md#helper-functions)
- [Math Functions](docs/templating-language.md#math-functions)
- [API Functions](docs/templating-language.md#api-functions)
- [Scratch](docs/templating-language.md#scratch)
- [Helper Functions](docs/templating-language.md#helper-functions)
- [Math Functions](docs/templating-language.md#math-functions)
- [Observability](docs/observability.md)
- [Logging](docs/observability.md#logging)
- [Logging](docs/observability.md#logging)
- [Modes](docs/modes.md)
- [Once Mode](docs/modes.md#once-mode)
- [De-Duplication Mode](docs/modes.md#de-duplication-mode)
- [Exec Mode](docs/modes.md#exec-mode)
- [Once Mode](docs/modes.md#once-mode)
- [De-Duplication Mode](docs/modes.md#de-duplication-mode)
- [Exec Mode](docs/modes.md#exec-mode)
- [Plugins](docs/plugins.md)
- [Caveats](#caveats)
- [Docker Image Use](#docker-image-use)
- [Dots in Service Names](#dots-in-service-names)
- [Termination on Error](#termination-on-error)
- [Commands](#commands)
- [Environment](#environment)
- [Multiple Commands](#multiple-commands)
- [Multi-phase Execution](#multi-phase-execution)
- [Docker Image Use](#docker-image-use)
- [Dots in Service Names](#dots-in-service-names)
- [Termination on Error](#termination-on-error)
- [Commands](#commands)
- [Environment](#environment)
- [Multiple Commands](#multiple-commands)
- [Multi-phase Execution](#multi-phase-execution)
- [Running and Process Lifecycle](#running-and-process-lifecycle)
- [Debugging](#debugging)
- [FAQ](#faq)
Expand Down Expand Up @@ -88,34 +88,34 @@ This short example assumes Consul is installed locally.

1. Start a Consul cluster in dev mode:

```shell
$ consul agent -dev
```
```shell
$ consul agent -dev
```

1. Author a template `in.tpl` to query the kv store:

```liquid
{{ key "foo" }}
```
```liquid
{{ key "foo" }}
```

1. Start Consul Template:

```shell
$ consul-template -template "in.tpl:out.txt" -once
```
```shell
$ consul-template -template "in.tpl:out.txt" -once
```

1. Write data to the key in Consul:

```shell
$ consul kv put foo bar
```
```shell
$ consul kv put foo bar
```

1. Observe Consul Template has written the file `out.txt`:

```shell
$ cat out.txt
bar
```
```shell
$ cat out.txt
bar
```

For more examples and use cases, please see the [examples folder][examples] in
this repository.
Expand Down
38 changes: 38 additions & 0 deletions docs/templating-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ provides the following functions:
- [timestamp](#timestamp)
- [toJSON](#tojson)
- [toJSONPretty](#tojsonpretty)
- [toUnescapedJSON](#tounescapedjson)
- [toUnescapedJSONPretty](#tounescapedjsonpretty)
- [toLower](#tolower)
- [toTitle](#totitle)
- [toTOML](#totoml)
Expand Down Expand Up @@ -1393,6 +1395,42 @@ renders

Note: Consul stores all KV data as strings. Thus true is "true", 1 is "1", etc.

##### `toUnescapedJSON`

Takes the result from a `tree` or `ls` call and converts it into a JSON object without HTML escaping. This function comes in handy when working with db connection strings or URIs containing query parameters.

```liquid
{{ tree "config" | explode | toUnescapedJSON }}
```

renders

```javascript
{"admin":{"port":"1234"},"maxconns":"5","minconns":"2", "queryparams": "a?b=c&d=e"}
```

##### `toUnescapedJSONPretty`

Takes the result from a `tree` or `ls` call and converts it into a
pretty-printed JSON object without HTML escaping, indented by two spaces.

```liquid
{{ tree "config" | explode | toUnescapedJSONPretty }}
```

renders

```javascript
{
"admin": {
"port": "1234"
},
"maxconns": "5",
"minconns": "2",
"queryparams": "a?b=c&d=e"
}
```

### `toLower`

Takes the argument as a string and converts it to lowercase.
Expand Down
24 changes: 24 additions & 0 deletions template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,30 @@ func toJSONPretty(m map[string]interface{}) (string, error) {
return string(bytes.TrimSpace(result)), err
}

// toUnescapedJSON converts the given structure into a deeply nested JSON string without HTML escaping.
func toUnescapedJSON(i interface{}) (string, error) {
buf := &bytes.Buffer{}
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
if err := encoder.Encode(i); err != nil {
return "", errors.Wrap(err, "toUnescapedJSON")
}
return strings.TrimRight(buf.String(), "\r\n"), nil
}

// toUnescapedJSONPretty converts the given structure into a deeply nested pretty JSON
// string without HTML escaping.
func toUnescapedJSONPretty(m map[string]interface{}) (string, error) {
buf := &bytes.Buffer{}
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
if err := encoder.Encode(m); err != nil {
return "", errors.Wrap(err, "toUnescapedJSONPretty")
}
return strings.TrimRight(buf.String(), "\r\n"), nil
}

// toTitle converts the given string (usually by a pipe) to titlecase.
func toTitle(s string) (string, error) {
return strings.Title(s), nil
Expand Down
88 changes: 45 additions & 43 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,49 +248,51 @@ func funcMap(i *funcMapInput) template.FuncMap {
"scratch": func() *Scratch { return &scratch },

// Helper functions
"base64Decode": base64Decode,
"base64Encode": base64Encode,
"base64URLDecode": base64URLDecode,
"base64URLEncode": base64URLEncode,
"byKey": byKey,
"byTag": byTag,
"contains": contains,
"containsAll": containsSomeFunc(true, true),
"containsAny": containsSomeFunc(false, false),
"containsNone": containsSomeFunc(true, false),
"containsNotAll": containsSomeFunc(false, true),
"env": envFunc(i.env),
"executeTemplate": executeTemplateFunc(i.t),
"explode": explode,
"explodeMap": explodeMap,
"in": in,
"indent": indent,
"loop": loop,
"join": join,
"trimSpace": trimSpace,
"parseBool": parseBool,
"parseFloat": parseFloat,
"parseInt": parseInt,
"parseJSON": parseJSON,
"parseUint": parseUint,
"parseYAML": parseYAML,
"plugin": plugin,
"regexReplaceAll": regexReplaceAll,
"regexMatch": regexMatch,
"replaceAll": replaceAll,
"sha256Hex": sha256Hex,
"md5sum": md5sum,
"timestamp": timestamp,
"toLower": toLower,
"toJSON": toJSON,
"toJSONPretty": toJSONPretty,
"toTitle": toTitle,
"toTOML": toTOML,
"toUpper": toUpper,
"toYAML": toYAML,
"split": split,
"byMeta": byMeta,
"sockaddr": sockaddr,
"base64Decode": base64Decode,
"base64Encode": base64Encode,
"base64URLDecode": base64URLDecode,
"base64URLEncode": base64URLEncode,
"byKey": byKey,
"byTag": byTag,
"contains": contains,
"containsAll": containsSomeFunc(true, true),
"containsAny": containsSomeFunc(false, false),
"containsNone": containsSomeFunc(true, false),
"containsNotAll": containsSomeFunc(false, true),
"env": envFunc(i.env),
"executeTemplate": executeTemplateFunc(i.t),
"explode": explode,
"explodeMap": explodeMap,
"in": in,
"indent": indent,
"loop": loop,
"join": join,
"trimSpace": trimSpace,
"parseBool": parseBool,
"parseFloat": parseFloat,
"parseInt": parseInt,
"parseJSON": parseJSON,
"parseUint": parseUint,
"parseYAML": parseYAML,
"plugin": plugin,
"regexReplaceAll": regexReplaceAll,
"regexMatch": regexMatch,
"replaceAll": replaceAll,
"sha256Hex": sha256Hex,
"md5sum": md5sum,
"timestamp": timestamp,
"toLower": toLower,
"toJSON": toJSON,
"toJSONPretty": toJSONPretty,
"toUnescapedJSON": toUnescapedJSON,
"toUnescapedJSONPretty": toUnescapedJSONPretty,
"toTitle": toTitle,
"toTOML": toTOML,
"toUpper": toUpper,
"toYAML": toYAML,
"split": split,
"byMeta": byMeta,
"sockaddr": sockaddr,
// Math functions
"add": add,
"subtract": subtract,
Expand Down
38 changes: 38 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,44 @@ func TestTemplate_Execute(t *testing.T) {
"[\"a\",\"b\",\"c\"]",
false,
},
{
"helper_toUnescapedJSON",
&NewTemplateInput{
Contents: `{{ "a?b&c,x?y&z" | split "," | toUnescapedJSON }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"[\"a?b&c\",\"x?y&z\"]",
false,
},
{
"helper_toUnescapedJSONPretty",
&NewTemplateInput{
Contents: `{{ tree "key" | explode | toUnescapedJSONPretty }}`,
},
&ExecuteInput{
Brain: func() *Brain {
b := NewBrain()
d, err := dep.NewKVListQuery("key")
if err != nil {
t.Fatal(err)
}
b.Remember(d, []*dep.KeyPair{
&dep.KeyPair{Key: "a", Value: "b&c"},
&dep.KeyPair{Key: "x", Value: "y&z"},
&dep.KeyPair{Key: "k", Value: "<>&&"},
})
return b
}(),
},
`{
"a": "b&c",
"k": "<>&&",
"x": "y&z"
}`,
false,
},
{
"helper_toLower",
&NewTemplateInput{
Expand Down

0 comments on commit fe2520f

Please sign in to comment.