mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #18463 from haoshuwei/modify-containerinspect-tocheckstatuscode
Modify docker inspect client to check statusCode instead of strings c…
This commit is contained in:
commit
2ecbc9774b
1 changed files with 6 additions and 4 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
@ -36,6 +37,7 @@ func (cli *DockerCli) CmdInspect(args ...string) error {
|
||||||
var tmpl *template.Template
|
var tmpl *template.Template
|
||||||
var err error
|
var err error
|
||||||
var obj []byte
|
var obj []byte
|
||||||
|
var statusCode int
|
||||||
|
|
||||||
if *tmplStr != "" {
|
if *tmplStr != "" {
|
||||||
if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil {
|
if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil {
|
||||||
|
@ -60,13 +62,13 @@ func (cli *DockerCli) CmdInspect(args ...string) error {
|
||||||
|
|
||||||
for _, name := range cmd.Args() {
|
for _, name := range cmd.Args() {
|
||||||
if *inspectType == "" || *inspectType == "container" {
|
if *inspectType == "" || *inspectType == "container" {
|
||||||
obj, _, err = readBody(cli.call("GET", "/containers/"+name+"/json?"+v.Encode(), nil, nil))
|
obj, statusCode, err = readBody(cli.call("GET", "/containers/"+name+"/json?"+v.Encode(), nil, nil))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == errConnectionFailed {
|
if err == errConnectionFailed {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if *inspectType == "container" {
|
if *inspectType == "container" {
|
||||||
if strings.Contains(err.Error(), "No such") {
|
if statusCode == http.StatusNotFound {
|
||||||
fmt.Fprintf(cli.err, "Error: No such container: %s\n", name)
|
fmt.Fprintf(cli.err, "Error: No such container: %s\n", name)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(cli.err, "%s", err)
|
fmt.Fprintf(cli.err, "%s", err)
|
||||||
|
@ -78,13 +80,13 @@ func (cli *DockerCli) CmdInspect(args ...string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj == nil && (*inspectType == "" || *inspectType == "image") {
|
if obj == nil && (*inspectType == "" || *inspectType == "image") {
|
||||||
obj, _, err = readBody(cli.call("GET", "/images/"+name+"/json", nil, nil))
|
obj, statusCode, err = readBody(cli.call("GET", "/images/"+name+"/json", nil, nil))
|
||||||
isImage = true
|
isImage = true
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == errConnectionFailed {
|
if err == errConnectionFailed {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if strings.Contains(err.Error(), "No such") {
|
if statusCode == http.StatusNotFound {
|
||||||
if *inspectType == "" {
|
if *inspectType == "" {
|
||||||
fmt.Fprintf(cli.err, "Error: No such image or container: %s\n", name)
|
fmt.Fprintf(cli.err, "Error: No such image or container: %s\n", name)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Reference in a new issue