Skip to content

Commit

Permalink
chore: add total line to cost
Browse files Browse the repository at this point in the history
  • Loading branch information
flowerinthenight committed Apr 18, 2023
1 parent 9afff40 commit 32f3c08
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cmds/cost/aws/usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ func get(cmd *cobra.Command, args []string, fl *Flags) {
table.SetColumnAlignment(colsAlign)
table.Append(cols)
var render bool
var totalUsage, totalCost float64

for {
v, err := stream.Recv()
Expand Down Expand Up @@ -354,6 +355,8 @@ func get(cmd *cobra.Command, args []string, fl *Flags) {
refCols[14].val = v.Aws.CostCategories
refCols[15].val = v.Aws.Usage
refCols[16].val = v.Aws.Cost
totalUsage += v.Aws.Usage
totalCost += v.Aws.Cost
row := []string{}
for _, rc := range refCols {
if rc.enable {
Expand All @@ -372,6 +375,19 @@ func get(cmd *cobra.Command, args []string, fl *Flags) {
}

if render {
// Add the total line.
totalLine := []string{}
if (len(cols) - 3) > 0 {
for i := 0; i < len(cols)-3; i++ {
totalLine = append(totalLine, "")
}
}

totalLine = append(totalLine, "TOTAL")
totalLine = append(totalLine, fmt.Sprintf("%.10f", totalUsage))
totalLine = append(totalLine, fmt.Sprintf("%.10f", totalCost))
table.Append(totalLine)

fmt.Printf("\033[2K\r") // reset cursor
table.Render()
}
Expand Down

0 comments on commit 32f3c08

Please sign in to comment.