create.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. "fmt"
  16. "github.com/spf13/cobra"
  17. "github.com/docker/compose-cli/api/compose"
  18. )
  19. type createOptions struct {
  20. *composeOptions
  21. forceRecreate bool
  22. noRecreate bool
  23. }
  24. func createCommand(p *projectOptions, backend compose.Service) *cobra.Command {
  25. opts := createOptions{
  26. composeOptions: &composeOptions{},
  27. }
  28. cmd := &cobra.Command{
  29. Use: "create [SERVICE...]",
  30. Short: "Creates containers for a service.",
  31. RunE: func(cmd *cobra.Command, args []string) error {
  32. if opts.Build && opts.noBuild {
  33. return fmt.Errorf("--build and --no-build are incompatible")
  34. }
  35. if opts.forceRecreate && opts.noRecreate {
  36. return fmt.Errorf("--force-recreate and --no-recreate are incompatible")
  37. }
  38. return runCreateStart(cmd.Context(), backend, upOptions{
  39. composeOptions: &composeOptions{
  40. projectOptions: p,
  41. Build: opts.Build,
  42. noBuild: opts.noBuild,
  43. },
  44. noStart: true,
  45. forceRecreate: opts.forceRecreate,
  46. noRecreate: opts.noRecreate,
  47. }, args)
  48. },
  49. }
  50. flags := cmd.Flags()
  51. flags.BoolVar(&opts.Build, "build", false, "Build images before starting containers.")
  52. flags.BoolVar(&opts.noBuild, "no-build", false, "Don't build an image, even if it's missing.")
  53. flags.BoolVar(&opts.forceRecreate, "force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
  54. flags.BoolVar(&opts.noRecreate, "no-recreate", false, "If containers already exist, don't recreate them. Incompatible with --force-recreate.")
  55. return cmd
  56. }