GetServiceNamesFunc returns a function for autocompleting service names for service flag. If existingOnly is true, only names of existing services will be returned.
(existingOnly bool)
| 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. |
| 473 | func GetServiceNamesFunc(existingOnly bool) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { |
| 474 | return func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 475 | // the project name can be passed as an argument |
| 476 | projectName := "" |
| 477 | if len(args) > 0 { |
| 478 | projectName = args[0] |
| 479 | } |
| 480 | app, err := GetActiveApp(projectName) |
| 481 | if err != nil { |
| 482 | return nil, cobra.ShellCompDirectiveNoFileComp |
| 483 | } |
| 484 | if app.ComposeYaml == nil || app.ComposeYaml.Services == nil { |
| 485 | return nil, cobra.ShellCompDirectiveNoFileComp |
| 486 | } |
| 487 | var services []string |
| 488 | for service := range app.ComposeYaml.Services { |
| 489 | if existingOnly { |
| 490 | c, err := app.FindContainerByType(service) |
| 491 | if err == nil && c != nil { |
| 492 | services = append(services, service) |
| 493 | } |
| 494 | } else { |
| 495 | services = append(services, service) |
| 496 | } |
| 497 | } |
| 498 | return services, cobra.ShellCompDirectiveNoFileComp |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | // GetRelativeDirectory returns the directory relative to project root |
| 503 | // Note that the relative dir is returned as unix-style forward-slashes |
no test coverage detected