/
html.go
81 lines (70 loc) 路 2.2 KB
/
html.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
70
71
72
73
74
75
76
77
78
79
80
81
package output
import (
"bufio"
"bytes"
"fmt"
"html/template"
"strings"
"github.com/shopspring/decimal"
"github.com/infracost/infracost/internal/ui"
"github.com/Masterminds/sprig"
"github.com/rs/zerolog/log"
)
func ToHTML(out Root, opts Options) ([]byte, error) {
var buf bytes.Buffer
bufw := bufio.NewWriter(&buf)
tmpl := template.New("html.tmpl")
tmpl.Funcs(sprig.FuncMap())
tmpl.Funcs(template.FuncMap{
"safeHTML": func(s interface{}) template.HTML {
return template.HTML(fmt.Sprint(s)) // nolint:gosec
},
"replaceNewLines": func(s string) template.HTML {
safe := template.HTMLEscapeString(s)
safe = strings.ReplaceAll(safe, "\n", "<br />")
return template.HTML(safe) // nolint:gosec
},
"stripColor": ui.StripColor,
"contains": contains,
"hasCost": func(cc []CostComponent, sr []Resource, resourceName string) bool {
if len(cc) > 0 || len(sr) > 0 {
return true
}
log.Debug().Msgf("Hiding resource with no usage: %s", resourceName)
return false
},
"filterZeroValComponents": filterZeroValComponents,
"filterZeroValResources": filterZeroValResources,
"formatCost2DP": func(d *decimal.Decimal) string { return FormatCost2DP(out.Currency, d) },
"formatPrice": func(d decimal.Decimal) string { return formatPrice(out.Currency, d) },
"formatTitleWithCurrency": func(title string) string { return formatTitleWithCurrency(title, out.Currency) },
"formatQuantity": formatQuantity,
"projectLabel": func(p Project) string {
return p.Label()
},
"projectModulePath": func(p Project) string {
return p.Metadata.TerraformModulePath
},
"projectWorkspace": func(p Project) string {
return p.Metadata.WorkspaceLabel()
},
})
_, err := tmpl.ParseFS(templatesFS, "templates/html.tmpl")
if err != nil {
return []byte{}, err
}
summaryMessage := out.summaryMessage(opts.ShowSkipped)
msg, exceeded := out.Projects.IsRunQuotaExceeded()
err = tmpl.Execute(bufw, struct {
Root Root
SummaryMessage string
Options Options
RunQuotaExceeded bool
RunQuotaMsg string
}{out, summaryMessage, opts, exceeded, msg})
if err != nil {
return []byte{}, err
}
bufw.Flush()
return buf.Bytes(), nil
}