completion.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/spf13/cobra"
  19. "github.com/docker/compose/v5/pkg/api"
  20. "github.com/docker/compose/v5/pkg/compose"
  21. )
  22. // validArgsFn defines a completion func to be returned to fetch completion options
  23. type validArgsFn func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)
  24. func noCompletion() validArgsFn {
  25. return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  26. return []string{}, cobra.ShellCompDirectiveNoSpace
  27. }
  28. }
  29. func completeServiceNames(dockerCli command.Cli, p *ProjectOptions) validArgsFn {
  30. return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  31. p.Offline = true
  32. backend, err := compose.NewComposeService(dockerCli)
  33. if err != nil {
  34. return nil, cobra.ShellCompDirectiveNoFileComp
  35. }
  36. project, _, err := p.ToProject(cmd.Context(), dockerCli, backend, nil)
  37. if err != nil {
  38. return nil, cobra.ShellCompDirectiveNoFileComp
  39. }
  40. var values []string
  41. serviceNames := append(project.ServiceNames(), project.DisabledServiceNames()...)
  42. for _, s := range serviceNames {
  43. if toComplete == "" || strings.HasPrefix(s, toComplete) {
  44. values = append(values, s)
  45. }
  46. }
  47. return values, cobra.ShellCompDirectiveNoFileComp
  48. }
  49. }
  50. func completeProjectNames(dockerCli command.Cli, backendOptions *BackendOptions) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  51. return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  52. backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
  53. if err != nil {
  54. return nil, cobra.ShellCompDirectiveError
  55. }
  56. list, err := backend.List(cmd.Context(), api.ListOptions{
  57. All: true,
  58. })
  59. if err != nil {
  60. return nil, cobra.ShellCompDirectiveError
  61. }
  62. var values []string
  63. for _, stack := range list {
  64. if strings.HasPrefix(stack.Name, toComplete) {
  65. values = append(values, stack.Name)
  66. }
  67. }
  68. return values, cobra.ShellCompDirectiveNoFileComp
  69. }
  70. }
  71. func completeProfileNames(dockerCli command.Cli, p *ProjectOptions) validArgsFn {
  72. return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  73. p.Offline = true
  74. backend, err := compose.NewComposeService(dockerCli)
  75. if err != nil {
  76. return nil, cobra.ShellCompDirectiveNoFileComp
  77. }
  78. project, _, err := p.ToProject(cmd.Context(), dockerCli, backend, nil)
  79. if err != nil {
  80. return nil, cobra.ShellCompDirectiveNoFileComp
  81. }
  82. allProfileNames := project.AllServices().GetProfiles()
  83. sort.Strings(allProfileNames)
  84. var values []string
  85. for _, profileName := range allProfileNames {
  86. if strings.HasPrefix(profileName, toComplete) {
  87. values = append(values, profileName)
  88. }
  89. }
  90. return values, cobra.ShellCompDirectiveNoFileComp
  91. }
  92. }
  93. func completeScaleArgs(cli command.Cli, p *ProjectOptions) cobra.CompletionFunc {
  94. return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  95. completions, directive := completeServiceNames(cli, p)(cmd, args, toComplete)
  96. for i, completion := range completions {
  97. completions[i] = completion + "="
  98. }
  99. return completions, directive
  100. }
  101. }