convert.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. "strings"
  21. "github.com/cnabio/cnab-to-oci/remotes"
  22. "github.com/distribution/distribution/v3/reference"
  23. cliconfig "github.com/docker/cli/cli/config"
  24. "github.com/opencontainers/go-digest"
  25. "github.com/spf13/cobra"
  26. "github.com/docker/compose-cli/api/client"
  27. "github.com/docker/compose-cli/api/compose"
  28. "github.com/docker/compose-cli/api/config"
  29. "github.com/docker/compose-cli/utils"
  30. )
  31. type convertOptions struct {
  32. *projectOptions
  33. Format string
  34. Output string
  35. quiet bool
  36. resolve bool
  37. services bool
  38. volumes bool
  39. hash string
  40. }
  41. var addFlagsFuncs []func(cmd *cobra.Command, opts *convertOptions)
  42. func convertCommand(p *projectOptions) *cobra.Command {
  43. opts := convertOptions{
  44. projectOptions: p,
  45. }
  46. cmd := &cobra.Command{
  47. Aliases: []string{"config"},
  48. Use: "convert SERVICES",
  49. Short: "Converts the compose file to platform's canonical format",
  50. RunE: func(cmd *cobra.Command, args []string) error {
  51. if opts.quiet {
  52. devnull, err := os.Open(os.DevNull)
  53. if err != nil {
  54. return err
  55. }
  56. os.Stdout = devnull
  57. }
  58. if opts.services {
  59. return runServices(opts)
  60. }
  61. if opts.volumes {
  62. return runVolumes(opts)
  63. }
  64. if opts.hash != "" {
  65. return runHash(opts)
  66. }
  67. return runConvert(cmd.Context(), opts, args)
  68. },
  69. }
  70. flags := cmd.Flags()
  71. flags.StringVar(&opts.Format, "format", "yaml", "Format the output. Values: [yaml | json]")
  72. flags.BoolVar(&opts.resolve, "resolve-image-digests", false, "Pin image tags to digests.")
  73. flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only validate the configuration, don't print anything.")
  74. flags.BoolVar(&opts.services, "services", false, "Print the service names, one per line.")
  75. flags.BoolVar(&opts.volumes, "volumes", false, "Print the volume names, one per line.")
  76. flags.StringVar(&opts.hash, "hash", "", "Print the service config hash, one per line.")
  77. // add flags for hidden backends
  78. for _, f := range addFlagsFuncs {
  79. f(cmd, &opts)
  80. }
  81. return cmd
  82. }
  83. func runConvert(ctx context.Context, opts convertOptions, services []string) error {
  84. var json []byte
  85. c, err := client.New(ctx)
  86. if err != nil {
  87. return err
  88. }
  89. project, err := opts.toProject(services)
  90. if err != nil {
  91. return err
  92. }
  93. if opts.resolve {
  94. configFile, err := cliconfig.Load(config.Dir())
  95. if err != nil {
  96. return err
  97. }
  98. resolver := remotes.CreateResolver(configFile)
  99. err = project.ResolveImages(func(named reference.Named) (digest.Digest, error) {
  100. _, desc, err := resolver.Resolve(ctx, named.String())
  101. return desc.Digest, err
  102. })
  103. if err != nil {
  104. return err
  105. }
  106. }
  107. json, err = c.ComposeService().Convert(ctx, project, compose.ConvertOptions{
  108. Format: opts.Format,
  109. Output: opts.Output,
  110. })
  111. if err != nil {
  112. return err
  113. }
  114. if opts.quiet {
  115. return nil
  116. }
  117. var out io.Writer = os.Stdout
  118. if opts.Output != "" && len(json) > 0 {
  119. file, err := os.Create(opts.Output)
  120. if err != nil {
  121. return err
  122. }
  123. out = bufio.NewWriter(file)
  124. }
  125. _, err = fmt.Fprint(out, string(json))
  126. return err
  127. }
  128. func runServices(opts convertOptions) error {
  129. project, err := opts.toProject(nil)
  130. if err != nil {
  131. return err
  132. }
  133. for _, s := range project.Services {
  134. fmt.Println(s.Name)
  135. }
  136. return nil
  137. }
  138. func runVolumes(opts convertOptions) error {
  139. project, err := opts.toProject(nil)
  140. if err != nil {
  141. return err
  142. }
  143. for _, v := range project.Volumes {
  144. fmt.Println(v.Name)
  145. }
  146. return nil
  147. }
  148. func runHash(opts convertOptions) error {
  149. var services []string
  150. if opts.hash != "*" {
  151. services = append(services, strings.Split(opts.hash, ",")...)
  152. }
  153. project, err := opts.toProject(services)
  154. if err != nil {
  155. return err
  156. }
  157. for _, s := range project.Services {
  158. hash, err := utils.ServiceHash(s)
  159. if err != nil {
  160. return err
  161. }
  162. fmt.Printf("%s %s\n", s.Name, hash)
  163. }
  164. return nil
  165. }