feat: add tmpl function required (#1188)

Co-authored-by: Raymond Liu (RD-TW) <raymond_liu@trend.com.tw>
This commit is contained in:
RaymondKYLiu
2020-04-10 08:23:42 +09:00
committed by GitHub
co-authored by Raymond Liu
parent 870cc03c70
commit 7d11f5dedc
3 changed files with 135 additions and 0 deletions
+13
View File
@@ -26,6 +26,7 @@ func (c *Context) createFuncMap() template.FuncMap {
"get": get,
"getOrNil": getOrNil,
"tpl": c.Tpl,
"required": Required,
}
if c.preRender {
// disable potential side-effect template calls
@@ -242,3 +243,15 @@ func RequiredEnv(name string) (string, error) {
return "", fmt.Errorf("required env var `%s` is not set", name)
}
func Required(warn string, val interface{}) (interface{}, error) {
if val == nil {
return nil, fmt.Errorf(warn)
} else if _, ok := val.(string); ok {
if val == "" {
return nil, fmt.Errorf(warn)
}
}
return val, nil
}