1
0

pull.go 2.1 KB

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