publish.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/docker/cli/cli/command"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/compose/v2/pkg/api"
  19. )
  20. type publishOptions struct {
  21. *ProjectOptions
  22. resolveImageDigests bool
  23. ociVersion string
  24. }
  25. func publishCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  26. opts := publishOptions{
  27. ProjectOptions: p,
  28. }
  29. cmd := &cobra.Command{
  30. Use: "publish [OPTIONS] REPOSITORY[:TAG]",
  31. Short: "Publish compose application",
  32. RunE: Adapt(func(ctx context.Context, args []string) error {
  33. return runPublish(ctx, dockerCli, backend, opts, args[0])
  34. }),
  35. Args: cobra.ExactArgs(1),
  36. }
  37. flags := cmd.Flags()
  38. flags.BoolVar(&opts.resolveImageDigests, "resolve-image-digests", false, "Pin image tags to digests")
  39. flags.StringVar(&opts.ociVersion, "oci-version", "", "OCI Image/Artifact specification version (automatically determined by default)")
  40. return cmd
  41. }
  42. func runPublish(ctx context.Context, dockerCli command.Cli, backend api.Service, opts publishOptions, repository string) error {
  43. project, _, err := opts.ToProject(ctx, dockerCli, nil)
  44. if err != nil {
  45. return err
  46. }
  47. return backend.Publish(ctx, project, repository, api.PublishOptions{
  48. ResolveImageDigests: opts.resolveImageDigests,
  49. OCIVersion: api.OCIVersion(opts.ociVersion),
  50. })
  51. }