Created
July 22, 2015 14:15
-
-
Save tbruyelle/d3bf6439bc12ea0e1cc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func tmpl(w io.Writer, text string, data interface{}) { | |
t := template.New("top") | |
t.Funcs(template.FuncMap{ | |
"trim": strings.TrimSpace, | |
// deref allows to use pointer in templates | |
"deref": func(data interface{}) string { | |
if reflect.ValueOf(data).IsNil() { | |
return "" | |
} | |
switch vv := data.(type) { | |
case *string: | |
return *vv | |
case *int: | |
return fmt.Sprintf("%d", *vv) | |
default: | |
return fmt.Sprint(vv) | |
} | |
}, | |
"localBranch": func(data interface{}) string { | |
switch data := data.(type) { | |
case string: | |
i := strings.Index(data, "origin/") | |
if i != -1 { | |
return data[7:] | |
} | |
return data | |
default: | |
return fmt.Sprint(data) | |
} | |
}, | |
}, | |
) | |
template.Must(t.Parse(text)) | |
if err := t.Execute(w, data); err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment