Skip to content

Commit d55c0b6

Browse files
committed
Printing billing usage for servers
1 parent d1e200a commit d55c0b6

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

pkg/cli/x_billing.go

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@
44

55
package cli
66

7-
import "fmt"
7+
import (
8+
"fmt"
9+
"text/tabwriter"
10+
"time"
11+
12+
"github.com/scaleway/scaleway-cli/pkg/commands"
13+
"github.com/scaleway/scaleway-cli/pkg/pricing"
14+
"github.com/scaleway/scaleway-cli/pkg/utils"
15+
"github.com/scaleway/scaleway-cli/vendor/github.com/docker/docker/pkg/units"
16+
)
817

918
var cmdBilling = &Command{
1019
Exec: runBilling,
@@ -16,20 +25,56 @@ var cmdBilling = &Command{
1625

1726
func init() {
1827
cmdBilling.Flag.BoolVar(&billingHelp, []string{"h", "-help"}, false, "Print usage")
28+
cmdBilling.Flag.BoolVar(&billingNoTrunc, []string{"-no-trunc"}, false, "Don't truncate output")
29+
}
30+
31+
// BillingArgs are flags for the `RunBilling` function
32+
type BillingArgs struct {
33+
NoTrunc bool
1934
}
2035

2136
// Flags
22-
var billingHelp bool // -h, --help flag
37+
var billingHelp bool // -h, --help flag
38+
var billingNoTrunc bool // --no-trunc flag
2339

24-
func runBilling(cmd *Command, args []string) error {
40+
func runBilling(cmd *Command, rawArgs []string) error {
2541
if billingHelp {
2642
return cmd.PrintUsage()
2743
}
28-
if len(args) > 0 {
44+
if len(rawArgs) > 0 {
2945
return cmd.PrintShortUsage()
3046
}
3147

32-
fmt.Println("BILLING")
48+
// cli parsing
49+
args := commands.PsArgs{
50+
NoTrunc: billingNoTrunc,
51+
}
52+
ctx := cmd.GetContext(rawArgs)
53+
54+
// table
55+
w := tabwriter.NewWriter(ctx.Stdout, 20, 1, 3, ' ', 0)
56+
defer w.Flush()
57+
fmt.Fprintf(w, "ID\tNAME\tSTARTED\tMONTH PRICE\n")
58+
59+
// servers
60+
servers, err := cmd.API.GetServers(true, 0)
61+
if err != nil {
62+
return err
63+
}
64+
for _, server := range *servers {
65+
if server.State != "running" {
66+
continue
67+
}
68+
shortID := utils.TruncIf(server.Identifier, 8, !args.NoTrunc)
69+
shortName := utils.TruncIf(utils.Wordify(server.Name), 25, !args.NoTrunc)
70+
modificationTime, _ := time.Parse("2006-01-02T15:04:05.000000+00:00", server.ModificationDate)
71+
modificationAgo := time.Now().UTC().Sub(modificationTime)
72+
shortModificationDate := units.HumanDuration(modificationAgo)
73+
usage := pricing.NewUsageByPath("/compute/c1/run")
74+
usage.SetStartEnd(modificationTime, time.Now().UTC())
75+
76+
fmt.Fprintf(w, "server/%s\t%s\t%s\t%s\n", shortID, shortName, shortModificationDate, usage.TotalString())
77+
}
3378

3479
return nil
3580
}

pkg/pricing/usage.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package pricing
22

33
import (
4+
"fmt"
45
"math/big"
56
"time"
7+
8+
"github.com/dustin/go-humanize"
69
)
710

811
type Usage struct {
@@ -77,3 +80,9 @@ func (u *Usage) Total() *big.Rat {
7780
total := new(big.Rat).Mul(u.BillableQuantity(), u.PricingObject.UnitPrice)
7881
return ratMin(total, u.PricingObject.UnitPriceCap)
7982
}
83+
84+
func (u *Usage) TotalString() string {
85+
total := u.Total()
86+
floatVal, _ := total.Float64()
87+
return fmt.Sprintf("%s %s", humanize.Ftoa(floatVal), u.PricingObject.Currency)
88+
}

0 commit comments

Comments
 (0)