github.com/brahmaroutu/docker@v1.2.1-0.20160809185609-eb28dde01f16/api/client/system/inspect.go (about)

     1  package system
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"golang.org/x/net/context"
     8  
     9  	"github.com/docker/docker/api/client"
    10  	"github.com/docker/docker/api/client/inspect"
    11  	"github.com/docker/docker/cli"
    12  	apiclient "github.com/docker/engine-api/client"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  type inspectOptions struct {
    17  	format      string
    18  	inspectType string
    19  	size        bool
    20  	ids         []string
    21  }
    22  
    23  // NewInspectCommand creates a new cobra.Command for `docker inspect`
    24  func NewInspectCommand(dockerCli *client.DockerCli) *cobra.Command {
    25  	var opts inspectOptions
    26  
    27  	cmd := &cobra.Command{
    28  		Use:   "inspect [OPTIONS] CONTAINER|IMAGE|TASK [CONTAINER|IMAGE|TASK...]",
    29  		Short: "Return low-level information on a container, image or task",
    30  		Args:  cli.RequiresMinArgs(1),
    31  		RunE: func(cmd *cobra.Command, args []string) error {
    32  			opts.ids = args
    33  			return runInspect(dockerCli, opts)
    34  		},
    35  	}
    36  
    37  	flags := cmd.Flags()
    38  	flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given go template")
    39  	flags.StringVar(&opts.inspectType, "type", "", "Return JSON for specified type, (e.g image, container or task)")
    40  	flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes if the type is container")
    41  
    42  	return cmd
    43  }
    44  
    45  func runInspect(dockerCli *client.DockerCli, opts inspectOptions) error {
    46  	ctx := context.Background()
    47  	client := dockerCli.Client()
    48  
    49  	var getRefFunc inspect.GetRefFunc
    50  	switch opts.inspectType {
    51  	case "container":
    52  		getRefFunc = func(ref string) (interface{}, []byte, error) {
    53  			return client.ContainerInspectWithRaw(ctx, ref, opts.size)
    54  		}
    55  	case "image":
    56  		getRefFunc = func(ref string) (interface{}, []byte, error) {
    57  			return client.ImageInspectWithRaw(ctx, ref, opts.size)
    58  		}
    59  	case "task":
    60  		if opts.size {
    61  			fmt.Fprintln(dockerCli.Err(), "WARNING: --size ignored for tasks")
    62  		}
    63  		getRefFunc = func(ref string) (interface{}, []byte, error) {
    64  			return client.TaskInspectWithRaw(ctx, ref)
    65  		}
    66  	case "":
    67  		getRefFunc = inspectAll(ctx, dockerCli, opts.size)
    68  	default:
    69  		return fmt.Errorf("%q is not a valid value for --type", opts.inspectType)
    70  	}
    71  
    72  	return inspect.Inspect(dockerCli.Out(), opts.ids, opts.format, getRefFunc)
    73  }
    74  
    75  func inspectAll(ctx context.Context, dockerCli *client.DockerCli, getSize bool) inspect.GetRefFunc {
    76  	client := dockerCli.Client()
    77  
    78  	return func(ref string) (interface{}, []byte, error) {
    79  		c, rawContainer, err := client.ContainerInspectWithRaw(ctx, ref, getSize)
    80  		if err == nil || !apiclient.IsErrNotFound(err) {
    81  			return c, rawContainer, err
    82  		}
    83  		// Search for image with that id if a container doesn't exist.
    84  		i, rawImage, err := client.ImageInspectWithRaw(ctx, ref, getSize)
    85  		if err == nil || !apiclient.IsErrNotFound(err) {
    86  			return i, rawImage, err
    87  		}
    88  
    89  		// Search for task with that id if an image doesn't exist.
    90  		t, rawTask, err := client.TaskInspectWithRaw(ctx, ref)
    91  		if err == nil || !(apiclient.IsErrNotFound(err) || isErrorNoSwarmMode(err)) {
    92  			if getSize {
    93  				fmt.Fprintln(dockerCli.Err(), "WARNING: --size ignored for tasks")
    94  			}
    95  			return t, rawTask, err
    96  		}
    97  		return nil, nil, fmt.Errorf("Error: No such container, image or task: %s", ref)
    98  	}
    99  }
   100  
   101  func isErrorNoSwarmMode(err error) bool {
   102  	return strings.Contains(err.Error(), "This node is not a swarm manager")
   103  }