convert.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. "sort"
  21. "strings"
  22. "github.com/cnabio/cnab-to-oci/remotes"
  23. "github.com/compose-spec/compose-go/cli"
  24. "github.com/distribution/distribution/v3/reference"
  25. cliconfig "github.com/docker/cli/cli/config"
  26. "github.com/opencontainers/go-digest"
  27. "github.com/spf13/cobra"
  28. "github.com/docker/compose-cli/pkg/api"
  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. noInterpolate bool
  38. services bool
  39. volumes bool
  40. profiles bool
  41. hash string
  42. }
  43. var addFlagsFuncs []func(cmd *cobra.Command, opts *convertOptions)
  44. func convertCommand(p *projectOptions, backend api.Service) *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. PreRunE: Adapt(func(ctx context.Context, 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. return nil
  61. }),
  62. RunE: Adapt(func(ctx context.Context, args []string) error {
  63. if opts.services {
  64. return runServices(opts)
  65. }
  66. if opts.volumes {
  67. return runVolumes(opts)
  68. }
  69. if opts.hash != "" {
  70. return runHash(opts)
  71. }
  72. if opts.profiles {
  73. return runProfiles(opts, args)
  74. }
  75. return runConvert(ctx, backend, opts, args)
  76. }),
  77. }
  78. flags := cmd.Flags()
  79. flags.StringVar(&opts.Format, "format", "yaml", "Format the output. Values: [yaml | json]")
  80. flags.BoolVar(&opts.resolve, "resolve-image-digests", false, "Pin image tags to digests.")
  81. flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only validate the configuration, don't print anything.")
  82. flags.BoolVar(&opts.noInterpolate, "no-interpolate", false, "Don't interpolate environment variables.")
  83. flags.BoolVar(&opts.services, "services", false, "Print the service names, one per line.")
  84. flags.BoolVar(&opts.volumes, "volumes", false, "Print the volume names, one per line.")
  85. flags.BoolVar(&opts.profiles, "profiles", false, "Print the profile names, one per line.")
  86. flags.StringVar(&opts.hash, "hash", "", "Print the service config hash, one per line.")
  87. // add flags for hidden backends
  88. for _, f := range addFlagsFuncs {
  89. f(cmd, &opts)
  90. }
  91. return cmd
  92. }
  93. func runConvert(ctx context.Context, backend api.Service, opts convertOptions, services []string) error {
  94. var json []byte
  95. project, err := opts.toProject(services, cli.WithInterpolation(!opts.noInterpolate))
  96. if err != nil {
  97. return err
  98. }
  99. if opts.resolve {
  100. configFile := cliconfig.LoadDefaultConfigFile(os.Stderr)
  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 = backend.Convert(ctx, project, api.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 n := range project.Volumes {
  147. fmt.Println(n)
  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. }
  169. func runProfiles(opts convertOptions, services []string) error {
  170. set := map[string]struct{}{}
  171. project, err := opts.toProject(services)
  172. if err != nil {
  173. return err
  174. }
  175. for _, s := range project.AllServices() {
  176. for _, p := range s.Profiles {
  177. set[p] = struct{}{}
  178. }
  179. }
  180. profiles := make([]string, 0, len(set))
  181. for p := range set {
  182. profiles = append(profiles, p)
  183. }
  184. sort.Strings(profiles)
  185. for _, p := range profiles {
  186. fmt.Println(p)
  187. }
  188. return nil
  189. }