pull.go 2.6 KB

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