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