convert.go 4.6 KB

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