GetProjectNamesFunc returns a function for autocompleting project names for command arguments. If status is "inactive" or "active", only names of inactive or active projects respectively are returned. If status is "all", all project names are returned. If numArgs is 0, completion will be provided fo
(status string, numArgs int)
| 430 | // If numArgs is 0, completion will be provided for infinite arguments, |
| 431 | // otherwise it will only be provided for the numArgs number of arguments. |
| 432 | func GetProjectNamesFunc(status string, numArgs int) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { |
| 433 | return func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 434 | // Don't provide completions if the user keeps hitting space after |
| 435 | // exhausting all of the valid arguments. |
| 436 | if numArgs > 0 && len(args)+1 > numArgs { |
| 437 | return nil, cobra.ShellCompDirectiveNoFileComp |
| 438 | } |
| 439 | |
| 440 | // Get all of the projects we're interested in for this completion function. |
| 441 | var apps []*DdevApp |
| 442 | var err error |
| 443 | if status == "inactive" { |
| 444 | apps, err = GetInactiveProjects() |
| 445 | } else if status == "active" { |
| 446 | apps, err = GetProjects(true) |
| 447 | } else if status == "all" { |
| 448 | apps, err = GetProjects(false) |
| 449 | } else { |
| 450 | // This is an error state - but we just return nothing |
| 451 | return nil, cobra.ShellCompDirectiveNoFileComp |
| 452 | } |
| 453 | // Return nothing if we have nothing, or return all of the project names. |
| 454 | // Note that if there's nothing to return, we don't let cobra pick completions |
| 455 | // from the files in the cwd. |
| 456 | if err != nil { |
| 457 | return nil, cobra.ShellCompDirectiveNoFileComp |
| 458 | } |
| 459 | |
| 460 | // Don't show arguments that are already written on the command line |
| 461 | var projectNames []string |
| 462 | for _, name := range ExtractProjectNames(apps) { |
| 463 | if !slices.Contains(args, name) { |
| 464 | projectNames = append(projectNames, name) |
| 465 | } |
| 466 | } |
| 467 | return projectNames, cobra.ShellCompDirectiveNoFileComp |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | // GetServiceNamesFunc returns a function for autocompleting service names for service flag. |
| 472 | // If existingOnly is true, only names of existing services will be returned. |
no test coverage detected