completion.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "sort"
  16. "strings"
  17. "github.com/docker/cli/cli/command"
  18. "github.com/docker/compose/v2/pkg/api"
  19. "github.com/spf13/cobra"
  20. )
  21. // validArgsFn defines a completion func to be returned to fetch completion options
  22. type validArgsFn func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)
  23. func noCompletion() validArgsFn {
  24. return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  25. return []string{}, cobra.ShellCompDirectiveNoSpace
  26. }
  27. }
  28. func completeServiceNames(dockerCli command.Cli, p *ProjectOptions) validArgsFn {
  29. return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  30. p.Offline = true
  31. project, _, err := p.ToProject(cmd.Context(), dockerCli, nil)
  32. if err != nil {
  33. return nil, cobra.ShellCompDirectiveNoFileComp
  34. }
  35. var values []string
  36. serviceNames := append(project.ServiceNames(), project.DisabledServiceNames()...)
  37. for _, s := range serviceNames {
  38. if toComplete == "" || strings.HasPrefix(s, toComplete) {
  39. values = append(values, s)
  40. }
  41. }
  42. return values, cobra.ShellCompDirectiveNoFileComp
  43. }
  44. }
  45. func completeProjectNames(backend api.Service) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  46. return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  47. list, err := backend.List(cmd.Context(), api.ListOptions{
  48. All: true,
  49. })
  50. if err != nil {
  51. return nil, cobra.ShellCompDirectiveError
  52. }
  53. var values []string
  54. for _, stack := range list {
  55. if strings.HasPrefix(stack.Name, toComplete) {
  56. values = append(values, stack.Name)
  57. }
  58. }
  59. return values, cobra.ShellCompDirectiveNoFileComp
  60. }
  61. }
  62. func completeProfileNames(dockerCli command.Cli, p *ProjectOptions) validArgsFn {
  63. return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  64. p.Offline = true
  65. project, _, err := p.ToProject(cmd.Context(), dockerCli, nil)
  66. if err != nil {
  67. return nil, cobra.ShellCompDirectiveNoFileComp
  68. }
  69. allProfileNames := project.AllServices().GetProfiles()
  70. sort.Strings(allProfileNames)
  71. var values []string
  72. for _, profileName := range allProfileNames {
  73. if strings.HasPrefix(profileName, toComplete) {
  74. values = append(values, profileName)
  75. }
  76. }
  77. return values, cobra.ShellCompDirectiveNoFileComp
  78. }
  79. }