publish.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. "errors"
  17. "github.com/docker/cli/cli"
  18. "github.com/docker/cli/cli/command"
  19. "github.com/docker/compose/v2/pkg/api"
  20. "github.com/sirupsen/logrus"
  21. "github.com/spf13/cobra"
  22. "github.com/spf13/pflag"
  23. )
  24. type publishOptions struct {
  25. *ProjectOptions
  26. resolveImageDigests bool
  27. ociVersion string
  28. withEnvironment bool
  29. assumeYes bool
  30. }
  31. func publishCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  32. opts := publishOptions{
  33. ProjectOptions: p,
  34. }
  35. cmd := &cobra.Command{
  36. Use: "publish [OPTIONS] REPOSITORY[:TAG]",
  37. Short: "Publish compose application",
  38. RunE: Adapt(func(ctx context.Context, args []string) error {
  39. return runPublish(ctx, dockerCli, backend, opts, args[0])
  40. }),
  41. Args: cli.ExactArgs(1),
  42. }
  43. flags := cmd.Flags()
  44. flags.BoolVar(&opts.resolveImageDigests, "resolve-image-digests", false, "Pin image tags to digests")
  45. flags.StringVar(&opts.ociVersion, "oci-version", "", "OCI image/artifact specification version (automatically determined by default)")
  46. flags.BoolVar(&opts.withEnvironment, "with-env", false, "Include environment variables in the published OCI artifact")
  47. flags.BoolVarP(&opts.assumeYes, "yes", "y", false, `Assume "yes" as answer to all prompts`)
  48. flags.SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
  49. // assumeYes was introduced by mistake as `--y`
  50. if name == "y" {
  51. logrus.Warn("--y is deprecated, please use --yes instead")
  52. name = "yes"
  53. }
  54. return pflag.NormalizedName(name)
  55. })
  56. return cmd
  57. }
  58. func runPublish(ctx context.Context, dockerCli command.Cli, backend api.Service, opts publishOptions, repository string) error {
  59. project, metrics, err := opts.ToProject(ctx, dockerCli, nil)
  60. if err != nil {
  61. return err
  62. }
  63. if metrics.CountIncludesLocal > 0 {
  64. return errors.New("cannot publish compose file with local includes")
  65. }
  66. return backend.Publish(ctx, project, repository, api.PublishOptions{
  67. ResolveImageDigests: opts.resolveImageDigests,
  68. OCIVersion: api.OCIVersion(opts.ociVersion),
  69. WithEnvironment: opts.withEnvironment,
  70. AssumeYes: opts.assumeYes,
  71. })
  72. }