github.com/hamo/docker@v1.11.1/api/client/inspect.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/net/context"
     7  
     8  	"github.com/docker/docker/api/client/inspect"
     9  	Cli "github.com/docker/docker/cli"
    10  	flag "github.com/docker/docker/pkg/mflag"
    11  	"github.com/docker/docker/utils/templates"
    12  	"github.com/docker/engine-api/client"
    13  )
    14  
    15  // CmdInspect displays low-level information on one or more containers or images.
    16  //
    17  // Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]
    18  func (cli *DockerCli) CmdInspect(args ...string) error {
    19  	cmd := Cli.Subcmd("inspect", []string{"CONTAINER|IMAGE [CONTAINER|IMAGE...]"}, Cli.DockerCommands["inspect"].Description, true)
    20  	tmplStr := cmd.String([]string{"f", "-format"}, "", "Format the output using the given go template")
    21  	inspectType := cmd.String([]string{"-type"}, "", "Return JSON for specified type, (e.g image or container)")
    22  	size := cmd.Bool([]string{"s", "-size"}, false, "Display total file sizes if the type is container")
    23  	cmd.Require(flag.Min, 1)
    24  
    25  	cmd.ParseFlags(args, true)
    26  
    27  	if *inspectType != "" && *inspectType != "container" && *inspectType != "image" {
    28  		return fmt.Errorf("%q is not a valid value for --type", *inspectType)
    29  	}
    30  
    31  	var elementSearcher inspectSearcher
    32  	switch *inspectType {
    33  	case "container":
    34  		elementSearcher = cli.inspectContainers(*size)
    35  	case "image":
    36  		elementSearcher = cli.inspectImages(*size)
    37  	default:
    38  		elementSearcher = cli.inspectAll(*size)
    39  	}
    40  
    41  	return cli.inspectElements(*tmplStr, cmd.Args(), elementSearcher)
    42  }
    43  
    44  func (cli *DockerCli) inspectContainers(getSize bool) inspectSearcher {
    45  	return func(ref string) (interface{}, []byte, error) {
    46  		return cli.client.ContainerInspectWithRaw(context.Background(), ref, getSize)
    47  	}
    48  }
    49  
    50  func (cli *DockerCli) inspectImages(getSize bool) inspectSearcher {
    51  	return func(ref string) (interface{}, []byte, error) {
    52  		return cli.client.ImageInspectWithRaw(context.Background(), ref, getSize)
    53  	}
    54  }
    55  
    56  func (cli *DockerCli) inspectAll(getSize bool) inspectSearcher {
    57  	return func(ref string) (interface{}, []byte, error) {
    58  		c, rawContainer, err := cli.client.ContainerInspectWithRaw(context.Background(), ref, getSize)
    59  		if err != nil {
    60  			// Search for image with that id if a container doesn't exist.
    61  			if client.IsErrContainerNotFound(err) {
    62  				i, rawImage, err := cli.client.ImageInspectWithRaw(context.Background(), ref, getSize)
    63  				if err != nil {
    64  					if client.IsErrImageNotFound(err) {
    65  						return nil, nil, fmt.Errorf("Error: No such image or container: %s", ref)
    66  					}
    67  					return nil, nil, err
    68  				}
    69  				return i, rawImage, err
    70  			}
    71  			return nil, nil, err
    72  		}
    73  		return c, rawContainer, err
    74  	}
    75  }
    76  
    77  type inspectSearcher func(ref string) (interface{}, []byte, error)
    78  
    79  func (cli *DockerCli) inspectElements(tmplStr string, references []string, searchByReference inspectSearcher) error {
    80  	elementInspector, err := cli.newInspectorWithTemplate(tmplStr)
    81  	if err != nil {
    82  		return Cli.StatusError{StatusCode: 64, Status: err.Error()}
    83  	}
    84  
    85  	var inspectErr error
    86  	for _, ref := range references {
    87  		element, raw, err := searchByReference(ref)
    88  		if err != nil {
    89  			inspectErr = err
    90  			break
    91  		}
    92  
    93  		if err := elementInspector.Inspect(element, raw); err != nil {
    94  			inspectErr = err
    95  			break
    96  		}
    97  	}
    98  
    99  	if err := elementInspector.Flush(); err != nil {
   100  		cli.inspectErrorStatus(err)
   101  	}
   102  
   103  	if status := cli.inspectErrorStatus(inspectErr); status != 0 {
   104  		return Cli.StatusError{StatusCode: status}
   105  	}
   106  	return nil
   107  }
   108  
   109  func (cli *DockerCli) inspectErrorStatus(err error) (status int) {
   110  	if err != nil {
   111  		fmt.Fprintf(cli.err, "%s\n", err)
   112  		status = 1
   113  	}
   114  	return
   115  }
   116  
   117  func (cli *DockerCli) newInspectorWithTemplate(tmplStr string) (inspect.Inspector, error) {
   118  	elementInspector := inspect.NewIndentedInspector(cli.out)
   119  	if tmplStr != "" {
   120  		tmpl, err := templates.Parse(tmplStr)
   121  		if err != nil {
   122  			return nil, fmt.Errorf("Template parsing error: %s", err)
   123  		}
   124  		elementInspector = inspect.NewTemplateInspector(cli.out, tmpl)
   125  	}
   126  	return elementInspector, nil
   127  }