pull.go 2.9 KB

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