exec.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "fmt"
  17. "os"
  18. "github.com/containerd/console"
  19. "github.com/docker/cli/cli"
  20. "github.com/spf13/cobra"
  21. "github.com/docker/compose-cli/api/compose"
  22. )
  23. type execOpts struct {
  24. *composeOptions
  25. service string
  26. command []string
  27. environment []string
  28. workingDir string
  29. noTty bool
  30. user string
  31. detach bool
  32. index int
  33. privileged bool
  34. }
  35. func execCommand(p *projectOptions, backend compose.Service) *cobra.Command {
  36. opts := execOpts{
  37. composeOptions: &composeOptions{
  38. projectOptions: p,
  39. },
  40. }
  41. runCmd := &cobra.Command{
  42. Use: "exec [options] [-e KEY=VAL...] [--] SERVICE COMMAND [ARGS...]",
  43. Short: "Execute a command in a running container.",
  44. Args: cobra.MinimumNArgs(2),
  45. PreRunE: Adapt(func(ctx context.Context, args []string) error {
  46. opts.service = args[0]
  47. opts.command = args[1:]
  48. return nil
  49. }),
  50. RunE: Adapt(func(ctx context.Context, args []string) error {
  51. return runExec(ctx, backend, opts)
  52. }),
  53. }
  54. runCmd.Flags().BoolVarP(&opts.detach, "detach", "d", false, "Detached mode: Run command in the background.")
  55. runCmd.Flags().StringArrayVarP(&opts.environment, "env", "e", []string{}, "Set environment variables")
  56. runCmd.Flags().IntVar(&opts.index, "index", 1, "index of the container if there are multiple instances of a service [default: 1].")
  57. runCmd.Flags().BoolVarP(&opts.privileged, "privileged", "", false, "Give extended privileges to the process.")
  58. runCmd.Flags().StringVarP(&opts.user, "user", "u", "", "Run the command as this user.")
  59. runCmd.Flags().BoolVarP(&opts.noTty, "", "T", false, "Disable pseudo-TTY allocation. By default `docker compose exec` allocates a TTY.")
  60. runCmd.Flags().StringVarP(&opts.workingDir, "workdir", "w", "", "Path to workdir directory for this command.")
  61. runCmd.Flags().SetInterspersed(false)
  62. return runCmd
  63. }
  64. func runExec(ctx context.Context, backend compose.Service, opts execOpts) error {
  65. project, err := opts.toProject(nil)
  66. if err != nil {
  67. return err
  68. }
  69. execOpts := compose.RunOptions{
  70. Service: opts.service,
  71. Command: opts.command,
  72. Environment: opts.environment,
  73. Tty: !opts.noTty,
  74. User: opts.user,
  75. Privileged: opts.privileged,
  76. Index: opts.index,
  77. Detach: opts.detach,
  78. WorkingDir: opts.workingDir,
  79. Writer: os.Stdout,
  80. Reader: os.Stdin,
  81. }
  82. if execOpts.Tty {
  83. con := console.Current()
  84. if err := con.SetRaw(); err != nil {
  85. return err
  86. }
  87. defer func() {
  88. if err := con.Reset(); err != nil {
  89. fmt.Println("Unable to close the console")
  90. }
  91. }()
  92. execOpts.Writer = con
  93. execOpts.Reader = con
  94. }
  95. exitCode, err := backend.Exec(ctx, project, execOpts)
  96. if exitCode != 0 {
  97. errMsg := ""
  98. if err != nil {
  99. errMsg = err.Error()
  100. }
  101. return cli.StatusError{StatusCode: exitCode, Status: errMsg}
  102. }
  103. return err
  104. }