aboutsummaryrefslogtreecommitdiff
path: root/cmd/podman/attach.go
blob: 5b58c022a47f2dfe08cbd8a0a0d7ed3bd6bbd680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main

import (
	"os"

	"github.com/containers/libpod/cmd/podman/libpodruntime"
	"github.com/containers/libpod/libpod"
	"github.com/pkg/errors"
	"github.com/urfave/cli"
)

var (
	attachFlags = []cli.Flag{
		cli.StringFlag{
			Name:  "detach-keys",
			Usage: "Override the key sequence for detaching a container. Format is a single character [a-Z] or ctrl-<value> where <value> is one of: a-z, @, ^, [, , or _.",
		},
		cli.BoolFlag{
			Name:  "no-stdin",
			Usage: "Do not attach STDIN. The default is false.",
		},
		cli.BoolTFlag{
			Name:  "sig-proxy",
			Usage: "proxy received signals to the process (default true)",
		},
		LatestFlag,
	}
	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."
	attachCommand     = cli.Command{
		Name:         "attach",
		Usage:        "Attach to a running container",
		Description:  attachDescription,
		Flags:        attachFlags,
		Action:       attachCmd,
		ArgsUsage:    "",
		OnUsageError: usageErrorHandler,
	}
)

func attachCmd(c *cli.Context) error {
	args := c.Args()
	var ctr *libpod.Container
	if err := validateFlags(c, attachFlags); err != nil {
		return err
	}
	if len(c.Args()) > 1 || (len(c.Args()) == 0 && !c.Bool("latest")) {
		return errors.Errorf("attach requires the name or id of one running container or the latest flag")
	}

	runtime, err := libpodruntime.GetRuntime(c)
	if err != nil {
		return errors.Wrapf(err, "error creating libpod runtime")
	}
	defer runtime.Shutdown(false)

	if c.Bool("latest") {
		ctr, err = runtime.GetLatestContainer()
	} else {
		ctr, err = runtime.LookupContainer(args[0])
	}

	if err != nil {
		return errors.Wrapf(err, "unable to exec into %s", args[0])
	}

	conState, err := ctr.State()
	if err != nil {
		return errors.Wrapf(err, "unable to determine state of %s", args[0])
	}
	if conState != libpod.ContainerStateRunning {
		return errors.Errorf("you can only attach to running containers")
	}

	inputStream := os.Stdin
	if c.Bool("no-stdin") {
		inputStream = nil
	}

	if err := startAttachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys"), c.BoolT("sig-proxy"), false); err != nil {
		return errors.Wrapf(err, "error attaching to container %s", ctr.ID())
	}

	return nil
}