pull.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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/compose-spec/compose-go/cli"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/compose-cli/api/client"
  19. "github.com/docker/compose-cli/api/progress"
  20. )
  21. type pullOptions struct {
  22. composeOptions
  23. }
  24. func pullCommand() *cobra.Command {
  25. opts := pullOptions{}
  26. pullCmd := &cobra.Command{
  27. Use: "pull [SERVICE...]",
  28. Short: "Pull service images",
  29. RunE: func(cmd *cobra.Command, args []string) error {
  30. return runPull(cmd.Context(), opts, args)
  31. },
  32. }
  33. pullCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir")
  34. pullCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
  35. return pullCmd
  36. }
  37. func runPull(ctx context.Context, opts pullOptions, services []string) error {
  38. c, err := client.NewWithDefaultLocalBackend(ctx)
  39. if err != nil {
  40. return err
  41. }
  42. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  43. options, err := opts.toProjectOptions()
  44. if err != nil {
  45. return "", err
  46. }
  47. project, err := cli.ProjectFromOptions(options)
  48. if err != nil {
  49. return "", err
  50. }
  51. err = filter(project, services)
  52. if err != nil {
  53. return "", err
  54. }
  55. return "", c.ComposeService().Pull(ctx, project)
  56. })
  57. return err
  58. }