github.com/mutagen-io/mutagen@v0.18.0-rc1/cmd/mutagen/common/templating/templating.go (about) 1 package templating 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "strings" 7 "text/template" 8 9 "github.com/mutagen-io/mutagen/pkg/platform/terminal" 10 ) 11 12 // jsonify is the built-in JSON encoder that's made available to templates. 13 func jsonify(value any) (string, error) { 14 // Create a buffer to store the output. 15 buffer := &bytes.Buffer{} 16 17 // Create and configure a JSON encoder. 18 encoder := json.NewEncoder(buffer) 19 encoder.SetEscapeHTML(false) 20 21 // Marshal the value. 22 if err := encoder.Encode(value); err != nil { 23 return "", err 24 } 25 26 // Convert the encoded JSON to a string. 27 result := buffer.String() 28 29 // Remove the trailing newline that's automatically added by Encode. 30 result = strings.TrimSuffix(result, "\n") 31 32 // Success. 33 return result, nil 34 } 35 36 // builtins are the builtin functions supported in output templates. 37 var builtins = template.FuncMap{ 38 "json": jsonify, 39 "shellSanitize": terminal.NeutralizeControlCharacters, 40 // TODO: Figure out what other functions we want to include here, if any. 41 }