convert.go 5.0 KB

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