/
template_functions.go
69 lines (59 loc) · 1.84 KB
/
template_functions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package controldisplay
import (
"bytes"
"encoding/csv"
"fmt"
"strings"
"sync"
"text/template"
"time"
"github.com/Masterminds/sprig/v3"
)
// templateFuncs merges desired functions from sprig with custom functions that we
// define in steampipe
func templateFuncs(renderContext TemplateRenderContext) template.FuncMap {
useFromSprigMap := []string{"upper", "toJson", "quote", "dict", "add", "now", "toPrettyJson"}
var funcs template.FuncMap = template.FuncMap{}
sprigMap := sprig.TxtFuncMap()
for _, use := range useFromSprigMap {
f, found := sprigMap[use]
if !found {
// guarantee that when a function is expected to be present
// it does not slip through any crack
panic(fmt.Sprintf("%s not found", use))
}
if found {
funcs[use] = f
}
}
// custom steampipe functions - ones we couldn't find in sprig
formatterTemplateFuncMap := template.FuncMap{
"durationInSeconds": durationInSeconds,
"toCsvCell": toCSVCellFnFactory(renderContext.Config.Separator),
}
for k, v := range formatterTemplateFuncMap {
funcs[k] = v
}
return funcs
}
// toCsvCell escapes a value for csv
// we need to do this in a factory function, so that we can
// set the separator for the CSV writer for this render session
func toCSVCellFnFactory(comma string) func(interface{}) string {
csvWriterBuffer := bytes.NewBufferString("")
csvBufferLock := sync.Mutex{}
csvWriter := csv.NewWriter(csvWriterBuffer)
if len(comma) > 0 {
csvWriter.Comma = []rune(comma)[0]
}
return func(v interface{}) string {
csvBufferLock.Lock()
defer csvBufferLock.Unlock()
csvWriterBuffer.Reset()
csvWriter.Write([]string{fmt.Sprintf("%v", v)})
csvWriter.Flush()
return strings.TrimSpace(csvWriterBuffer.String())
}
}
// durationInSeconds returns the passed in duration as seconds
func durationInSeconds(t time.Duration) float64 { return t.Seconds() }