Browse code

Merge pull request #11665 from runcom/11613-api-exec-start-structured-response

Return ContainerExecCreateResponse from container exec create API endpoint

Jessie Frazelle authored on 2015/03/24 09:52:15
Showing 3 changed files
... ...
@@ -2677,12 +2677,15 @@ func (cli *DockerCli) CmdExec(args ...string) error {
2677 2677
 		return err
2678 2678
 	}
2679 2679
 
2680
-	var execResult engine.Env
2681
-	if err := execResult.Decode(stream); err != nil {
2680
+	var response types.ContainerExecCreateResponse
2681
+	if err := json.NewDecoder(stream).Decode(&response); err != nil {
2682 2682
 		return err
2683 2683
 	}
2684
+	for _, warning := range response.Warnings {
2685
+		fmt.Fprintf(cli.err, "WARNING: %s\n", warning)
2686
+	}
2684 2687
 
2685
-	execID := execResult.Get("Id")
2688
+	execID := response.ID
2686 2689
 
2687 2690
 	if execID == "" {
2688 2691
 		fmt.Fprintf(cli.out, "exec ID empty")
... ...
@@ -1155,10 +1155,11 @@ func postContainerExecCreate(eng *engine.Engine, version version.Version, w http
1155 1155
 		return nil
1156 1156
 	}
1157 1157
 	var (
1158
-		out          engine.Env
1159 1158
 		name         = vars["name"]
1160 1159
 		job          = eng.Job("execCreate", name)
1161 1160
 		stdoutBuffer = bytes.NewBuffer(nil)
1161
+		outWarnings  []string
1162
+		warnings     = bytes.NewBuffer(nil)
1162 1163
 	)
1163 1164
 
1164 1165
 	if err := job.DecodeEnv(r.Body); err != nil {
... ...
@@ -1166,15 +1167,23 @@ func postContainerExecCreate(eng *engine.Engine, version version.Version, w http
1166 1166
 	}
1167 1167
 
1168 1168
 	job.Stdout.Add(stdoutBuffer)
1169
+	// Read warnings from stderr
1170
+	job.Stderr.Add(warnings)
1169 1171
 	// Register an instance of Exec in container.
1170 1172
 	if err := job.Run(); err != nil {
1171 1173
 		fmt.Fprintf(os.Stderr, "Error setting up exec command in container %s: %s\n", name, err)
1172 1174
 		return err
1173 1175
 	}
1174
-	// Return the ID
1175
-	out.Set("Id", engine.Tail(stdoutBuffer, 1))
1176
+	// Parse warnings from stderr
1177
+	scanner := bufio.NewScanner(warnings)
1178
+	for scanner.Scan() {
1179
+		outWarnings = append(outWarnings, scanner.Text())
1180
+	}
1176 1181
 
1177
-	return writeJSONEnv(w, http.StatusCreated, out)
1182
+	return writeJSON(w, http.StatusCreated, &types.ContainerExecCreateResponse{
1183
+		ID:       engine.Tail(stdoutBuffer, 1),
1184
+		Warnings: outWarnings,
1185
+	})
1178 1186
 }
1179 1187
 
1180 1188
 // TODO(vishh): Refactor the code to avoid having to specify stream config as part of both create and start.
... ...
@@ -9,3 +9,12 @@ type ContainerCreateResponse struct {
9 9
 	// Warnings are any warnings encountered during the creation of the container.
10 10
 	Warnings []string `json:"Warnings"`
11 11
 }
12
+
13
+// POST /containers/{name:.*}/exec
14
+type ContainerExecCreateResponse struct {
15
+	// ID is the exec ID.
16
+	ID string `json:"Id"`
17
+
18
+	// Warnings are any warnings encountered during the execution of the command.
19
+	Warnings []string `json:"Warnings"`
20
+}