diff options
author | Daniel J Walsh <dwalsh@redhat.com> | 2017-12-15 16:58:36 -0500 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2017-12-18 16:46:05 +0000 |
commit | 5770dc2640c216525ab84031e3712fcc46b3b087 (patch) | |
tree | 8a1c5c4e4a6ce6a35a3767247623a62bfd698f77 /cmd/podman/attach.go | |
parent | de3468e120d489d046c08dad72ba2262e222ccb1 (diff) | |
download | podman-5770dc2640c216525ab84031e3712fcc46b3b087.tar.gz podman-5770dc2640c216525ab84031e3712fcc46b3b087.tar.bz2 podman-5770dc2640c216525ab84031e3712fcc46b3b087.zip |
Rename all references to kpod to podman
The decision is in, kpod is going to be named podman.
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
Closes: #145
Approved by: umohnani8
Diffstat (limited to 'cmd/podman/attach.go')
-rw-r--r-- | cmd/podman/attach.go | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/cmd/podman/attach.go b/cmd/podman/attach.go new file mode 100644 index 000000000..8c2c99fd5 --- /dev/null +++ b/cmd/podman/attach.go @@ -0,0 +1,86 @@ +package main + +import ( + "sync" + + "github.com/pkg/errors" + "github.com/projectatomic/libpod/libpod" + "github.com/sirupsen/logrus" + "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.", + }, + } + 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: "", + } +) + +func attachCmd(c *cli.Context) error { + args := c.Args() + if err := validateFlags(c, attachFlags); err != nil { + return err + } + + if len(c.Args()) < 1 || len(c.Args()) > 1 { + return errors.Errorf("attach requires the name or id of one running container") + } + + runtime, err := getRuntime(c) + if err != nil { + return errors.Wrapf(err, "error creating libpod runtime") + } + defer runtime.Shutdown(false) + + 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") + } + // Create a bool channel to track that the console socket attach + // is successful. + attached := make(chan bool) + // Create a waitgroup so we can sync and wait for all goroutines + // to finish before exiting main + var wg sync.WaitGroup + + // We increment the wg counter because we need to do the attach + wg.Add(1) + // Attach to the running container + go func() { + logrus.Debugf("trying to attach to the container %s", ctr.ID()) + defer wg.Done() + if err := ctr.Attach(c.Bool("no-stdin"), c.String("detach-keys"), attached); err != nil { + logrus.Errorf("unable to attach to container %s: %q", ctr.ID(), err) + } + }() + if !<-attached { + return errors.Errorf("unable to attach to container %s", ctr.ID()) + } + wg.Wait() + + return nil +} |