MCPcopy Create free account
hub / github.com/cli/cli / getMachineName

Function getMachineName

pkg/cmd/codespace/create.go:520–578  ·  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 int64, machine, branch, location string, devcontainerPath string)

Source from the content-addressed store, hash-verified

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