github.com/kcmerrill/alfred@v0.0.0-20180727171036-06445dcb5e3d/pkg/alfred/translate.go (about) 1 package alfred 2 3 import ( 4 "bytes" 5 "os" 6 "text/template" 7 8 "github.com/Masterminds/sprig" 9 ) 10 11 func translateArgs(args []string, context *Context) []string { 12 translated := make([]string, 0) 13 for _, arg := range args { 14 translated = append(translated, translate(arg, context)) 15 } 16 return translated 17 } 18 19 func translate(raw string, context *Context) string { 20 if raw == "" { 21 // Nothing to translate, move along 22 return raw 23 } 24 fmap := sprig.TxtFuncMap() 25 te := template.Must(template.New("template." + context.TaskName).Funcs(fmap).Parse(raw)) 26 var b bytes.Buffer 27 context.Lock.Lock() 28 err := te.Execute(&b, context) 29 context.Lock.Unlock() 30 if err != nil { 31 context.Ok = false 32 outFail("template", "Invalid Argument(s)", context) 33 // TODO: chew on this some, should we fail? If not, how do we handle this gracefully? 34 os.Exit(42) 35 } 36 return b.String() 37 }