MCPcopy Index your code
hub / github.com/ddev/ddev / ExecWithTty

Method ExecWithTty

pkg/ddevapp/ddevapp.go:2578–2631  ·  view source on GitHub ↗

ExecWithTty executes a given command in the container of given type. It allocates a pty for interactive work.

(opts *ExecOpts)

Source from the content-addressed store, hash-verified

2576// ExecWithTty executes a given command in the container of given type.
2577// It allocates a pty for interactive work.
2578func (app *DdevApp) ExecWithTty(opts *ExecOpts) error {
2579 _ = app.DockerEnv()
2580
2581 if opts.Service == "" {
2582 opts.Service = "web"
2583 }
2584
2585 state, err := dockerutil.GetContainerStateByName(fmt.Sprintf("ddev-%s-%s", app.Name, opts.Service))
2586 if err != nil || state != "running" {
2587 return fmt.Errorf("service %s is not running in project %s (state=%s)", opts.Service, app.Name, state)
2588 }
2589
2590 args := []string{"exec"}
2591
2592 // In the case where this is being used without an available tty,
2593 // make sure we use the -T to turn off tty to avoid panic in docker-compose v2.2.3
2594 // see https://stackoverflow.com/questions/70855915/fix-panic-provided-file-is-not-a-console-from-docker-compose-in-github-action
2595 if !term.IsTerminal(int(os.Stdin.Fd())) {
2596 args = append(args, "-T")
2597 }
2598
2599 if opts.Dir != "" {
2600 args = append(args, "-w", opts.Dir)
2601 }
2602
2603 if opts.User != "" {
2604 args = append(args, "-u", opts.User)
2605 }
2606
2607 args = append(args, opts.Service)
2608
2609 if opts.Cmd == "" {
2610 return fmt.Errorf("no command provided")
2611 }
2612
2613 // Cases to handle
2614 // - Free form, all unquoted. Like `ls -l -a`
2615 // - Quoted to delay pipes and other features to container, like `"ls -l -a | grep junk"`
2616 // Note that a set quoted on the host in ddev exec will come through as a single arg
2617
2618 // Use Bash for our containers, sh for 3rd-party containers
2619 // that may not have Bash.
2620 shell := "bash"
2621 if !nodeps.ArrayContainsString([]string{"web", "db"}, opts.Service) {
2622 shell = "sh"
2623 }
2624
2625 args = append(args, shell, "-c", opts.Cmd)
2626
2627 return dockerutil.ComposeWithStreams(&dockerutil.ComposeCmdOpts{
2628 ComposeFiles: []string{app.DockerComposeFullRenderedYAMLPath()},
2629 Action: args,
2630 }, os.Stdin, os.Stdout, os.Stderr)
2631}
2632
2633func (app *DdevApp) ExecOnHostOrService(service string, cmd string) error {
2634 var err error

Callers 2

TestDdevExecFunction · 0.95
ssh.goFile · 0.80

Calls 6

DockerEnvMethod · 0.95
GetContainerStateByNameFunction · 0.92
ArrayContainsStringFunction · 0.92
ComposeWithStreamsFunction · 0.92
ErrorfMethod · 0.80

Tested by 1

TestDdevExecFunction · 0.76