convert.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. Copyright 2020 Docker, Inc.
  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. "context"
  16. "fmt"
  17. "github.com/compose-spec/compose-go/cli"
  18. "github.com/spf13/cobra"
  19. "github.com/docker/compose-cli/api/client"
  20. )
  21. func convertCommand() *cobra.Command {
  22. opts := composeOptions{}
  23. convertCmd := &cobra.Command{
  24. Use: "convert",
  25. Short: "Converts the compose file to a cloud format (default: cloudformation)",
  26. RunE: func(cmd *cobra.Command, args []string) error {
  27. return runConvert(cmd.Context(), opts)
  28. },
  29. }
  30. convertCmd.Flags().StringVarP(&opts.Name, "project-name", "p", "", "Project name")
  31. convertCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir")
  32. convertCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
  33. convertCmd.Flags().StringArrayVarP(&opts.Environment, "environment", "e", []string{}, "Environment variables")
  34. return convertCmd
  35. }
  36. func runConvert(ctx context.Context, opts composeOptions) error {
  37. var json []byte
  38. c, err := client.New(ctx)
  39. if err != nil {
  40. return err
  41. }
  42. options, err := opts.toProjectOptions()
  43. if err != nil {
  44. return err
  45. }
  46. project, err := cli.ProjectFromOptions(options)
  47. if err != nil {
  48. return err
  49. }
  50. json, err = c.ComposeService().Convert(ctx, project)
  51. if err != nil {
  52. return err
  53. }
  54. fmt.Println(string(json))
  55. return nil
  56. }