This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
105 lines (89 loc) · 2.66 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"log"
"strings"
"github.com/googleinterns/terraform-cost-estimation/billing"
"github.com/googleinterns/terraform-cost-estimation/io"
"github.com/googleinterns/terraform-cost-estimation/jsdecode"
res "github.com/googleinterns/terraform-cost-estimation/resources"
cd "github.com/googleinterns/terraform-cost-estimation/resources/classdetail"
)
var (
output = flag.String("output", "stdout", `Write the cost estimations to the given paths.
If set to 'stdout', all the outputs will be shown in the command line.
Multiple output file names must be delimited by ','.
Mixed file names and stdout values are allowed.`)
format = flag.String("format", "txt", `Write the pricing information in the specified format.
Can be set to: txt, json, html.`)
)
func minInt(x, y int) int {
if x < y {
return x
}
return y
}
func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: go run main.go [OPTIONS] FILE\n\n")
fmt.Fprintf(flag.CommandLine.Output(), "Outputs the cost estimation of Terraform resources from a JSON plan file.")
fmt.Fprintf(flag.CommandLine.Output(), "\n\nOptions:\n")
flag.PrintDefaults()
}
flag.Parse()
if len(flag.Args()) == 0 {
log.Fatal("Error: No input file.")
}
outputs := strings.Split(*output, ",")
if *output != "stdout" {
if len(outputs) != len(flag.Args()) {
log.Fatal("Error: Input and output files number differ.")
}
}
catalog, err := billing.NewComputeEngineCatalog(context.Background())
if err != nil {
log.Fatalf("Error: %v", err)
}
classDetails, err := cd.NewResourceDetail()
if err != nil {
log.Fatalf("Error: %+v", err)
}
for i, inputName := range flag.Args() {
plan, err := io.GetPlan(inputName)
if err != nil {
log.Fatalf("Error: %v", err)
}
resources := jsdecode.GetResources(classDetails, plan)
finalResources := []res.ResourceState{}
for _, r := range resources {
if err = r.CompletePricingInfo(catalog); err != nil {
log.Printf("In file %s got error: %v", inputName, err)
continue
}
finalResources = append(finalResources, r)
}
outputName := outputs[minInt(i, len(outputs)-1)]
fout, err := io.GetOutputWriter(outputName)
if err != nil {
log.Fatalf("Error: %v", err)
}
switch {
case *format == "json":
if err = io.GenerateJsonOut(fout, finalResources); err != nil {
log.Printf("Error: %v", err)
}
case *format == "html":
if err = io.GenerateWebPage(fout, finalResources); err != nil {
log.Printf("Error: %v", err)
}
case *format == "txt":
io.OutputPricing(finalResources, fout)
default:
}
if err = io.FinishOutput(fout); err != nil {
log.Fatalf("Error: %v", err)
}
}
}