convert.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. opts.Output = os.DevNull
  54. return runConvert(cmd.Context(), opts, args)
  55. },
  56. }
  57. flags := cmd.Flags()
  58. flags.StringVar(&opts.Format, "format", "yaml", "Format the output. Values: [yaml | json]")
  59. flags.BoolVar(&opts.resolve, "resolve-image-digests", false, "Pin image tags to digests.")
  60. flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only validate the configuration, don't print anything.")
  61. // add flags for hidden backends
  62. for _, f := range addFlagsFuncs {
  63. f(cmd, &opts)
  64. }
  65. return cmd
  66. }
  67. func runConvert(ctx context.Context, opts convertOptions, services []string) error {
  68. var json []byte
  69. c, err := client.NewWithDefaultLocalBackend(ctx)
  70. if err != nil {
  71. return err
  72. }
  73. project, err := opts.toProject(services)
  74. if err != nil {
  75. return err
  76. }
  77. if opts.resolve {
  78. configFile, err := cliconfig.Load(config.Dir(ctx))
  79. if err != nil {
  80. return err
  81. }
  82. resolver := remotes.CreateResolver(configFile)
  83. err = project.ResolveImages(func(named reference.Named) (digest.Digest, error) {
  84. _, desc, err := resolver.Resolve(ctx, named.String())
  85. return desc.Digest, err
  86. })
  87. if err != nil {
  88. return err
  89. }
  90. }
  91. json, err = c.ComposeService().Convert(ctx, project, compose.ConvertOptions{
  92. Format: opts.Format,
  93. Output: opts.Output,
  94. })
  95. if err != nil {
  96. return err
  97. }
  98. if opts.quiet {
  99. return nil
  100. }
  101. var out io.Writer = os.Stdout
  102. if opts.Output != "" {
  103. file, err := os.Create(opts.Output)
  104. if err != nil {
  105. return err
  106. }
  107. out = bufio.NewWriter(file)
  108. }
  109. _, err = fmt.Fprint(out, string(json))
  110. return err
  111. }