pull.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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-cli/api/compose"
  21. "github.com/docker/compose-cli/api/progress"
  22. "github.com/docker/compose-cli/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 compose.Service) *cobra.Command {
  34. opts := pullOptions{
  35. projectOptions: p,
  36. }
  37. cmd := &cobra.Command{
  38. Use: "pull [SERVICE...]",
  39. Short: "Pull service images",
  40. RunE: 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 runPull(ctx, backend, opts, args)
  45. }),
  46. }
  47. flags := cmd.Flags()
  48. flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Pull without printing progress information")
  49. cmd.Flags().BoolVar(&opts.includeDeps, "include-deps", false, "Also pull services declared as dependencies")
  50. cmd.Flags().BoolVar(&opts.parallel, "parallel", true, "DEPRECATED pull multiple images in parallel.")
  51. flags.MarkHidden("parallel") //nolint:errcheck
  52. cmd.Flags().BoolVar(&opts.parallel, "no-parallel", true, "DEPRECATED disable parallel pulling.")
  53. flags.MarkHidden("no-parallel") //nolint:errcheck
  54. cmd.Flags().BoolVar(&opts.ignorePullFailures, "ignore-pull-failures", false, "Pull what it can and ignores images with pull failures")
  55. return cmd
  56. }
  57. func runPull(ctx context.Context, backend compose.Service, opts pullOptions, services []string) error {
  58. project, err := opts.toProject(services)
  59. if err != nil {
  60. return err
  61. }
  62. if !opts.includeDeps {
  63. enabled, err := project.GetServices(services...)
  64. if err != nil {
  65. return err
  66. }
  67. for _, s := range project.Services {
  68. if !utils.StringContains(services, s.Name) {
  69. project.DisabledServices = append(project.DisabledServices, s)
  70. }
  71. }
  72. project.Services = enabled
  73. }
  74. apiOpts := compose.PullOptions{
  75. IgnoreFailures: opts.ignorePullFailures,
  76. }
  77. if opts.quiet {
  78. return backend.Pull(ctx, project, apiOpts)
  79. }
  80. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  81. return "", backend.Pull(ctx, project, apiOpts)
  82. })
  83. return err
  84. }