From 9eef9eb212cf4c3ec137de9db7eb8b67a8f6c351 Mon Sep 17 00:00:00 2001
From: Marco Vedovati <mvedovati@suse.com>
Date: Thu, 28 Jun 2018 19:08:49 +0200
Subject: Refactor podman/utils with a single container start and attach
 function

Use a single function startAttachCtr() to handle both container start
with attach and attach to running containers, as the code handling the
attach is common for the 2 use cases.

Signed-off-by: Marco Vedovati <mvedovati@suse.com>

Closes: #1025
Approved by: rhatdan
---
 cmd/podman/attach.go |  2 +-
 cmd/podman/run.go    |  2 +-
 cmd/podman/start.go  | 13 ++++++++----
 cmd/podman/utils.go  | 60 ++++++----------------------------------------------
 4 files changed, 17 insertions(+), 60 deletions(-)

diff --git a/cmd/podman/attach.go b/cmd/podman/attach.go
index 7cbf9ca86..f615ce026 100644
--- a/cmd/podman/attach.go
+++ b/cmd/podman/attach.go
@@ -75,7 +75,7 @@ func attachCmd(c *cli.Context) error {
 		inputStream = nil
 	}
 
-	if err := attachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys"), c.BoolT("sig-proxy")); err != 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())
 	}
 
diff --git a/cmd/podman/run.go b/cmd/podman/run.go
index 8eb747686..2964605f6 100644
--- a/cmd/podman/run.go
+++ b/cmd/podman/run.go
@@ -194,7 +194,7 @@ func runCmd(c *cli.Context) error {
 		}
 	}
 
-	if err := startAttachCtr(ctr, outputStream, errorStream, inputStream, c.String("detach-keys"), c.BoolT("sig-proxy")); err != nil {
+	if err := startAttachCtr(ctr, outputStream, errorStream, inputStream, c.String("detach-keys"), c.BoolT("sig-proxy"), true); err != nil {
 		// This means the command did not exist
 		exitCode = 127
 		if strings.Index(err.Error(), "permission denied") > -1 {
diff --git a/cmd/podman/start.go b/cmd/podman/start.go
index 00528d04e..e917d9198 100644
--- a/cmd/podman/start.go
+++ b/cmd/podman/start.go
@@ -96,16 +96,21 @@ func startCmd(c *cli.Context) error {
 			return errors.Wrapf(err, "unable to get container state")
 		}
 
+		ctrRunning := ctrState == libpod.ContainerStateRunning
+
 		if attach {
 			inputStream := os.Stdin
 			if !c.Bool("interactive") {
 				inputStream = nil
 			}
-			if ctrState == libpod.ContainerStateRunning {
-				return attachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys"), c.BoolT("sig-proxy"))
+
+			// attach to the container and also start it not already running
+			err = startAttachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys"), c.Bool("sig-proxy"), !ctrRunning)
+			if ctrRunning {
+				return err
 			}
 
-			if err := startAttachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys"), c.Bool("sig-proxy")); err != nil {
+			if err != nil {
 				return errors.Wrapf(err, "unable to start container %s", ctr.ID())
 			}
 
@@ -117,7 +122,7 @@ func startCmd(c *cli.Context) error {
 
 			return ctr.Cleanup()
 		}
-		if ctrState == libpod.ContainerStateRunning {
+		if ctrRunning {
 			fmt.Println(ctr.ID())
 			continue
 		}
diff --git a/cmd/podman/utils.go b/cmd/podman/utils.go
index 72bdac256..21bc1ae74 100644
--- a/cmd/podman/utils.go
+++ b/cmd/podman/utils.go
@@ -18,8 +18,8 @@ import (
 type RawTtyFormatter struct {
 }
 
-// Attach to a container
-func attachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string, sigProxy bool) error {
+// Start (if required) and attach to a container
+func startAttachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string, sigProxy bool, startContainer bool) error {
 	ctx := context.Background()
 	resize := make(chan remotecommand.TerminalSize)
 
@@ -67,60 +67,12 @@ func attachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys
 		streams.AttachInput = false
 	}
 
-	if sigProxy {
-		ProxySignals(ctr)
-	}
-
-	return ctr.Attach(streams, detachKeys, resize)
-}
-
-// Start and attach to a container
-func startAttachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string, sigProxy bool) error {
-	ctx := context.Background()
-	resize := make(chan remotecommand.TerminalSize)
-
-	haveTerminal := terminal.IsTerminal(int(os.Stdin.Fd()))
-
-	// Check if we are attached to a terminal. If we are, generate resize
-	// events, and set the terminal to raw mode
-	if haveTerminal && ctr.Spec().Process.Terminal {
-		logrus.Debugf("Handling terminal attach")
-
-		subCtx, cancel := context.WithCancel(ctx)
-		defer cancel()
-
-		resizeTty(subCtx, resize)
-
-		oldTermState, err := term.SaveState(os.Stdin.Fd())
-		if err != nil {
-			return errors.Wrapf(err, "unable to save terminal state")
+	if !startContainer {
+		if sigProxy {
+			ProxySignals(ctr)
 		}
 
-		logrus.SetFormatter(&RawTtyFormatter{})
-		term.SetRawTerminal(os.Stdin.Fd())
-
-		defer restoreTerminal(oldTermState)
-	}
-
-	streams := new(libpod.AttachStreams)
-	streams.OutputStream = stdout
-	streams.ErrorStream = stderr
-	streams.InputStream = stdin
-	streams.AttachOutput = true
-	streams.AttachError = true
-	streams.AttachInput = true
-
-	if stdout == nil {
-		logrus.Debugf("Not attaching to stdout")
-		streams.AttachOutput = false
-	}
-	if stderr == nil {
-		logrus.Debugf("Not attaching to stderr")
-		streams.AttachError = false
-	}
-	if stdin == nil {
-		logrus.Debugf("Not attaching to stdin")
-		streams.AttachInput = false
+		return ctr.Attach(streams, detachKeys, resize)
 	}
 
 	attachChan, err := ctr.StartAndAttach(getContext(), streams, detachKeys, resize)
-- 
cgit v1.2.3-54-g00ecf