convert.go 5.1 KB

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