Skip to content

Commit

Permalink
Add fail function
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent Rancourt authored and Kent Rancourt committed May 16, 2017
1 parent f5b0ed4 commit 3ca3f10
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/flow_control.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Flow Control Functions

## fail

Unconditionally returns an empty `string` and an `error` with the specified
text. This is useful in scenarios where other conditionals have determined that
template rendering should fail.

```
fail "Please accept the end user license agreement"
```
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The Sprig library provides over 70 template functions for Go's template language
- [Dictionaries and Dict Functions](dicts.html): `dict`, `hasKey`, `pluck`, etc.
- [Type Conversion Functions](conversion.html): `atoi`, `int64`, `toString`, etc.
- [File Path Functions](paths.html): `base`, `dir`, `ext`, `clean`, `isAbs`
- [Flow Control Functions](flow_control.html): `fail`
- Advanced Functions
- [UUID Functions](uuid.html): `uuidv4`
- [OS Functions](os.html): `env`, `expandenv`
Expand Down
16 changes: 16 additions & 0 deletions flow_control_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package sprig

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFail(t *testing.T) {
const msg = "This is an error!"
tpl := fmt.Sprintf(`{{fail "%s"}}`, msg)
_, err := runRaw(tpl, nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), msg)
}
4 changes: 4 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sprig

import (
"errors"
"html/template"
"os"
"path"
Expand Down Expand Up @@ -254,4 +255,7 @@ var genericMap = map[string]interface{}{
// SemVer:
"semver": semver,
"semverCompare": semverCompare,

// Flow Control:
"fail": func(msg string) (string, error) { return "", errors.New(msg) },
}

0 comments on commit 3ca3f10

Please sign in to comment.