pull.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "context"
  16. "fmt"
  17. "os"
  18. "github.com/compose-spec/compose-go/types"
  19. "github.com/morikuni/aec"
  20. "github.com/spf13/cobra"
  21. "github.com/docker/compose/v2/pkg/api"
  22. "github.com/docker/compose/v2/pkg/utils"
  23. )
  24. type pullOptions struct {
  25. *ProjectOptions
  26. composeOptions
  27. quiet bool
  28. parallel bool
  29. noParallel bool
  30. includeDeps bool
  31. ignorePullFailures bool
  32. }
  33. func pullCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
  34. opts := pullOptions{
  35. ProjectOptions: p,
  36. }
  37. cmd := &cobra.Command{
  38. Use: "pull [OPTIONS] [SERVICE...]",
  39. Short: "Pull service images",
  40. PreRunE: Adapt(func(ctx context.Context, args []string) error {
  41. if opts.noParallel {
  42. fmt.Fprint(os.Stderr, aec.Apply("option '--no-parallel' is DEPRECATED and will be ignored.\n", aec.RedF))
  43. }
  44. return nil
  45. }),
  46. RunE: Adapt(func(ctx context.Context, args []string) error {
  47. return runPull(ctx, backend, opts, args)
  48. }),
  49. ValidArgsFunction: completeServiceNames(p),
  50. }
  51. flags := cmd.Flags()
  52. flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Pull without printing progress information")
  53. cmd.Flags().BoolVar(&opts.includeDeps, "include-deps", false, "Also pull services declared as dependencies")
  54. cmd.Flags().BoolVar(&opts.parallel, "parallel", true, "DEPRECATED pull multiple images in parallel.")
  55. flags.MarkHidden("parallel") //nolint:errcheck
  56. cmd.Flags().BoolVar(&opts.parallel, "no-parallel", true, "DEPRECATED disable parallel pulling.")
  57. flags.MarkHidden("no-parallel") //nolint:errcheck
  58. cmd.Flags().BoolVar(&opts.ignorePullFailures, "ignore-pull-failures", false, "Pull what it can and ignores images with pull failures")
  59. return cmd
  60. }
  61. func withSelectedServicesOnly(project *types.Project, services []string) error {
  62. enabled, err := project.GetServices(services...)
  63. if err != nil {
  64. return err
  65. }
  66. for _, s := range project.Services {
  67. if !utils.StringContains(services, s.Name) {
  68. project.DisabledServices = append(project.DisabledServices, s)
  69. }
  70. }
  71. project.Services = enabled
  72. return nil
  73. }
  74. func runPull(ctx context.Context, backend api.Service, opts pullOptions, services []string) error {
  75. project, err := opts.ToProject(services)
  76. if err != nil {
  77. return err
  78. }
  79. if !opts.includeDeps {
  80. err := withSelectedServicesOnly(project, services)
  81. if err != nil {
  82. return err
  83. }
  84. }
  85. return backend.Pull(ctx, project, api.PullOptions{
  86. Quiet: opts.quiet,
  87. IgnoreFailures: opts.ignorePullFailures,
  88. })
  89. }