-
Notifications
You must be signed in to change notification settings - Fork 224
/
regions.go
65 lines (51 loc) · 1.25 KB
/
regions.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
package platform
import (
"context"
"fmt"
"sort"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/internal/client"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/render"
)
func newRegions() (cmd *cobra.Command) {
const (
long = `View a list of regions where Fly has edges and/or datacenters
`
short = "List regions"
)
cmd = command.New("regions", short, long, runRegions,
command.RequireSession,
)
cmd.Args = cobra.NoArgs
return
}
func runRegions(ctx context.Context) error {
client := client.FromContext(ctx).API()
regions, _, err := client.PlatformRegions(ctx)
if err != nil {
return fmt.Errorf("failed retrieving regions: %w", err)
}
sort.Slice(regions, func(i, j int) bool {
return regions[i].Code < regions[j].Code
})
out := iostreams.FromContext(ctx).Out
if config.FromContext(ctx).JSONOutput {
return render.JSON(out, regions)
}
var rows [][]string
for _, region := range regions {
gateway := ""
if region.GatewayAvailable {
gateway = "✓"
}
rows = append(rows, []string{
region.Code,
region.Name,
gateway,
})
}
return render.Table(out, "", rows, "Code", "Name", "Gateway")
}