mirror of
				https://github.com/docker/compose.git
				synced 2025-11-04 13:44:48 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			67 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package cmd
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"fmt"
 | 
						|
	"io"
 | 
						|
	"os"
 | 
						|
	"strings"
 | 
						|
 | 
						|
	"github.com/containerd/console"
 | 
						|
	"github.com/pkg/errors"
 | 
						|
	"github.com/spf13/cobra"
 | 
						|
 | 
						|
	"github.com/docker/api/client"
 | 
						|
)
 | 
						|
 | 
						|
type execOpts struct {
 | 
						|
	Tty bool
 | 
						|
}
 | 
						|
 | 
						|
// ExecCommand runs a command in a running container
 | 
						|
func ExecCommand() *cobra.Command {
 | 
						|
	var opts execOpts
 | 
						|
	cmd := &cobra.Command{
 | 
						|
		Use:   "exec",
 | 
						|
		Short: "Run a command in a running container",
 | 
						|
		Args:  cobra.MinimumNArgs(2),
 | 
						|
		RunE: func(cmd *cobra.Command, args []string) error {
 | 
						|
			return runExec(cmd.Context(), opts, args[0], strings.Join(args[1:], " "))
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	cmd.Flags().BoolVarP(&opts.Tty, "tty", "t", false, "Allocate a pseudo-TTY")
 | 
						|
 | 
						|
	return cmd
 | 
						|
}
 | 
						|
 | 
						|
func runExec(ctx context.Context, opts execOpts, name string, command string) error {
 | 
						|
	c, err := client.New(ctx)
 | 
						|
	if err != nil {
 | 
						|
		return errors.Wrap(err, "cannot connect to backend")
 | 
						|
	}
 | 
						|
 | 
						|
	var (
 | 
						|
		con    console.Console
 | 
						|
		stdout io.Writer
 | 
						|
	)
 | 
						|
 | 
						|
	stdout = os.Stdout
 | 
						|
 | 
						|
	if opts.Tty {
 | 
						|
		con = console.Current()
 | 
						|
		if err := con.SetRaw(); err != nil {
 | 
						|
			return err
 | 
						|
		}
 | 
						|
		defer func() {
 | 
						|
			if err := con.Reset(); err != nil {
 | 
						|
				fmt.Println("Unable to close the console")
 | 
						|
			}
 | 
						|
		}()
 | 
						|
 | 
						|
		stdout = con
 | 
						|
	}
 | 
						|
 | 
						|
	return c.ContainerService().Exec(ctx, name, command, os.Stdin, stdout)
 | 
						|
}
 |