convert.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/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. options, err := opts.toProjectOptions()
  28. if err != nil {
  29. return err
  30. }
  31. return runConvert(cmd.Context(), options)
  32. },
  33. }
  34. convertCmd.Flags().StringVarP(&opts.Name, "project-name", "p", "", "Project name")
  35. convertCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir")
  36. convertCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
  37. return convertCmd
  38. }
  39. func runConvert(ctx context.Context, opts *cli.ProjectOptions) error {
  40. var json []byte
  41. c, err := client.New(ctx)
  42. if err != nil {
  43. return err
  44. }
  45. json, err = c.ComposeService().Convert(ctx, opts)
  46. if err != nil {
  47. return err
  48. }
  49. fmt.Println(string(json))
  50. return nil
  51. }