/
markdown.go
451 lines (379 loc) · 13 KB
/
markdown.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package output
import (
"bufio"
"bytes"
"embed"
"fmt"
"regexp"
"sort"
"strings"
"text/template"
"unicode/utf8"
"github.com/Masterminds/sprig"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
"github.com/infracost/infracost/internal/ui"
)
//go:embed templates/*
var templatesFS embed.FS
func formatMarkdownCostChange(currency string, pastCost, cost *decimal.Decimal, skipPlusMinus, skipPercent bool) string {
if pastCost == nil && cost == nil {
return "-"
}
plusMinus := "+"
if skipPlusMinus {
plusMinus = ""
}
if pastCost != nil && cost != nil && pastCost.Equals(*cost) {
return plusMinus + formatWholeDecimalCurrency(currency, decimal.Zero)
}
percentChange := ""
if !skipPercent {
percentChange = formatPercentChange(pastCost, cost)
if len(percentChange) > 0 {
percentChange = " " + "(" + percentChange + ")"
}
}
// can't just use out.DiffTotalMonthlyCost because it isn't set if there is no past cost
if pastCost != nil {
d := cost.Sub(*pastCost)
if skipPlusMinus {
d = d.Abs()
return formatCost(currency, &d) + percentChange
}
if d.LessThan(decimal.Zero) {
plusMinus = ""
}
return plusMinus + formatCost(currency, &d) + percentChange
}
return plusMinus + formatCost(currency, cost) + percentChange
}
func formatCostChangeSentence(currency string, pastCost, cost *decimal.Decimal, useEmoji bool) string {
up := "📈"
down := "📉"
if !useEmoji {
up = "↑"
down = "↓"
}
if pastCost == nil {
return "Monthly cost will increase by " + formatCost(currency, cost) + " " + up
}
diff := cost.Sub(*pastCost).Abs()
change := formatCost(currency, &diff)
if pastCost.Equals(*cost) {
return "Monthly cost will not change"
}
if pastCost.GreaterThan(*cost) {
return "Monthly cost will decrease by " + change + " " + down
}
return "Monthly cost will increase by " + change + " " + up
}
func calculateMetadataToDisplay(projects []Project) (hasModulePath bool, hasWorkspace bool) {
sort.Slice(projects, func(i, j int) bool {
if projects[i].Name != projects[j].Name {
return projects[i].Name < projects[j].Name
}
if projects[i].Metadata.TerraformModulePath != projects[j].Metadata.TerraformModulePath {
return projects[i].Metadata.TerraformModulePath < projects[j].Metadata.TerraformModulePath
}
return projects[i].Metadata.WorkspaceLabel() < projects[j].Metadata.WorkspaceLabel()
})
// check if any projects that have the same name have different path or workspace
for i, p := range projects {
if i > 0 { // we compare vs the previous item, so skip index 0
prev := projects[i-1]
if p.Name == prev.Name {
if p.Metadata.TerraformModulePath != prev.Metadata.TerraformModulePath {
hasModulePath = true
}
if p.Metadata.WorkspaceLabel() != prev.Metadata.WorkspaceLabel() {
hasWorkspace = true
}
}
}
}
return hasModulePath, hasWorkspace
}
// MarkdownCtx holds information that can be used and executed with a go template.
type MarkdownCtx struct {
Root Root
SkippedProjectCount int
ErroredProjectCount int
SkippedUnchangedProjectCount int
DiffOutput string
Options Options
MarkdownOptions MarkdownOptions
RunQuotaMsg string
UsageCostsMsg string
CostDetailsMsg string
}
// MarkdownOutput holds the message converted to markdown with additional
// information about its length.
type MarkdownOutput struct {
Msg []byte
RuneLen int
OriginalMsgSize int
}
func ToMarkdown(out Root, opts Options, markdownOpts MarkdownOptions) (MarkdownOutput, error) {
var diffMsg string
if opts.diffMsg != "" {
diffMsg = opts.diffMsg
} else {
diff, err := ToDiff(out, opts)
if err != nil {
return MarkdownOutput{}, errors.Wrap(err, "Failed to generate diff")
}
diffMsg = ui.StripColor(string(diff))
}
hasModulePath, hasWorkspace := calculateMetadataToDisplay(out.Projects)
var buf bytes.Buffer
bufw := bufio.NewWriter(&buf)
filename := "markdown-html.tmpl"
if out.Metadata.UsageApiEnabled {
// for now only show the new usage-costs-including comment if the usage api has been enabled
// once we have all the other usage cost stuff done this will replace the old comment template
filename = "markdown-html-usage-api.tmpl"
}
if markdownOpts.BasicSyntax {
filename = "markdown.tmpl"
if out.Metadata.UsageApiEnabled {
// for now only show the new usage-costs-including comment if the usage api has been enabled
// once we have all the other usage cost stuff done this will replace the old comment template
filename = "markdown-usage-api.tmpl"
}
}
runQuotaMsg, exceeded := out.Projects.IsRunQuotaExceeded()
if exceeded {
filename = "run-quota-exceeded.tmpl"
}
tmpl := template.New(filename)
tmpl.Funcs(sprig.TxtFuncMap())
tmpl.Funcs(template.FuncMap{
"formatCost": func(d *decimal.Decimal) string {
if d == nil || d.IsZero() {
return formatWholeDecimalCurrency(out.Currency, decimal.Zero)
}
return formatCost(out.Currency, d)
},
"formatCostChange": func(pastCost, cost *decimal.Decimal) string {
return formatMarkdownCostChange(out.Currency, pastCost, cost, false, false)
},
"formatCostChangeWithoutPercent": func(pastCost, cost *decimal.Decimal) string {
return formatMarkdownCostChange(out.Currency, pastCost, cost, false, true)
},
"formatCostChangeSentence": formatCostChangeSentence,
"showProject": func(p Project) bool {
return showProject(p, opts, false)
},
"cloudURL": func() string {
return out.CloudURL
},
"displaySub": func() bool {
if out.CloudURL != "" {
return true
}
return markdownOpts.WillUpdate || markdownOpts.WillReplace
},
"displayTable": func() bool {
var valid Projects
for _, project := range out.Projects {
if showProject(project, opts, false) {
valid = append(valid, project)
}
}
return len(valid) > 0
},
"displayOutput": func() bool {
if markdownOpts.OmitDetails {
return false
}
// we always want to show the output if there are unsupported resources. This is
// because we want to show the unsupported resources in the output so that the
// user can see why the cost changes are different from expectations.
if out.HasUnsupportedResources() {
return true
}
var valid Projects
for _, project := range out.Projects {
if showProject(project, opts, true) {
valid = append(valid, project)
}
}
return len(valid) > 0
},
"metadataHeaders": func() []string {
headers := []string{}
if hasModulePath {
headers = append(headers, "Module path")
}
if hasWorkspace {
headers = append(headers, "Workspace")
}
return headers
},
"metadataFields": func(p Project) []string {
fields := []string{}
if hasModulePath {
fields = append(fields, p.Metadata.TerraformModulePath)
}
if hasWorkspace {
fields = append(fields, p.Metadata.WorkspaceLabel())
}
return fields
},
"metadataPlaceholders": func() []string {
placeholders := []string{}
if hasModulePath {
placeholders = append(placeholders, "")
}
if hasWorkspace {
placeholders = append(placeholders, "")
}
return placeholders
},
"stringsJoin": strings.Join,
"truncateMiddle": truncateMiddle,
})
_, err := tmpl.ParseFS(templatesFS, "templates/"+filename)
if err != nil {
return MarkdownOutput{}, err
}
skippedProjectCount := 0
for _, p := range out.Projects {
if p.Metadata.HasErrors() {
continue
}
if (p.Diff == nil || len(p.Diff.Resources) == 0) && !hasCodeChanges(opts, p) {
skippedProjectCount++
}
}
erroredProjectCount := 0
for _, p := range out.Projects {
if p.Metadata.HasErrors() {
erroredProjectCount++
}
}
skippedUnchangedProjectCount := 0
if opts.ShowOnlyChanges {
for _, p := range out.Projects {
if !hasCodeChanges(opts, p) {
skippedUnchangedProjectCount++
}
}
}
err = tmpl.Execute(bufw, MarkdownCtx{
Root: out,
SkippedProjectCount: skippedProjectCount,
ErroredProjectCount: erroredProjectCount,
SkippedUnchangedProjectCount: skippedUnchangedProjectCount,
DiffOutput: diffMsg,
Options: opts,
MarkdownOptions: markdownOpts,
RunQuotaMsg: runQuotaMsg,
UsageCostsMsg: usageCostsMessage(out, true),
CostDetailsMsg: costsDetailsMessage(out),
})
if err != nil {
return MarkdownOutput{}, err
}
bufw.Flush()
msg := buf.Bytes()
msgByteLength := len(msg)
msgRuneLength := utf8.RuneCount(msg)
originalSize := msgRuneLength
if opts.originalSize > 0 {
originalSize = opts.originalSize
}
if markdownOpts.MaxMessageSize > 0 && msgByteLength > markdownOpts.MaxMessageSize {
// truncation relies on rune length
q := float64(msgRuneLength) / float64(msgByteLength)
truncateLength := msgRuneLength - int(q*float64(markdownOpts.MaxMessageSize))
newLength := utf8.RuneCountInString(diffMsg) - truncateLength - 1000
opts.diffMsg = truncateMiddle(diffMsg, newLength, "\n\n...(truncated due to message size limit)...\n\n")
opts.originalSize = msgRuneLength
return ToMarkdown(out, opts, markdownOpts)
}
return MarkdownOutput{Msg: msg, RuneLen: msgRuneLength, OriginalMsgSize: originalSize}, nil
}
func hasCodeChanges(options Options, project Project) bool {
return options.ShowOnlyChanges && project.Metadata.VCSCodeChanged != nil && *project.Metadata.VCSCodeChanged
}
var (
configFileDocsURL = "https://www.infracost.io/docs/features/config_file/"
usageFileDocsURL = "https://www.infracost.io/docs/features/usage_based_resources/#infracost-usageyml"
usageDefaultsDocsURL = "https://www.infracost.io/docs/features/usage_based_resources"
usageDefaultsDashboardSuffix = "settings/usage-defaults"
cloudURLRegex = regexp.MustCompile(`(https?://[^/]+/org/[^/]+/)`)
)
func usageCostsMessage(out Root, useLinks bool) string {
if !out.Metadata.UsageApiEnabled {
return handleUsageApiDisabledMessage(out, useLinks)
}
return handleUsageApiEnabledMessage(out, useLinks)
}
func usageDefaultsStr(out Root, useMarkdownLinks bool) string {
if !useMarkdownLinks {
return "usage defaults from Infracost Cloud"
}
usageDefaultsURL := usageDefaultsDocsURL
match := cloudURLRegex.FindStringSubmatch(out.CloudURL)
if len(match) > 0 {
usageDefaultsURL = match[0] + usageDefaultsDashboardSuffix
}
return fmt.Sprintf("[usage defaults](%s)", usageDefaultsURL)
}
func configFilesStr(useMarkdownLinks bool) string {
if !useMarkdownLinks {
return "config files"
}
return fmt.Sprintf("[config files](%s)", configFileDocsURL)
}
func usageFileStr(name string, useMarkdownLinks bool) string {
if !useMarkdownLinks {
return name
}
return fmt.Sprintf("[%s](%s)", name, usageFileDocsURL)
}
func handleUsageApiEnabledMessage(out Root, useMarkdownLinks bool) string {
if out.Metadata.ConfigFilePath != "" {
return constructConfigFilePathMessage(out, useMarkdownLinks)
}
if out.Metadata.UsageFilePath != "" {
return fmt.Sprintf("*Usage costs were estimated by merging %s and values from %s.", usageDefaultsStr(out, useMarkdownLinks), usageFileStr(out.Metadata.UsageFilePath, useMarkdownLinks))
}
return fmt.Sprintf("*Usage costs were estimated with %s, which can also be %s in this repo.", usageDefaultsStr(out, useMarkdownLinks), usageFileStr("overridden", useMarkdownLinks))
}
func constructConfigFilePathMessage(out Root, useMarkdownLinks bool) string {
if out.Metadata.ConfigFileHasUsageFile {
return fmt.Sprintf("*Usage costs were estimated by merging %s and values from the %s.", usageDefaultsStr(out, useMarkdownLinks), configFilesStr(useMarkdownLinks))
}
return fmt.Sprintf("*Usage costs were estimated with %s, which can also be overridden in an %s.", usageDefaultsStr(out, useMarkdownLinks), usageFileStr("infracost-usage.yml", useMarkdownLinks))
}
func handleUsageApiDisabledMessage(out Root, useMarkdownLinks bool) string {
if out.Metadata.ConfigFilePath != "" {
if out.Metadata.ConfigFileHasUsageFile {
return fmt.Sprintf("*Usage costs were estimated using values from the %s.", configFilesStr(useMarkdownLinks))
}
return fmt.Sprintf("*Usage costs can be estimated by adding %s or %s in the config file.", usageDefaultsStr(out, useMarkdownLinks), usageFileStr("usage files", useMarkdownLinks))
}
if out.Metadata.UsageFilePath != "" {
return fmt.Sprintf("*Usage costs were estimated using %s.", usageFileStr(out.Metadata.UsageFilePath, useMarkdownLinks))
}
return fmt.Sprintf("*Usage costs can be estimated by adding %s or an %s.", usageDefaultsStr(out, useMarkdownLinks), usageFileStr("infracost-usage.yml", useMarkdownLinks))
}
func costsDetailsMessage(out Root) string {
var msgs []string
if out.Summary != nil && out.Summary.TotalUnsupportedResources != nil && *out.Summary.TotalUnsupportedResources > 0 {
msgs = append(msgs, "unsupported resources")
}
for _, p := range out.Projects {
if len(p.Metadata.Errors) > 0 {
msgs = append(msgs, "skipped projects due to errors")
break
}
}
if len(msgs) == 0 {
return ""
}
return fmt.Sprintf("(includes details of %s)", strings.Join(msgs, " and "))
}