forked from digitalocean/doctl
/
size.go
56 lines (48 loc) · 1.33 KB
/
size.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
package main
import (
"fmt"
"os"
"github.com/digitalocean/doctl/Godeps/_workspace/src/github.com/codegangsta/cli"
"github.com/digitalocean/doctl/Godeps/_workspace/src/github.com/digitalocean/godo"
"github.com/digitalocean/doctl/Godeps/_workspace/src/golang.org/x/oauth2"
)
var SizeCommand = cli.Command{
Name: "size",
Usage: "Size commands.",
Action: sizeList,
Subcommands: []cli.Command{
{
Name: "list",
Aliases: []string{"s"},
Usage: "List all sizes.",
Action: sizeList,
},
},
}
func sizeList(ctx *cli.Context) {
if ctx.BoolT("help") == true {
cli.ShowAppHelp(ctx)
os.Exit(1)
}
tokenSource := &TokenSource{
AccessToken: APIKey,
}
oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
client := godo.NewClient(oauthClient)
opt := &godo.ListOptions{
Page: 1,
PerPage: 50, // Not likely to have more than 50 sizes soon
}
sizeList, _, err := client.Sizes.List(opt)
if err != nil {
fmt.Printf("Unable to list Sizes: %s\n", err)
os.Exit(1)
}
cliOut := NewCLIOutput()
defer cliOut.Flush()
cliOut.Header("Slug", "Memory", "VCPUs", "Disk", "Transfer", "Price Monthly", "Price Hourly")
for _, size := range sizeList {
cliOut.Writeln("%s\t%dMB\t%d\t%dGB\t%d\t$%.0f\t$%.5f\n",
size.Slug, size.Memory, size.Vcpus, size.Disk, size.Transfer, size.PriceMonthly, size.PriceHourly)
}
}