convert.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "bufio"
  16. "context"
  17. "fmt"
  18. "io"
  19. "os"
  20. "github.com/cnabio/cnab-to-oci/remotes"
  21. "github.com/distribution/distribution/v3/reference"
  22. cliconfig "github.com/docker/cli/cli/config"
  23. "github.com/opencontainers/go-digest"
  24. "github.com/spf13/cobra"
  25. "github.com/docker/compose-cli/api/client"
  26. "github.com/docker/compose-cli/api/compose"
  27. "github.com/docker/compose-cli/api/config"
  28. )
  29. type convertOptions struct {
  30. *projectOptions
  31. Format string
  32. Output string
  33. quiet bool
  34. resolve bool
  35. }
  36. var addFlagsFuncs []func(cmd *cobra.Command, opts *convertOptions)
  37. func convertCommand(p *projectOptions) *cobra.Command {
  38. opts := convertOptions{
  39. projectOptions: p,
  40. }
  41. cmd := &cobra.Command{
  42. Aliases: []string{"config"},
  43. Use: "convert SERVICES",
  44. Short: "Converts the compose file to platform's canonical format",
  45. RunE: func(cmd *cobra.Command, args []string) error {
  46. if opts.quiet {
  47. devnull, err := os.Open(os.DevNull)
  48. if err != nil {
  49. return err
  50. }
  51. os.Stdout = devnull
  52. }
  53. return runConvert(cmd.Context(), opts, args)
  54. },
  55. }
  56. flags := cmd.Flags()
  57. flags.StringVar(&opts.Format, "format", "yaml", "Format the output. Values: [yaml | json]")
  58. flags.BoolVar(&opts.resolve, "resolve-image-digests", false, "Pin image tags to digests.")
  59. flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only validate the configuration, don't print anything.")
  60. // add flags for hidden backends
  61. for _, f := range addFlagsFuncs {
  62. f(cmd, &opts)
  63. }
  64. return cmd
  65. }
  66. func runConvert(ctx context.Context, opts convertOptions, services []string) error {
  67. var json []byte
  68. c, err := client.NewWithDefaultLocalBackend(ctx)
  69. if err != nil {
  70. return err
  71. }
  72. project, err := opts.toProject(services)
  73. if err != nil {
  74. return err
  75. }
  76. if opts.resolve {
  77. configFile, err := cliconfig.Load(config.Dir(ctx))
  78. if err != nil {
  79. return err
  80. }
  81. resolver := remotes.CreateResolver(configFile)
  82. err = project.ResolveImages(func(named reference.Named) (digest.Digest, error) {
  83. _, desc, err := resolver.Resolve(ctx, named.String())
  84. return desc.Digest, err
  85. })
  86. if err != nil {
  87. return err
  88. }
  89. }
  90. json, err = c.ComposeService().Convert(ctx, project, compose.ConvertOptions{
  91. Format: opts.Format,
  92. Output: opts.Output,
  93. })
  94. if err != nil {
  95. return err
  96. }
  97. if opts.quiet {
  98. return nil
  99. }
  100. var out io.Writer = os.Stdout
  101. if opts.Output != "" && len(json) > 0 {
  102. file, err := os.Create(opts.Output)
  103. if err != nil {
  104. return err
  105. }
  106. out = bufio.NewWriter(file)
  107. }
  108. _, err = fmt.Fprint(out, string(json))
  109. return err
  110. }