compose.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package compose
  2. import (
  3. "github.com/spf13/cobra"
  4. "github.com/docker/api/client"
  5. "github.com/docker/api/compose"
  6. )
  7. // Command returns the compose command with its child commands
  8. func Command() *cobra.Command {
  9. command := &cobra.Command{
  10. Short: "Docker Compose",
  11. Use: "compose",
  12. }
  13. command.AddCommand(
  14. upCommand(),
  15. downCommand(),
  16. )
  17. return command
  18. }
  19. func upCommand() *cobra.Command {
  20. opts := &compose.ProjectOptions{}
  21. upCmd := &cobra.Command{
  22. Use: "up",
  23. RunE: func(cmd *cobra.Command, args []string) error {
  24. c, err := client.New(cmd.Context())
  25. if err != nil {
  26. return err
  27. }
  28. return c.ComposeService().Up(cmd.Context(), *opts)
  29. },
  30. }
  31. upCmd.Flags().StringVar(&opts.Name, "name", "", "Project name")
  32. upCmd.Flags().StringVar(&opts.WorkDir, "workdir", ".", "Work dir")
  33. upCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
  34. upCmd.Flags().StringArrayVarP(&opts.Environment, "environment", "e", []string{}, "Environment variables")
  35. return upCmd
  36. }
  37. func downCommand() *cobra.Command {
  38. opts := &compose.ProjectOptions{}
  39. downCmd := &cobra.Command{
  40. Use: "down",
  41. RunE: func(cmd *cobra.Command, args []string) error {
  42. c, err := client.New(cmd.Context())
  43. if err != nil {
  44. return err
  45. }
  46. return c.ComposeService().Down(cmd.Context(), *opts)
  47. },
  48. }
  49. downCmd.Flags().StringVar(&opts.Name, "name", "", "Project name")
  50. downCmd.Flags().StringVar(&opts.WorkDir, "workdir", ".", "Work dir")
  51. return downCmd
  52. }