-
Notifications
You must be signed in to change notification settings - Fork 221
/
regions.go
71 lines (59 loc) · 1.57 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
66
67
68
69
70
71
package lfsc
import (
"context"
"fmt"
"sort"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/lfsc-go"
)
func newRegions() (cmd *cobra.Command) {
const (
long = `View a list of LiteFS Cloud regions`
short = "List LiteFS Cloud regions"
)
cmd = command.New("regions", short, long, runRegions,
command.RequireSession,
)
cmd.Args = cobra.NoArgs
flag.Add(cmd,
urlFlag(),
flag.JSONOutput())
return
}
func runRegions(ctx context.Context) error {
client := client.FromContext(ctx).API()
flyRegions, _, err := client.PlatformRegions(ctx)
if err != nil {
return fmt.Errorf("failed retrieving regions: %w", err)
}
lfscClient := lfsc.NewClient()
lfscClient.URL = flag.GetString(ctx, "url")
regions, err := lfscClient.Regions(ctx)
if err != nil {
return fmt.Errorf("failed retrieving litefs cloud regions: %w", err)
}
sort.Slice(regions, func(i, j int) bool {
return regions[i] < regions[j]
})
out := iostreams.FromContext(ctx).Out
if config.FromContext(ctx).JSONOutput {
return render.JSON(out, regions)
}
var rows [][]string
for _, region := range regions {
var regionName string
for _, flyRegion := range flyRegions {
if flyRegion.Code == region {
regionName = flyRegion.Name
}
}
rows = append(rows, []string{regionName, region})
}
return render.Table(out, "", rows, "Name", "Code")
}