-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.go
70 lines (55 loc) · 1.63 KB
/
text.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
package formats
import (
"bufio"
"fmt"
"github.com/dazfuller/azcosts/internal/model"
"os"
"strings"
)
type TextFormatter struct {
useStdOut bool
outputPath string
}
func MakeTextFormatter(useStdOut bool, outputPath string) (TextFormatter, error) {
if err := validateOptions(useStdOut, outputPath); err != nil {
return TextFormatter{}, err
}
return TextFormatter{useStdOut: useStdOut, outputPath: outputPath}, nil
}
func (tf TextFormatter) Generate(costs []model.ResourceGroupSummary) error {
var writer *bufio.Writer
if tf.useStdOut {
writer = bufio.NewWriter(os.Stdout)
} else {
file, err := os.Create(tf.outputPath)
if err != nil {
return err
}
writer = bufio.NewWriter(file)
}
writer.WriteString(fmt.Sprintf("%-70s %-30s", "Resource Group", "Subscription"))
for _, bp := range costs[0].Costs {
writer.WriteString(fmt.Sprintf(" %12s", bp.Period))
}
writer.WriteString(fmt.Sprintf("%12s\n", "Total Costs"))
writer.WriteString(fmt.Sprintf("%-70s %-30s", strings.Repeat("=", 70), strings.Repeat("=", 30)))
for range costs[0].Costs {
writer.WriteString(fmt.Sprintf(" %12s", strings.Repeat("=", 12)))
}
writer.WriteString(fmt.Sprintf(" %12s\n", strings.Repeat("=", 12)))
for _, rg := range costs {
writer.WriteString(fmt.Sprintf("%-70s %-30s", trimValue(rg.Name, 50), trimValue(rg.SubscriptionName, 30)))
for _, cost := range rg.Costs {
writer.WriteString(fmt.Sprintf(" %12.2f", cost.Total))
}
writer.WriteString(fmt.Sprintf(" %12.2f\n", rg.TotalCost))
}
writer.Flush()
return nil
}
func trimValue(value string, maxLen int) string {
if len(value) > maxLen {
return value[0:maxLen]
}
return value
}