summaryrefslogtreecommitdiff
path: root/cmd/podmanV2/report/templates.go
blob: dc43d4f9b4e90f83a3dc048502d820f2ef805348 (plain)
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
package report

import (
	"strings"
	"text/template"
	"time"

	"github.com/docker/go-units"
)

var defaultFuncMap = template.FuncMap{
	"ellipsis": func(s string, length int) string {
		if len(s) > length {
			return s[:length-3] + "..."
		}
		return s
	},
	// TODO: Remove on Go 1.14 port
	"slice": func(s string, i, j int) string {
		if i > j || len(s) < i {
			return s
		}
		if len(s) < j {
			return s[i:]
		}
		return s[i:j]
	},
	"toRFC3339": func(t int64) string {
		return time.Unix(t, 0).Format(time.RFC3339)
	},
	"toHumanDuration": func(t int64) string {
		return units.HumanDuration(time.Since(time.Unix(t, 0))) + " ago"
	},
	"toHumanSize": func(sz int64) string {
		return units.HumanSize(float64(sz))
	},
}

func ReportHeader(columns ...string) []byte {
	hdr := make([]string, len(columns))
	for i, h := range columns {
		hdr[i] = strings.ToUpper(h)
	}
	return []byte(strings.Join(hdr, "\t") + "\n")
}

func AppendFuncMap(funcMap template.FuncMap) template.FuncMap {
	merged := PodmanTemplateFuncs()
	for k, v := range funcMap {
		merged[k] = v
	}
	return merged
}

func PodmanTemplateFuncs() template.FuncMap {
	merged := make(template.FuncMap)
	for k, v := range defaultFuncMap {
		merged[k] = v
	}
	return merged
}