Browse code

refactor attach to not use internal data structures

- refactor to make it easier to split the api in the future

Signed-off-by: Morgan Bauer <mbauer@us.ibm.com>

Morgan Bauer authored on 2015/09/18 04:57:57
Showing 2 changed files
... ...
@@ -444,10 +444,10 @@ func (s *Server) postContainersAttach(ctx context.Context, w http.ResponseWriter
444 444
 	if vars == nil {
445 445
 		return fmt.Errorf("Missing parameter")
446 446
 	}
447
+	containerName := vars["name"]
447 448
 
448
-	cont, err := s.daemon.Get(vars["name"])
449
-	if err != nil {
450
-		return err
449
+	if !s.daemon.Exists(containerName) {
450
+		return derr.ErrorCodeNoSuchContainer.WithArgs(containerName)
451 451
 	}
452 452
 
453 453
 	inStream, outStream, err := hijackServer(w)
... ...
@@ -472,7 +472,7 @@ func (s *Server) postContainersAttach(ctx context.Context, w http.ResponseWriter
472 472
 		Stream:    boolValue(r, "stream"),
473 473
 	}
474 474
 
475
-	if err := s.daemon.ContainerAttachWithLogs(cont, attachWithLogsConfig); err != nil {
475
+	if err := s.daemon.ContainerAttachWithLogs(containerName, attachWithLogsConfig); err != nil {
476 476
 		fmt.Fprintf(outStream, "Error attaching: %s\n", err)
477 477
 	}
478 478
 
... ...
@@ -486,10 +486,10 @@ func (s *Server) wsContainersAttach(ctx context.Context, w http.ResponseWriter,
486 486
 	if vars == nil {
487 487
 		return fmt.Errorf("Missing parameter")
488 488
 	}
489
+	containerName := vars["name"]
489 490
 
490
-	cont, err := s.daemon.Get(vars["name"])
491
-	if err != nil {
492
-		return err
491
+	if !s.daemon.Exists(containerName) {
492
+		return derr.ErrorCodeNoSuchContainer.WithArgs(containerName)
493 493
 	}
494 494
 
495 495
 	h := websocket.Handler(func(ws *websocket.Conn) {
... ...
@@ -503,7 +503,7 @@ func (s *Server) wsContainersAttach(ctx context.Context, w http.ResponseWriter,
503 503
 			Stream:    boolValue(r, "stream"),
504 504
 		}
505 505
 
506
-		if err := s.daemon.ContainerWsAttachWithLogs(cont, wsAttachWithLogsConfig); err != nil {
506
+		if err := s.daemon.ContainerWsAttachWithLogs(containerName, wsAttachWithLogsConfig); err != nil {
507 507
 			logrus.Errorf("Error attaching websocket: %s", err)
508 508
 		}
509 509
 	})
... ...
@@ -15,7 +15,12 @@ type ContainerAttachWithLogsConfig struct {
15 15
 }
16 16
 
17 17
 // ContainerAttachWithLogs attaches to logs according to the config passed in. See ContainerAttachWithLogsConfig.
18
-func (daemon *Daemon) ContainerAttachWithLogs(container *Container, c *ContainerAttachWithLogsConfig) error {
18
+func (daemon *Daemon) ContainerAttachWithLogs(prefixOrName string, c *ContainerAttachWithLogsConfig) error {
19
+	container, err := daemon.Get(prefixOrName)
20
+	if err != nil {
21
+		return err
22
+	}
23
+
19 24
 	var errStream io.Writer
20 25
 
21 26
 	if !container.Config.Tty {
... ...
@@ -50,6 +55,10 @@ type ContainerWsAttachWithLogsConfig struct {
50 50
 }
51 51
 
52 52
 // ContainerWsAttachWithLogs websocket connection
53
-func (daemon *Daemon) ContainerWsAttachWithLogs(container *Container, c *ContainerWsAttachWithLogsConfig) error {
53
+func (daemon *Daemon) ContainerWsAttachWithLogs(prefixOrName string, c *ContainerWsAttachWithLogsConfig) error {
54
+	container, err := daemon.Get(prefixOrName)
55
+	if err != nil {
56
+		return err
57
+	}
54 58
 	return container.attachWithLogs(c.InStream, c.OutStream, c.ErrStream, c.Logs, c.Stream)
55 59
 }