Browse code

Merge pull request #36815 from allencloud/simplify-ode

refactor: simplify code to make function getExecConfig more readable

Brian Goff authored on 2018/05/11 23:06:33
Showing 1 changed files
... ...
@@ -44,29 +44,29 @@ func (d *Daemon) ExecExists(name string) (bool, error) {
44 44
 // with the exec instance is stopped or paused, it will return an error.
45 45
 func (d *Daemon) getExecConfig(name string) (*exec.Config, error) {
46 46
 	ec := d.execCommands.Get(name)
47
+	if ec == nil {
48
+		return nil, errExecNotFound(name)
49
+	}
47 50
 
48 51
 	// If the exec is found but its container is not in the daemon's list of
49 52
 	// containers then it must have been deleted, in which case instead of
50 53
 	// saying the container isn't running, we should return a 404 so that
51 54
 	// the user sees the same error now that they will after the
52 55
 	// 5 minute clean-up loop is run which erases old/dead execs.
53
-
54
-	if ec != nil {
55
-		if container := d.containers.Get(ec.ContainerID); container != nil {
56
-			if !container.IsRunning() {
57
-				return nil, fmt.Errorf("Container %s is not running: %s", container.ID, container.State.String())
58
-			}
59
-			if container.IsPaused() {
60
-				return nil, errExecPaused(container.ID)
61
-			}
62
-			if container.IsRestarting() {
63
-				return nil, errContainerIsRestarting(container.ID)
64
-			}
65
-			return ec, nil
66
-		}
56
+	container := d.containers.Get(ec.ContainerID)
57
+	if container == nil {
58
+		return nil, containerNotFound(name)
67 59
 	}
68
-
69
-	return nil, errExecNotFound(name)
60
+	if !container.IsRunning() {
61
+		return nil, fmt.Errorf("Container %s is not running: %s", container.ID, container.State.String())
62
+	}
63
+	if container.IsPaused() {
64
+		return nil, errExecPaused(container.ID)
65
+	}
66
+	if container.IsRestarting() {
67
+		return nil, errContainerIsRestarting(container.ID)
68
+	}
69
+	return ec, nil
70 70
 }
71 71
 
72 72
 func (d *Daemon) unregisterExecCommand(container *container.Container, execConfig *exec.Config) {