Skip to content
This repository has been archived by the owner on Feb 13, 2025. It is now read-only.

cmd/bosun: add pct template function #1083

Merged
merged 1 commit into from
Jun 18, 2015
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
5 changes: 4 additions & 1 deletion cmd/bosun/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,10 +668,13 @@ var defaultFuncs = ttemplate.FuncMap{
}
return ByteSize(0), fmt.Errorf("unexpected type: %T (%v)", v, v)
},
"pct": func(i interface{}) string {
return fmt.Sprintf("%.2f%%", i)
},
"replace": strings.Replace,
"short": func(v string) string {
return strings.SplitN(v, ".", 2)[0]
},
"replace": strings.Replace,
}

func (c *Conf) loadTemplate(s *parse.SectionNode) {
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ Global template functions:

* V: performs variable expansion on the argument and returns it. Needed since normal variable expansion is not done due to the `$` character being used by the Go template syntax.
* bytes: converts the string input into a human-readable number of bytes with extension KB, MB, GB, etc.
* pct: formats the float argument as a percentage. For example: `{{5.1 | pct}}` -> `5.10%`.
* replace: [strings.Replace](http://golang.org/pkg/strings/#Replace)
* short: Trims the string to everything before the first period. Useful for turning a FQDN into a shortname. For example: `{{short "foo.baz.com"}}` -> `foo`.

Expand Down