github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/attach.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/containers/libpod/cmd/podman/cliconfig"
     5  	"github.com/containers/libpod/pkg/adapter"
     6  	"github.com/pkg/errors"
     7  	"github.com/spf13/cobra"
     8  )
     9  
    10  var (
    11  	attachCommand     cliconfig.AttachValues
    12  	attachDescription = "The podman attach command allows you to attach to a running container using the container's ID or name, either to view its ongoing output or to control it interactively."
    13  	_attachCommand    = &cobra.Command{
    14  		Use:   "attach [flags] CONTAINER",
    15  		Short: "Attach to a running container",
    16  		Long:  attachDescription,
    17  		RunE: func(cmd *cobra.Command, args []string) error {
    18  			attachCommand.InputArgs = args
    19  			attachCommand.GlobalFlags = MainGlobalOpts
    20  			attachCommand.Remote = remoteclient
    21  			return attachCmd(&attachCommand)
    22  		},
    23  		Example: `podman attach ctrID
    24    podman attach 1234
    25    podman attach --no-stdin foobar`,
    26  	}
    27  )
    28  
    29  func init() {
    30  	attachCommand.Command = _attachCommand
    31  	attachCommand.SetHelpTemplate(HelpTemplate())
    32  	attachCommand.SetUsageTemplate(UsageTemplate())
    33  	flags := attachCommand.Flags()
    34  	flags.StringVar(&attachCommand.DetachKeys, "detach-keys", getDefaultDetachKeys(), "Select the key sequence for detaching a container. Format is a single character `[a-Z]` or a comma separated sequence of `ctrl-<value>`, where `<value>` is one of: `a-z`, `@`, `^`, `[`, `\\`, `]`, `^` or `_`")
    35  	flags.BoolVar(&attachCommand.NoStdin, "no-stdin", false, "Do not attach STDIN. The default is false")
    36  	flags.BoolVar(&attachCommand.SigProxy, "sig-proxy", true, "Proxy received signals to the process")
    37  	flags.BoolVarP(&attachCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
    38  	markFlagHiddenForRemoteClient("latest", flags)
    39  	// TODO allow for passing of a new detach keys
    40  	markFlagHiddenForRemoteClient("detach-keys", flags)
    41  }
    42  
    43  func attachCmd(c *cliconfig.AttachValues) error {
    44  	if len(c.InputArgs) > 1 || (len(c.InputArgs) == 0 && !c.Latest) {
    45  		return errors.Errorf("attach requires the name or id of one running container or the latest flag")
    46  	}
    47  	if remoteclient && len(c.InputArgs) != 1 {
    48  		return errors.Errorf("attach requires the name or id of one running container")
    49  	}
    50  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    51  	if err != nil {
    52  		return errors.Wrapf(err, "error creating runtime")
    53  	}
    54  	defer runtime.DeferredShutdown(false)
    55  	return runtime.Attach(getContext(), c)
    56  }