MCPcopy
hub / github.com/cli/cli / getMachineName

Function getMachineName

pkg/cmd/codespace/create.go:514–572  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

512
513// getMachineName prompts the user to select the machine type, or validates the machine if non-empty.
514func 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

Callers 1

CreateMethod · 0.85

Calls 4

buildDisplayNameFunction · 0.85
GetCodespacesMachinesMethod · 0.65
ErrorfMethod · 0.65
AskMethod · 0.65

Tested by

no test coverage detected