fix(envvar): use template for string value

This commit is contained in:
Jan De Dobbeleer 2021-11-15 13:26:36 +01:00 committed by Jan De Dobbeleer
parent 6eb9a2e492
commit 290b1a3d8c
2 changed files with 19 additions and 1 deletions

View file

@ -50,7 +50,12 @@ The segment will show when the value of the environment variable isn't empty.
```
- var_name: `string` - the name of the environment variable
- template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
properties below. Defaults to the value of the environment variable.
## Template Properties
- `.Value`: `string` - the value of the environment variable
[go-text-template]: https://golang.org/pkg/text/template/
[sprig]: https://masterminds.github.io/sprig/

View file

@ -18,8 +18,21 @@ func (e *envvar) enabled() bool {
}
func (e *envvar) string() string {
segmentTemplate := e.props.getString(SegmentTemplate, "")
if len(segmentTemplate) == 0 {
return e.Value
}
template := &textTemplate{
Template: segmentTemplate,
Context: e,
Env: e.env,
}
text, err := template.render()
if err != nil {
return err.Error()
}
return text
}
func (e *envvar) init(props *properties, env environmentInfo) {
e.props = props