pull.go 2.6 KB

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