pull.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/docker/cli/cli/command"
  20. "github.com/morikuni/aec"
  21. "github.com/spf13/cobra"
  22. "github.com/docker/compose/v2/pkg/api"
  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. noBuildable bool
  33. }
  34. func pullCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  35. opts := pullOptions{
  36. ProjectOptions: p,
  37. }
  38. cmd := &cobra.Command{
  39. Use: "pull [OPTIONS] [SERVICE...]",
  40. Short: "Pull service images",
  41. PreRunE: Adapt(func(ctx context.Context, args []string) error {
  42. if opts.noParallel {
  43. fmt.Fprint(os.Stderr, aec.Apply("option '--no-parallel' is DEPRECATED and will be ignored.\n", aec.RedF))
  44. }
  45. return nil
  46. }),
  47. RunE: Adapt(func(ctx context.Context, args []string) error {
  48. return runPull(ctx, dockerCli, backend, opts, args)
  49. }),
  50. ValidArgsFunction: completeServiceNames(dockerCli, p),
  51. }
  52. flags := cmd.Flags()
  53. flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Pull without printing progress information.")
  54. cmd.Flags().BoolVar(&opts.includeDeps, "include-deps", false, "Also pull services declared as dependencies.")
  55. cmd.Flags().BoolVar(&opts.parallel, "parallel", true, "DEPRECATED pull multiple images in parallel.")
  56. flags.MarkHidden("parallel") //nolint:errcheck
  57. cmd.Flags().BoolVar(&opts.parallel, "no-parallel", true, "DEPRECATED disable parallel pulling.")
  58. flags.MarkHidden("no-parallel") //nolint:errcheck
  59. cmd.Flags().BoolVar(&opts.ignorePullFailures, "ignore-pull-failures", false, "Pull what it can and ignores images with pull failures.")
  60. cmd.Flags().BoolVar(&opts.noBuildable, "ignore-buildable", false, "Ignore images that can be built.")
  61. return cmd
  62. }
  63. func runPull(ctx context.Context, dockerCli command.Cli, backend api.Service, opts pullOptions, services []string) error {
  64. project, err := opts.ToProject(dockerCli, services)
  65. if err != nil {
  66. return err
  67. }
  68. if !opts.includeDeps {
  69. err := project.ForServices(services, types.IgnoreDependencies)
  70. if err != nil {
  71. return err
  72. }
  73. }
  74. return backend.Pull(ctx, project, api.PullOptions{
  75. Quiet: opts.quiet,
  76. IgnoreFailures: opts.ignorePullFailures,
  77. IgnoreBuildable: opts.noBuildable,
  78. })
  79. }