completion.go 3.6 KB

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