pull.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. "github.com/spf13/cobra"
  17. "github.com/docker/compose-cli/api/client"
  18. "github.com/docker/compose-cli/api/progress"
  19. "github.com/docker/compose-cli/utils"
  20. )
  21. type pullOptions struct {
  22. *projectOptions
  23. composeOptions
  24. quiet bool
  25. includeDeps bool
  26. }
  27. func pullCommand(p *projectOptions) *cobra.Command {
  28. opts := pullOptions{
  29. projectOptions: p,
  30. }
  31. cmd := &cobra.Command{
  32. Use: "pull [SERVICE...]",
  33. Short: "Pull service images",
  34. RunE: func(cmd *cobra.Command, args []string) error {
  35. return runPull(cmd.Context(), opts, args)
  36. },
  37. }
  38. cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Pull without printing progress information")
  39. cmd.Flags().BoolVar(&opts.includeDeps, "include-deps", false, "Also pull services declared as dependencies")
  40. return cmd
  41. }
  42. func runPull(ctx context.Context, opts pullOptions, services []string) error {
  43. c, err := client.New(ctx)
  44. if err != nil {
  45. return err
  46. }
  47. project, err := opts.toProject(services)
  48. if err != nil {
  49. return err
  50. }
  51. if !opts.includeDeps {
  52. enabled, err := project.GetServices(services...)
  53. if err != nil {
  54. return err
  55. }
  56. for _, s := range project.Services {
  57. if !utils.StringContains(services, s.Name) {
  58. project.DisabledServices = append(project.DisabledServices, s)
  59. }
  60. }
  61. project.Services = enabled
  62. }
  63. if opts.quiet {
  64. return c.ComposeService().Pull(ctx, project)
  65. }
  66. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  67. return "", c.ComposeService().Pull(ctx, project)
  68. })
  69. return err
  70. }