completeVMIDs is a Cobra ValidArgsFunction that returns VMID completions with the guest name and type as description. Requires no positional args already provided.
(cmd *cobra.Command, args []string, toComplete string)
| 556 | // with the guest name and type as description. Requires no positional args |
| 557 | // already provided. |
| 558 | func completeVMIDs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 559 | if len(args) != 0 { |
| 560 | return nil, cobra.ShellCompDirectiveNoFileComp |
| 561 | } |
| 562 | |
| 563 | session, err := initCLISession(cmd) |
| 564 | if err != nil || session == nil { |
| 565 | return nil, cobra.ShellCompDirectiveError |
| 566 | } |
| 567 | |
| 568 | vms, err := session.getVMs(context.Background()) |
| 569 | if err != nil { |
| 570 | return nil, cobra.ShellCompDirectiveError |
| 571 | } |
| 572 | |
| 573 | var completions []string |
| 574 | |
| 575 | for _, vm := range vms { |
| 576 | if vm == nil { |
| 577 | continue |
| 578 | } |
| 579 | |
| 580 | id := fmt.Sprintf("%d", vm.ID) |
| 581 | if strings.HasPrefix(id, toComplete) { |
| 582 | completions = append(completions, fmt.Sprintf("%s\t%s (%s)", id, vm.Name, vm.Type)) |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | return completions, cobra.ShellCompDirectiveNoFileComp |
| 587 | } |
| 588 | |
| 589 | // addNoWaitFlag adds a --no-wait flag to cmd for task-producing commands. |
| 590 | func addNoWaitFlag(cmd *cobra.Command) { |
nothing calls this directly
no test coverage detected