Guillaume J. Charmes 12 лет назад
Родитель
Сommit
cacc7e564a
3 измененных файлов с 59 добавлено и 60 удалено
  1. 7 7
      api.go
  2. 0 1
      commands.go
  3. 52 52
      server.go

+ 7 - 7
api.go

@@ -97,9 +97,9 @@ func getContainersExport(srv *Server, w http.ResponseWriter, r *http.Request) ([
 
 	in, out, err := hijackServer(w)
 	if err != nil {
-		defer in.Close()
 		return nil, err
 	}
+	defer in.Close()
 	fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
 	if err := srv.ContainerExport(name, out); err != nil {
 		fmt.Fprintf(out, "Error: %s\n", err)
@@ -117,9 +117,9 @@ func getImages(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, err
 	if viz {
 		in, out, err := hijackServer(w)
 		if err != nil {
-			defer in.Close()
 			return nil, err
 		}
+		defer in.Close()
 		fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
 		if err := srv.ImagesViz(out); err != nil {
 			fmt.Fprintf(out, "Error: %s\n", err)
@@ -269,9 +269,9 @@ func postImages(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, er
 
 	in, out, err := hijackServer(w)
 	if err != nil {
-		defer in.Close()
 		return nil, err
 	}
+	defer in.Close()
 	fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
 	if image != "" { //pull
 		registry := r.Form.Get("registry")
@@ -317,9 +317,9 @@ func postImagesInsert(srv *Server, w http.ResponseWriter, r *http.Request) ([]by
 
 	in, out, err := hijackServer(w)
 	if err != nil {
-		defer in.Close()
 		return nil, err
 	}
+	defer in.Close()
 	fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
 	if err := srv.ImageInsert(name, url, path, out); err != nil {
 		fmt.Fprintf(out, "Error: %s\n", err)
@@ -340,9 +340,9 @@ func postImagesPush(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte
 
 	in, out, err := hijackServer(w)
 	if err != nil {
-		defer in.Close()
 		return nil, err
 	}
+	defer in.Close()
 	fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
 	if err := srv.ImagePush(name, registry, out); err != nil {
 		fmt.Fprintln(out, "Error: %s\n", err)
@@ -354,9 +354,9 @@ func postImagesPush(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte
 func postBuild(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	in, out, err := hijackServer(w)
 	if err != nil {
-		defer in.Close()
 		return nil, err
 	}
+	defer in.Close()
 	fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
 	if err := srv.ImageCreateFromFile(in, out); err != nil {
 		fmt.Fprintln(out, "Error: %s\n", err)
@@ -487,9 +487,9 @@ func postContainersAttach(srv *Server, w http.ResponseWriter, r *http.Request) (
 
 	in, out, err := hijackServer(w)
 	if err != nil {
-		defer in.Close()
 		return nil, err
 	}
+	defer in.Close()
 
 	fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
 	if err := srv.ContainerAttach(name, logs, stream, stdin, stdout, stderr, in, out); err != nil {

+ 0 - 1
commands.go

@@ -1140,7 +1140,6 @@ func CmdRun(args ...string) error {
 			return err
 		}
 	}
-
 	if !config.AttachStdout && !config.AttachStderr {
 		fmt.Println(out.Id)
 	}

+ 52 - 52
server.go

@@ -497,66 +497,66 @@ func (srv *Server) ContainerWait(name string) (int, error) {
 }
 
 func (srv *Server) ContainerAttach(name string, logs, stream, stdin, stdout, stderr bool, in io.ReadCloser, out io.Writer) error {
-	if container := srv.runtime.Get(name); container != nil {
-		//logs
-		if logs {
-			if stdout {
-				cLog, err := container.ReadLog("stdout")
-				if err != nil {
-					Debugf(err.Error())
-				} else if _, err := io.Copy(out, cLog); err != nil {
-					Debugf(err.Error())
-				}
+	container := srv.runtime.Get(name)
+	if container == nil {
+		return fmt.Errorf("No such container: %s", name)
+	}
+	//logs
+	if logs {
+		if stdout {
+			cLog, err := container.ReadLog("stdout")
+			if err != nil {
+				Debugf(err.Error())
+			} else if _, err := io.Copy(out, cLog); err != nil {
+				Debugf(err.Error())
 			}
-			if stderr {
-				cLog, err := container.ReadLog("stderr")
-				if err != nil {
-					Debugf(err.Error())
-				} else if _, err := io.Copy(out, cLog); err != nil {
-					Debugf(err.Error())
-				}
+		}
+		if stderr {
+			cLog, err := container.ReadLog("stderr")
+			if err != nil {
+				Debugf(err.Error())
+			} else if _, err := io.Copy(out, cLog); err != nil {
+				Debugf(err.Error())
 			}
 		}
+	}
 
-		//stream
-		if stream {
-			if container.State.Ghost {
-				return fmt.Errorf("Impossible to attach to a ghost container")
-			}
+	//stream
+	if stream {
+		if container.State.Ghost {
+			return fmt.Errorf("Impossible to attach to a ghost container")
+		}
 
-			var (
-				cStdin           io.ReadCloser
-				cStdout, cStderr io.Writer
-				cStdinCloser     io.Closer
-			)
-
-			if stdin {
-				r, w := io.Pipe()
-				go func() {
-					defer w.Close()
-					defer Debugf("Closing buffered stdin pipe")
-					io.Copy(w, in)
-				}()
-				cStdin = r
-				cStdinCloser = in
-			}
-			if stdout {
-				cStdout = out
-			}
-			if stderr {
-				cStderr = out
-			}
+		var (
+			cStdin           io.ReadCloser
+			cStdout, cStderr io.Writer
+			cStdinCloser     io.Closer
+		)
 
-			<-container.Attach(cStdin, cStdinCloser, cStdout, cStderr)
+		if stdin {
+			r, w := io.Pipe()
+			go func() {
+				defer w.Close()
+				defer Debugf("Closing buffered stdin pipe")
+				io.Copy(w, in)
+			}()
+			cStdin = r
+			cStdinCloser = in
+		}
+		if stdout {
+			cStdout = out
+		}
+		if stderr {
+			cStderr = out
+		}
 
-			// If we are in stdinonce mode, wait for the process to end
-			// otherwise, simply return
-			if container.Config.StdinOnce && !container.Config.Tty {
-				container.Wait()
-			}
+		<-container.Attach(cStdin, cStdinCloser, cStdout, cStderr)
+
+		// If we are in stdinonce mode, wait for the process to end
+		// otherwise, simply return
+		if container.Config.StdinOnce && !container.Config.Tty {
+			container.Wait()
 		}
-	} else {
-		return fmt.Errorf("No such container: %s", name)
 	}
 	return nil
 }