up.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "context"
  16. "github.com/spf13/cobra"
  17. "github.com/compose-spec/compose-go/cli"
  18. "github.com/docker/compose-cli/api/client"
  19. "github.com/docker/compose-cli/context/store"
  20. "github.com/docker/compose-cli/progress"
  21. )
  22. func upCommand(contextType string) *cobra.Command {
  23. opts := composeOptions{}
  24. upCmd := &cobra.Command{
  25. Use: "up",
  26. RunE: func(cmd *cobra.Command, args []string) error {
  27. return runUp(cmd.Context(), opts)
  28. },
  29. }
  30. upCmd.Flags().StringVarP(&opts.Name, "project-name", "p", "", "Project name")
  31. upCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir")
  32. upCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
  33. upCmd.Flags().StringArrayVarP(&opts.Environment, "environment", "e", []string{}, "Environment variables")
  34. upCmd.Flags().BoolP("detach", "d", true, " Detached mode: Run containers in the background")
  35. if contextType == store.AciContextType {
  36. upCmd.Flags().StringVar(&opts.DomainName, "domainname", "", "Container NIS domain name")
  37. }
  38. return upCmd
  39. }
  40. func runUp(ctx context.Context, opts composeOptions) error {
  41. c, err := client.New(ctx)
  42. if err != nil {
  43. return err
  44. }
  45. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  46. options, err := opts.toProjectOptions()
  47. if err != nil {
  48. return "", err
  49. }
  50. project, err := cli.ProjectFromOptions(options)
  51. if opts.DomainName != "" {
  52. //arbitrarily set the domain name on the first service ; ACI backend will expose the entire project
  53. project.Services[0].DomainName = opts.DomainName
  54. }
  55. if err != nil {
  56. return "", err
  57. }
  58. return "", c.ComposeService().Up(ctx, project)
  59. })
  60. return err
  61. }