convert.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "github.com/docker/compose-cli/api/compose"
  21. "github.com/spf13/cobra"
  22. "github.com/docker/compose-cli/api/client"
  23. )
  24. type convertOptions struct {
  25. *projectOptions
  26. Format string
  27. Output string
  28. quiet bool
  29. }
  30. var addFlagsFuncs []func(cmd *cobra.Command, opts *convertOptions)
  31. func convertCommand(p *projectOptions) *cobra.Command {
  32. opts := convertOptions{
  33. projectOptions: p,
  34. }
  35. cmd := &cobra.Command{
  36. Aliases: []string{"config"},
  37. Use: "convert SERVICES",
  38. Short: "Converts the compose file to platform's canonical format",
  39. RunE: func(cmd *cobra.Command, args []string) error {
  40. if opts.quiet {
  41. devnull, err := os.Open(os.DevNull)
  42. if err != nil {
  43. return err
  44. }
  45. os.Stdout = devnull
  46. }
  47. opts.Output = os.DevNull
  48. return runConvert(cmd.Context(), opts, args)
  49. },
  50. }
  51. flags := cmd.Flags()
  52. flags.StringVar(&opts.Format, "format", "yaml", "Format the output. Values: [yaml | json]")
  53. flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only validate the configuration, don't print anything.")
  54. // add flags for hidden backends
  55. for _, f := range addFlagsFuncs {
  56. f(cmd, &opts)
  57. }
  58. return cmd
  59. }
  60. func runConvert(ctx context.Context, opts convertOptions, services []string) error {
  61. var json []byte
  62. c, err := client.NewWithDefaultLocalBackend(ctx)
  63. if err != nil {
  64. return err
  65. }
  66. project, err := opts.toProject(services)
  67. if err != nil {
  68. return err
  69. }
  70. json, err = c.ComposeService().Convert(ctx, project, compose.ConvertOptions{
  71. Format: opts.Format,
  72. Output: opts.Output,
  73. })
  74. if err != nil {
  75. return err
  76. }
  77. if opts.quiet {
  78. return nil
  79. }
  80. var out io.Writer = os.Stdout
  81. if opts.Output != "" {
  82. file, err := os.Create(opts.Output)
  83. if err != nil {
  84. return err
  85. }
  86. out = bufio.NewWriter(file)
  87. }
  88. _, err = fmt.Fprint(out, string(json))
  89. return err
  90. }