Browse code

Modufy docker inspect client to check statusCode instead of strings contains

Signed-off-by: Shuwei Hao <haosw@cn.ibm.com>

Shuwei Hao authored on 2015/12/07 14:22:37
Showing 1 changed files
... ...
@@ -5,6 +5,7 @@ import (
5 5
 	"encoding/json"
6 6
 	"fmt"
7 7
 	"io"
8
+	"net/http"
8 9
 	"net/url"
9 10
 	"strings"
10 11
 	"text/template"
... ...
@@ -36,6 +37,7 @@ func (cli *DockerCli) CmdInspect(args ...string) error {
36 36
 	var tmpl *template.Template
37 37
 	var err error
38 38
 	var obj []byte
39
+	var statusCode int
39 40
 
40 41
 	if *tmplStr != "" {
41 42
 		if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil {
... ...
@@ -60,13 +62,13 @@ func (cli *DockerCli) CmdInspect(args ...string) error {
60 60
 
61 61
 	for _, name := range cmd.Args() {
62 62
 		if *inspectType == "" || *inspectType == "container" {
63
-			obj, _, err = readBody(cli.call("GET", "/containers/"+name+"/json?"+v.Encode(), nil, nil))
63
+			obj, statusCode, err = readBody(cli.call("GET", "/containers/"+name+"/json?"+v.Encode(), nil, nil))
64 64
 			if err != nil {
65 65
 				if err == errConnectionFailed {
66 66
 					return err
67 67
 				}
68 68
 				if *inspectType == "container" {
69
-					if strings.Contains(err.Error(), "No such") {
69
+					if statusCode == http.StatusNotFound {
70 70
 						fmt.Fprintf(cli.err, "Error: No such container: %s\n", name)
71 71
 					} else {
72 72
 						fmt.Fprintf(cli.err, "%s", err)
... ...
@@ -78,13 +80,13 @@ func (cli *DockerCli) CmdInspect(args ...string) error {
78 78
 		}
79 79
 
80 80
 		if obj == nil && (*inspectType == "" || *inspectType == "image") {
81
-			obj, _, err = readBody(cli.call("GET", "/images/"+name+"/json", nil, nil))
81
+			obj, statusCode, err = readBody(cli.call("GET", "/images/"+name+"/json", nil, nil))
82 82
 			isImage = true
83 83
 			if err != nil {
84 84
 				if err == errConnectionFailed {
85 85
 					return err
86 86
 				}
87
-				if strings.Contains(err.Error(), "No such") {
87
+				if statusCode == http.StatusNotFound {
88 88
 					if *inspectType == "" {
89 89
 						fmt.Fprintf(cli.err, "Error: No such image or container: %s\n", name)
90 90
 					} else {