publish.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/sirupsen/logrus"
  20. "github.com/spf13/cobra"
  21. "github.com/spf13/pflag"
  22. "github.com/docker/compose/v5/pkg/api"
  23. "github.com/docker/compose/v5/pkg/compose"
  24. )
  25. type publishOptions struct {
  26. *ProjectOptions
  27. resolveImageDigests bool
  28. ociVersion string
  29. withEnvironment bool
  30. assumeYes bool
  31. app bool
  32. insecureRegistry bool
  33. }
  34. func publishCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command {
  35. opts := publishOptions{
  36. ProjectOptions: p,
  37. }
  38. cmd := &cobra.Command{
  39. Use: "publish [OPTIONS] REPOSITORY[:TAG]",
  40. Short: "Publish compose application",
  41. RunE: Adapt(func(ctx context.Context, args []string) error {
  42. return runPublish(ctx, dockerCli, backendOptions, opts, args[0])
  43. }),
  44. Args: cli.ExactArgs(1),
  45. }
  46. flags := cmd.Flags()
  47. flags.BoolVar(&opts.resolveImageDigests, "resolve-image-digests", false, "Pin image tags to digests")
  48. flags.StringVar(&opts.ociVersion, "oci-version", "", "OCI image/artifact specification version (automatically determined by default)")
  49. flags.BoolVar(&opts.withEnvironment, "with-env", false, "Include environment variables in the published OCI artifact")
  50. flags.BoolVarP(&opts.assumeYes, "yes", "y", false, `Assume "yes" as answer to all prompts`)
  51. flags.BoolVar(&opts.app, "app", false, "Published compose application (includes referenced images)")
  52. flags.BoolVar(&opts.insecureRegistry, "insecure-registry", false, "Use insecure registry")
  53. flags.SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
  54. // assumeYes was introduced by mistake as `--y`
  55. if name == "y" {
  56. logrus.Warn("--y is deprecated, please use --yes instead")
  57. name = "yes"
  58. }
  59. return pflag.NormalizedName(name)
  60. })
  61. // Should **only** be used for testing purpose, we don't want to promote use of insecure registries
  62. _ = flags.MarkHidden("insecure-registry")
  63. return cmd
  64. }
  65. func runPublish(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOptions, opts publishOptions, repository string) error {
  66. if opts.assumeYes {
  67. backendOptions.Options = append(backendOptions.Options, compose.WithPrompt(compose.AlwaysOkPrompt()))
  68. }
  69. backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
  70. if err != nil {
  71. return err
  72. }
  73. project, metrics, err := opts.ToProject(ctx, dockerCli, backend, nil)
  74. if err != nil {
  75. return err
  76. }
  77. if metrics.CountIncludesLocal > 0 {
  78. return errors.New("cannot publish compose file with local includes")
  79. }
  80. return backend.Publish(ctx, project, repository, api.PublishOptions{
  81. ResolveImageDigests: opts.resolveImageDigests || opts.app,
  82. Application: opts.app,
  83. OCIVersion: api.OCIVersion(opts.ociVersion),
  84. WithEnvironment: opts.withEnvironment,
  85. InsecureRegistry: opts.insecureRegistry,
  86. })
  87. }