bridge.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "fmt"
  17. "io"
  18. "github.com/distribution/reference"
  19. "github.com/docker/cli/cli/command"
  20. "github.com/docker/docker/api/types/image"
  21. "github.com/docker/docker/pkg/stringid"
  22. "github.com/docker/go-units"
  23. "github.com/spf13/cobra"
  24. "github.com/docker/compose/v2/cmd/formatter"
  25. "github.com/docker/compose/v2/pkg/api"
  26. "github.com/docker/compose/v2/pkg/bridge"
  27. )
  28. func bridgeCommand(p *ProjectOptions, dockerCli command.Cli) *cobra.Command {
  29. cmd := &cobra.Command{
  30. Use: "bridge CMD [OPTIONS]",
  31. Short: "Convert compose files into another model",
  32. TraverseChildren: true,
  33. }
  34. cmd.AddCommand(
  35. convertCommand(p, dockerCli),
  36. transformersCommand(dockerCli),
  37. )
  38. return cmd
  39. }
  40. func convertCommand(p *ProjectOptions, dockerCli command.Cli) *cobra.Command {
  41. return &cobra.Command{
  42. Use: "convert",
  43. Short: "Convert compose files to Kubernetes manifests, Helm charts, or another model",
  44. RunE: Adapt(func(ctx context.Context, args []string) error {
  45. return api.ErrNotImplemented
  46. }),
  47. }
  48. }
  49. func transformersCommand(dockerCli command.Cli) *cobra.Command {
  50. cmd := &cobra.Command{
  51. Use: "transformations CMD [OPTIONS]",
  52. Short: "Manage transformation images",
  53. }
  54. cmd.AddCommand(
  55. listTransformersCommand(dockerCli),
  56. createTransformerCommand(dockerCli),
  57. )
  58. return cmd
  59. }
  60. func listTransformersCommand(dockerCli command.Cli) *cobra.Command {
  61. options := lsOptions{}
  62. cmd := &cobra.Command{
  63. Use: "list",
  64. Aliases: []string{"ls"},
  65. Short: "List available transformations",
  66. RunE: Adapt(func(ctx context.Context, args []string) error {
  67. transformers, err := bridge.ListTransformers(ctx, dockerCli)
  68. if err != nil {
  69. return err
  70. }
  71. return displayTransformer(dockerCli, transformers, options)
  72. }),
  73. }
  74. cmd.Flags().StringVar(&options.Format, "format", "table", "Format the output. Values: [table | json]")
  75. cmd.Flags().BoolVarP(&options.Quiet, "quiet", "q", false, "Only display transformer names")
  76. return cmd
  77. }
  78. func displayTransformer(dockerCli command.Cli, transformers []image.Summary, options lsOptions) error {
  79. if options.Quiet {
  80. for _, t := range transformers {
  81. if len(t.RepoTags) > 0 {
  82. _, _ = fmt.Fprintln(dockerCli.Out(), t.RepoTags[0])
  83. } else {
  84. _, _ = fmt.Fprintln(dockerCli.Out(), t.ID)
  85. }
  86. }
  87. return nil
  88. }
  89. return formatter.Print(transformers, options.Format, dockerCli.Out(),
  90. func(w io.Writer) {
  91. for _, img := range transformers {
  92. id := stringid.TruncateID(img.ID)
  93. size := units.HumanSizeWithPrecision(float64(img.Size), 3)
  94. repo, tag := "<none>", "<none>"
  95. if len(img.RepoTags) > 0 {
  96. ref, err := reference.ParseDockerRef(img.RepoTags[0])
  97. if err == nil {
  98. // ParseDockerRef will reject a local image ID
  99. repo = reference.FamiliarName(ref)
  100. if tagged, ok := ref.(reference.Tagged); ok {
  101. tag = tagged.Tag()
  102. }
  103. }
  104. }
  105. _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", id, repo, tag, size)
  106. }
  107. },
  108. "IMAGE ID", "REPO", "TAGS", "SIZE")
  109. }
  110. func createTransformerCommand(dockerCli command.Cli) *cobra.Command {
  111. var opts bridge.CreateTransformerOptions
  112. cmd := &cobra.Command{
  113. Use: "create [OPTION] PATH",
  114. Short: "Create a new transformation",
  115. RunE: Adapt(func(ctx context.Context, args []string) error {
  116. opts.Dest = args[0]
  117. return bridge.CreateTransformer(ctx, dockerCli, opts)
  118. }),
  119. }
  120. cmd.Flags().StringVarP(&opts.From, "from", "f", "", "Existing transformation to copy (default: docker/compose-bridge-kubernetes)")
  121. return cmd
  122. }