push.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/types"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/compose/v2/pkg/api"
  19. )
  20. type pushOptions struct {
  21. *ProjectOptions
  22. composeOptions
  23. IncludeDeps bool
  24. Ignorefailures bool
  25. Quiet bool
  26. Repository string
  27. }
  28. func pushCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
  29. opts := pushOptions{
  30. ProjectOptions: p,
  31. }
  32. pushCmd := &cobra.Command{
  33. Use: "push [OPTIONS] [SERVICE...]",
  34. Short: "Push service images",
  35. RunE: Adapt(func(ctx context.Context, args []string) error {
  36. return runPush(ctx, backend, opts, args)
  37. }),
  38. ValidArgsFunction: completeServiceNames(p),
  39. }
  40. pushCmd.Flags().BoolVar(&opts.Ignorefailures, "ignore-push-failures", false, "Push what it can and ignores images with push failures")
  41. pushCmd.Flags().BoolVar(&opts.IncludeDeps, "include-deps", false, "Also push images of services declared as dependencies")
  42. pushCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Push without printing progress information")
  43. pushCmd.Flags().StringVarP(&opts.Repository, "repository", "r", "", "Also publish the compose application in repository")
  44. return pushCmd
  45. }
  46. func runPush(ctx context.Context, backend api.Service, opts pushOptions, services []string) error {
  47. project, err := opts.ToProject(services)
  48. if err != nil {
  49. return err
  50. }
  51. if !opts.IncludeDeps {
  52. err := project.ForServices(services, types.IgnoreDependencies)
  53. if err != nil {
  54. return err
  55. }
  56. }
  57. return backend.Push(ctx, project, api.PushOptions{
  58. IgnoreFailures: opts.Ignorefailures,
  59. Quiet: opts.Quiet,
  60. Repository: opts.Repository,
  61. })
  62. }