github.com/scaleway/scaleway-cli@v1.11.1/pkg/cli/x_billing.go (about)

     1  // Copyright (C) 2015 Scaleway. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE.md file.
     4  
     5  package cli
     6  
     7  import (
     8  	"fmt"
     9  	"math/big"
    10  	"strings"
    11  	"text/tabwriter"
    12  	"time"
    13  
    14  	"github.com/Sirupsen/logrus"
    15  	"github.com/docker/go-units"
    16  	"github.com/scaleway/scaleway-cli/pkg/commands"
    17  	"github.com/scaleway/scaleway-cli/pkg/pricing"
    18  	"github.com/scaleway/scaleway-cli/pkg/utils"
    19  )
    20  
    21  var cmdBilling = &Command{
    22  	Exec:        runBilling,
    23  	UsageLine:   "_billing [OPTIONS]",
    24  	Description: "",
    25  	Hidden:      true,
    26  	Help:        "Get resources billing estimation",
    27  }
    28  
    29  func init() {
    30  	cmdBilling.Flag.BoolVar(&billingHelp, []string{"h", "-help"}, false, "Print usage")
    31  	cmdBilling.Flag.BoolVar(&billingNoTrunc, []string{"-no-trunc"}, false, "Don't truncate output")
    32  }
    33  
    34  // BillingArgs are flags for the `RunBilling` function
    35  type BillingArgs struct {
    36  	NoTrunc bool
    37  }
    38  
    39  // Flags
    40  var billingHelp bool    // -h, --help flag
    41  var billingNoTrunc bool // --no-trunc flag
    42  
    43  func runBilling(cmd *Command, rawArgs []string) error {
    44  	if billingHelp {
    45  		return cmd.PrintUsage()
    46  	}
    47  	if len(rawArgs) > 0 {
    48  		return cmd.PrintShortUsage()
    49  	}
    50  
    51  	// cli parsing
    52  	args := commands.PsArgs{
    53  		NoTrunc: billingNoTrunc,
    54  	}
    55  	ctx := cmd.GetContext(rawArgs)
    56  
    57  	logrus.Warn("")
    58  	logrus.Warn("Warning: 'scw _billing' is a work-in-progress price estimation tool")
    59  	logrus.Warn("For real usage, visit https://cloud.scaleway.com/#/billing")
    60  	logrus.Warn("")
    61  
    62  	// table
    63  	w := tabwriter.NewWriter(ctx.Stdout, 20, 1, 3, ' ', 0)
    64  	defer w.Flush()
    65  	fmt.Fprintf(w, "ID\tNAME\tSTARTED\tMONTH PRICE\n")
    66  
    67  	// servers
    68  	servers, err := cmd.API.GetServers(true, 0)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	totalMonthPrice := new(big.Rat)
    74  
    75  	for _, server := range *servers {
    76  		if server.State != "running" {
    77  			continue
    78  		}
    79  		commercialType := strings.ToLower(server.CommercialType)
    80  		shortID := utils.TruncIf(server.Identifier, 8, !args.NoTrunc)
    81  		shortName := utils.TruncIf(utils.Wordify(server.Name), 25, !args.NoTrunc)
    82  		modificationTime, _ := time.Parse("2006-01-02T15:04:05.000000+00:00", server.ModificationDate)
    83  		modificationAgo := time.Now().UTC().Sub(modificationTime)
    84  		shortModificationDate := units.HumanDuration(modificationAgo)
    85  		usage := pricing.NewUsageByPath(fmt.Sprintf("/compute/%s/run", commercialType))
    86  		usage.SetStartEnd(modificationTime, time.Now().UTC())
    87  
    88  		totalMonthPrice = totalMonthPrice.Add(totalMonthPrice, usage.Total())
    89  
    90  		fmt.Fprintf(w, "server/%s/%s\t%s\t%s\t%s\n", commercialType, shortID, shortName, shortModificationDate, usage.TotalString())
    91  	}
    92  
    93  	fmt.Fprintf(w, "TOTAL\t\t\t%s\n", pricing.PriceString(totalMonthPrice, "EUR"))
    94  
    95  	return nil
    96  }