convert.go 5.1 KB

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