-
Notifications
You must be signed in to change notification settings - Fork 234
/
regions.go
45 lines (37 loc) · 1.21 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
package launch
import (
"context"
"fmt"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/prompt"
)
func getRegionByCode(ctx context.Context, regionCode string) (*api.Region, error) {
apiClient := client.FromContext(ctx).API()
allRegions, _, err := apiClient.PlatformRegions(ctx)
if err != nil {
return nil, err
}
for _, r := range allRegions {
if r.Code == regionCode {
return &r, nil
}
}
return nil, fmt.Errorf("Unknown region '%s'. Run `fly platform regions` to see valid names", regionCode)
}
// computeRegionToUse looks at --region flag, existing fly.toml primary_region and as last
// meassure asks the user from a list of valid platform regions which one to use
func computeRegionToUse(ctx context.Context, appConfig *appconfig.Config, paidPlan bool) (*api.Region, error) {
regionCode := flag.GetRegion(ctx)
if regionCode == "" {
regionCode = appConfig.PrimaryRegion
}
if regionCode != "" {
return getRegionByCode(ctx, regionCode)
}
return prompt.Region(ctx, !paidPlan,
prompt.RegionParams{Message: "Choose a region for deployment:"},
)
}