Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #14 from rocketmiles/csv
Browse files Browse the repository at this point in the history
  • Loading branch information
bradq committed Oct 11, 2021
2 parents 0e778f6 + 94651da commit ad8b1ae
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ This is a simple CLI tool that wraps the AWS Cost Explorer APIs to be able to qu

### Homebrew

```
```bash
brew tap rocketmiles/aws-cct https://github.com/rocketmiles/aws-cct
brew install aws-cct
```

Updating
```
```bash
brew upgrade aws-cct
```

Expand All @@ -27,7 +27,7 @@ Download from the [releases](https://github.com/rocketmiles/aws-cct/releases)

### Go Get

```
```bash
go get github.com/rocketmiles/aws-cct
```

Expand All @@ -46,52 +46,60 @@ COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--start value First month to compare (2020-01-01) (default: "2020-12-01")
--end value Second month to compare (2020-02-01) (default: "2021-01-01")
--start value First month to compare (2020-01-01) (default: "2021-09-01")
--end value Second month to compare (2020-02-01) (default: "2021-10-01")
--cost-metric value Cost Metric to compare (NetAmortizedCost, UnblendedCost, etc.) (default: "NetAmortizedCost")
--service value Define a service to dig into
--tag value Tag value to filter results (app=web, env=prod, etc.)
--sort value Column to sort results on (name, start, end, delta, deltapercent) (default: "name")
--sort-order value Order to sort in (asc or desc) (default: "asc")
--output value Output format (supported formats: table, csv) (default: "table")
--help, -h show help (default: false)
```

*Simple usage*
```
```bash
aws-cct
```

*Check Unblended Costs*
```
```bash
aws-cct --cost-metric UnblendedCost
```

*Dig into EC2 costs*

You can get the string from the initial output. Simply copy the value in the "SERVICE" section and you can filter into that
```
```bash
aws-cct --service "Amazon Elastic Compute Cloud - Compute"
```

*Filter by tags*

You can get filter costs by tag, to return costs for resources that match all specified tag values.
```
```bash
aws-cct --tag app=widgetizer --tag env=production
```

*Compare Older Months*
```
```bash
aws-cct --start 2020-08-01 --end 2020-09-01
```

*Sort on a column*

You can sort on any column, ascending or descending, for example to see the largest deltas first.
```
```bash
aws-cct --sort delta --sort-order desc
```

*Output in CSV format*

This will output in a CSV friendly format and you can utilize this to do analysis or for reporting.
```bash
aws-cct --output csv
```

## Local Development

Requires Go >= 1.15.3
Expand Down
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGw
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/leekchan/accounting v1.0.0 h1:+Wd7dJ//dFPa28rc1hjyy+qzCbXPMR91Fb6F1VGTQHg=
github.com/leekchan/accounting v1.0.0/go.mod h1:3timm6YPhY3YDaGxl0q3eaflX0eoSx3FXn7ckHe4tO0=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
Expand All @@ -34,7 +35,6 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/urfave/cli v1.22.4 h1:u7tSpNPPswAFymm8IehJhy4uJMlUuU/GmqSkvJ1InXA=
github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4=
github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
31 changes: 25 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func main() {
var serviceFilter string
var sortColumn string
var sortOrder string
var output string
currentDate := time.Now()
thisMonthFirst := time.Date(currentDate.Year(), currentDate.Month(), 1, 0, 0, 0, 0, time.UTC)
previousMonthFirst := thisMonthFirst.AddDate(0, -1, 0)
Expand Down Expand Up @@ -61,8 +62,8 @@ func main() {
Destination: &serviceFilter,
},
&cli.StringSliceFlag{
Name: "tag",
Usage: "Tag value to filter results (app=web, env=prod, etc.)",
Name: "tag",
Usage: "Tag value to filter results (app=web, env=prod, etc.)",
},
&cli.StringFlag{
Name: "sort",
Expand All @@ -76,9 +77,16 @@ func main() {
Usage: "Order to sort in (asc or desc)",
Destination: &sortOrder,
},
&cli.StringFlag{
Name: "output",
Value: "table",
Usage: "Output format (supported formats: table, csv)",
Destination: &output,
},
},
Action: func(c *cli.Context) error {
ac := accounting.Accounting{Symbol: "$", Precision: 2}
var ac *accounting.Accounting
ac = accounting.DefaultAccounting("$", 2)
sess, _ := session.NewSession()
svc := costexplorer.New(sess)

Expand Down Expand Up @@ -176,6 +184,11 @@ func main() {
secondMonthHeader += " (projection)"
}

// Remove commas to make output compatible for CSVs
if output == "csv" {
ac.SetThousandSeparator("")
}

tw.AppendHeader(table.Row{"Service", firstMonthStart, secondMonthHeader, "Delta", "Delta Percent"})
for _, serviceCosts := range finalResultsCosts {
tw.AppendRow(table.Row{serviceCosts.serviceName, ac.FormatMoney(serviceCosts.amount), ac.FormatMoney(serviceCosts.secondAmount), ac.FormatMoney(serviceCosts.delta), fmt.Sprintf("%s%%%%", accounting.FormatNumber(serviceCosts.deltaPercent, 1, "", "."))})
Expand All @@ -192,9 +205,15 @@ func main() {
{Name: "Delta Percent", Align: text.AlignRight, AlignFooter: text.AlignRight},
})

fmt.Printf("\n")
fmt.Printf(tw.Render())
fmt.Printf("\n")
switch output {
case "csv":
fmt.Printf(tw.RenderCSV())
default:
fmt.Printf("\n")
fmt.Printf(tw.Render())
fmt.Printf("\n")
}

return nil
},
}
Expand Down

0 comments on commit ad8b1ae

Please sign in to comment.