pull.go 2.8 KB

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