pull.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. }
  25. func pullCommand(p *projectOptions) *cobra.Command {
  26. opts := pullOptions{
  27. projectOptions: p,
  28. }
  29. cmd := &cobra.Command{
  30. Use: "pull [SERVICE...]",
  31. Short: "Pull service images",
  32. RunE: func(cmd *cobra.Command, args []string) error {
  33. return runPull(cmd.Context(), opts, args)
  34. },
  35. }
  36. cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Pull without printing progress information")
  37. return cmd
  38. }
  39. func runPull(ctx context.Context, opts pullOptions, services []string) error {
  40. c, err := client.NewWithDefaultLocalBackend(ctx)
  41. if err != nil {
  42. return err
  43. }
  44. project, err := opts.toProject(services)
  45. if err != nil {
  46. return err
  47. }
  48. if opts.quiet {
  49. return c.ComposeService().Pull(ctx, project)
  50. }
  51. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  52. return "", c.ComposeService().Pull(ctx, project)
  53. })
  54. return err
  55. }