getMachineName prompts the user to select the machine type, or validates the machine if non-empty.
(ctx context.Context, apiClient apiClient, prompter SurveyPrompter, repoID int, machine, branch, location string, devcontainerPath string)
| 512 | |
| 513 | // getMachineName prompts the user to select the machine type, or validates the machine if non-empty. |
| 514 | func getMachineName(ctx context.Context, apiClient apiClient, prompter SurveyPrompter, repoID int, machine, branch, location string, devcontainerPath string) (string, error) { |
| 515 | machines, err := apiClient.GetCodespacesMachines(ctx, repoID, branch, location, devcontainerPath) |
| 516 | if err != nil { |
| 517 | return "", fmt.Errorf("error requesting machine instance types: %w", err) |
| 518 | } |
| 519 | |
| 520 | // if user supplied a machine type, it must be valid |
| 521 | // if no machine type was supplied, we don't error if there are no machine types for the current repo |
| 522 | if machine != "" { |
| 523 | for _, m := range machines { |
| 524 | if machine == m.Name { |
| 525 | return machine, nil |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | availableMachines := make([]string, len(machines)) |
| 530 | for i := 0; i < len(machines); i++ { |
| 531 | availableMachines[i] = machines[i].Name |
| 532 | } |
| 533 | |
| 534 | return "", fmt.Errorf("there is no such machine for the repository: %s\nAvailable machines: %v", machine, availableMachines) |
| 535 | } else if len(machines) == 0 { |
| 536 | return "", nil |
| 537 | } |
| 538 | |
| 539 | if len(machines) == 1 { |
| 540 | // VS Code does not prompt for machine if there is only one, this makes us consistent with that behavior |
| 541 | return machines[0].Name, nil |
| 542 | } |
| 543 | |
| 544 | machineNames := make([]string, 0, len(machines)) |
| 545 | machineByName := make(map[string]*api.Machine) |
| 546 | for _, m := range machines { |
| 547 | machineName := buildDisplayName(m.DisplayName, m.PrebuildAvailability) |
| 548 | machineNames = append(machineNames, machineName) |
| 549 | machineByName[machineName] = m |
| 550 | } |
| 551 | |
| 552 | machineSurvey := []*survey.Question{ |
| 553 | { |
| 554 | Name: "machine", |
| 555 | Prompt: &survey.Select{ |
| 556 | Message: "Choose Machine Type:", |
| 557 | Options: machineNames, |
| 558 | Default: machineNames[0], |
| 559 | }, |
| 560 | Validate: survey.Required, |
| 561 | }, |
| 562 | } |
| 563 | |
| 564 | var machineAnswers struct{ Machine string } |
| 565 | if err := prompter.Ask(machineSurvey, &machineAnswers); err != nil { |
| 566 | return "", fmt.Errorf("error getting machine: %w", err) |
| 567 | } |
| 568 | |
| 569 | selectedMachine := machineByName[machineAnswers.Machine] |
| 570 | |
| 571 | return selectedMachine.Name, nil |
no test coverage detected