forked from digitalocean/doctl
/
region.go
55 lines (47 loc) · 1.22 KB
/
region.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
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 RegionCommand = cli.Command{
Name: "region",
Usage: "Region commands.",
Action: regionList,
Subcommands: []cli.Command{
{
Name: "list",
Aliases: []string{"l"},
Usage: "List All Regions.",
Action: regionList,
},
},
}
func regionList(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 regions soon
}
regionList, _, err := client.Regions.List(opt)
if err != nil {
fmt.Printf("Unable to list Regions: %s\n", err)
os.Exit(1)
}
cliOut := NewCLIOutput()
defer cliOut.Flush()
cliOut.Header("Name", "Slug", "Available")
for _, region := range regionList {
cliOut.Writeln("%s\t%s\t%t\n", region.Name, region.Slug, region.Available)
}
}