PromptRegion fetches regions from the API and prompts the user to select one.
( io *iostreams.IOStreams, client *dashboard.Client, accessToken string, )
| 68 | |
| 69 | // PromptRegion fetches regions from the API and prompts the user to select one. |
| 70 | func PromptRegion( |
| 71 | io *iostreams.IOStreams, |
| 72 | client *dashboard.Client, |
| 73 | accessToken string, |
| 74 | ) (string, error) { |
| 75 | io.StartProgressIndicatorWithLabel("Fetching regions") |
| 76 | regions, err := client.ListRegions(accessToken) |
| 77 | io.StopProgressIndicator() |
| 78 | if err != nil { |
| 79 | return "", fmt.Errorf("failed to fetch regions: %w", err) |
| 80 | } |
| 81 | |
| 82 | if len(regions) == 0 { |
| 83 | return "", fmt.Errorf("no regions available") |
| 84 | } |
| 85 | |
| 86 | regionOptions := make([]string, len(regions)) |
| 87 | for i, r := range regions { |
| 88 | if r.Name != "" { |
| 89 | regionOptions[i] = fmt.Sprintf("%s (%s)", r.Code, r.Name) |
| 90 | } else { |
| 91 | regionOptions[i] = r.Code |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | var selected int |
| 96 | err = prompt.SurveyAskOne( |
| 97 | &survey.Select{ |
| 98 | Message: "Region:", |
| 99 | Options: regionOptions, |
| 100 | }, |
| 101 | &selected, |
| 102 | ) |
| 103 | if err != nil { |
| 104 | return "", err |
| 105 | } |
| 106 | |
| 107 | return regions[selected].Code, nil |
| 108 | } |
| 109 | |
| 110 | // CreateAndFetchApplication creates an application (with region retry) and |
| 111 | // generates an API key for it. It returns the region the application was |
no test coverage detected