/
x_billing.go
96 lines (78 loc) · 2.62 KB
/
x_billing.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
// Copyright (C) 2015 Scaleway. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.md file.
package cli
import (
"fmt"
"math/big"
"strings"
"text/tabwriter"
"time"
"github.com/Sirupsen/logrus"
"github.com/docker/go-units"
"github.com/scaleway/scaleway-cli/pkg/commands"
"github.com/scaleway/scaleway-cli/pkg/pricing"
"github.com/scaleway/scaleway-cli/pkg/utils"
)
var cmdBilling = &Command{
Exec: runBilling,
UsageLine: "_billing [OPTIONS]",
Description: "",
Hidden: true,
Help: "Get resources billing estimation",
}
func init() {
cmdBilling.Flag.BoolVar(&billingHelp, []string{"h", "-help"}, false, "Print usage")
cmdBilling.Flag.BoolVar(&billingNoTrunc, []string{"-no-trunc"}, false, "Don't truncate output")
}
// BillingArgs are flags for the `RunBilling` function
type BillingArgs struct {
NoTrunc bool
}
// Flags
var billingHelp bool // -h, --help flag
var billingNoTrunc bool // --no-trunc flag
func runBilling(cmd *Command, rawArgs []string) error {
if billingHelp {
return cmd.PrintUsage()
}
if len(rawArgs) > 0 {
return cmd.PrintShortUsage()
}
// cli parsing
args := commands.PsArgs{
NoTrunc: billingNoTrunc,
}
ctx := cmd.GetContext(rawArgs)
logrus.Warn("")
logrus.Warn("Warning: 'scw _billing' is a work-in-progress price estimation tool")
logrus.Warn("For real usage, visit https://cloud.scaleway.com/#/billing")
logrus.Warn("")
// table
w := tabwriter.NewWriter(ctx.Stdout, 20, 1, 3, ' ', 0)
defer w.Flush()
fmt.Fprintf(w, "ID\tNAME\tSTARTED\tMONTH PRICE\n")
// servers
servers, err := cmd.API.GetServers(true, 0)
if err != nil {
return err
}
totalMonthPrice := new(big.Rat)
for _, server := range *servers {
if server.State != "running" {
continue
}
commercialType := strings.ToLower(server.CommercialType)
shortID := utils.TruncIf(server.Identifier, 8, !args.NoTrunc)
shortName := utils.TruncIf(utils.Wordify(server.Name), 25, !args.NoTrunc)
modificationTime, _ := time.Parse("2006-01-02T15:04:05.000000+00:00", server.ModificationDate)
modificationAgo := time.Now().UTC().Sub(modificationTime)
shortModificationDate := units.HumanDuration(modificationAgo)
usage := pricing.NewUsageByPath(fmt.Sprintf("/compute/%s/run", commercialType))
usage.SetStartEnd(modificationTime, time.Now().UTC())
totalMonthPrice = totalMonthPrice.Add(totalMonthPrice, usage.Total())
fmt.Fprintf(w, "server/%s/%s\t%s\t%s\t%s\n", commercialType, shortID, shortName, shortModificationDate, usage.TotalString())
}
fmt.Fprintf(w, "TOTAL\t\t\t%s\n", pricing.PriceString(totalMonthPrice, "EUR"))
return nil
}