Browse Source

Merge pull request #25990 from vieux/plugin_inspect

add -f to plugin inspect
Vincent Demeester 8 years ago
parent
commit
bf0077c138

+ 31 - 24
api/client/plugin/inspect.go

@@ -3,50 +3,57 @@
 package plugin
 package plugin
 
 
 import (
 import (
-	"encoding/json"
 	"fmt"
 	"fmt"
 
 
 	"github.com/docker/docker/api/client"
 	"github.com/docker/docker/api/client"
+	"github.com/docker/docker/api/client/inspect"
 	"github.com/docker/docker/cli"
 	"github.com/docker/docker/cli"
 	"github.com/docker/docker/reference"
 	"github.com/docker/docker/reference"
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 	"golang.org/x/net/context"
 	"golang.org/x/net/context"
 )
 )
 
 
+type inspectOptions struct {
+	pluginNames []string
+	format      string
+}
+
 func newInspectCommand(dockerCli *client.DockerCli) *cobra.Command {
 func newInspectCommand(dockerCli *client.DockerCli) *cobra.Command {
+	var opts inspectOptions
+
 	cmd := &cobra.Command{
 	cmd := &cobra.Command{
 		Use:   "inspect PLUGIN",
 		Use:   "inspect PLUGIN",
 		Short: "Inspect a plugin",
 		Short: "Inspect a plugin",
-		Args:  cli.ExactArgs(1),
+		Args:  cli.RequiresMinArgs(1),
 		RunE: func(cmd *cobra.Command, args []string) error {
 		RunE: func(cmd *cobra.Command, args []string) error {
-			return runInspect(dockerCli, args[0])
+			opts.pluginNames = args
+			return runInspect(dockerCli, opts)
 		},
 		},
 	}
 	}
 
 
+	flags := cmd.Flags()
+	flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given go template")
 	return cmd
 	return cmd
 }
 }
 
 
-func runInspect(dockerCli *client.DockerCli, name string) error {
-	named, err := reference.ParseNamed(name) // FIXME: validate
-	if err != nil {
-		return err
-	}
-	if reference.IsNameOnly(named) {
-		named = reference.WithDefaultTag(named)
-	}
-	ref, ok := named.(reference.NamedTagged)
-	if !ok {
-		return fmt.Errorf("invalid name: %s", named.String())
-	}
-	p, err := dockerCli.Client().PluginInspect(context.Background(), ref.String())
-	if err != nil {
-		return err
-	}
+func runInspect(dockerCli *client.DockerCli, opts inspectOptions) error {
+	client := dockerCli.Client()
+	ctx := context.Background()
+	getRef := func(name string) (interface{}, []byte, error) {
+		named, err := reference.ParseNamed(name) // FIXME: validate
+		if err != nil {
+			return nil, nil, err
+		}
+		if reference.IsNameOnly(named) {
+			named = reference.WithDefaultTag(named)
+		}
+		ref, ok := named.(reference.NamedTagged)
+		if !ok {
+			return nil, nil, fmt.Errorf("invalid name: %s", named.String())
+		}
 
 
-	b, err := json.MarshalIndent(p, "", "\t")
-	if err != nil {
-		return err
+		return client.PluginInspectWithRaw(ctx, ref.String())
 	}
 	}
-	_, err = dockerCli.Out().Write(b)
-	return err
+
+	return inspect.Inspect(dockerCli.Out(), opts.pluginNames, opts.format, getRef)
 }
 }

+ 9 - 1
docs/reference/commandline/plugin_inspect.md

@@ -17,7 +17,8 @@ Usage:  docker plugin inspect PLUGIN
 Inspect a plugin
 Inspect a plugin
 
 
 Options:
 Options:
-      --help   Print usage
+      -f, --format string   Format the output using the given go template
+          --help            Print usage
 ```
 ```
 
 
 Returns information about a plugin. By default, this command renders all results
 Returns information about a plugin. By default, this command renders all results
@@ -138,6 +139,13 @@ $ docker plugin inspect tiborvass/no-remove:latest
 (output formatted for readability)
 (output formatted for readability)
 
 
 
 
+```bash
+$ docker plugin inspect -f '{{.Id}}' tiborvass/no-remove:latest
+```
+```
+8c74c978c434745c3ade82f1bc0acf38d04990eaf494fa507c16d9f1daa99c21
+```
+
 
 
 ## Related information
 ## Related information
 
 

+ 1 - 1
hack/vendor.sh

@@ -61,7 +61,7 @@ clone git golang.org/x/sys eb2c74142fd19a79b3f237334c7384d5167b1b46 https://gith
 clone git github.com/docker/go-units eb879ae3e2b84e2a142af415b679ddeda47ec71c
 clone git github.com/docker/go-units eb879ae3e2b84e2a142af415b679ddeda47ec71c
 clone git github.com/docker/go-connections fa2850ff103453a9ad190da0df0af134f0314b3d
 clone git github.com/docker/go-connections fa2850ff103453a9ad190da0df0af134f0314b3d
 
 
-clone git github.com/docker/engine-api 94a8f8f29307ab291abad6c6f2182d67089aae5d
+clone git github.com/docker/engine-api 8d8fffdf863b12d03c76abf6ca1377e6f8f4e549
 clone git github.com/RackSec/srslog 259aed10dfa74ea2961eddd1d9847619f6e98837
 clone git github.com/RackSec/srslog 259aed10dfa74ea2961eddd1d9847619f6e98837
 clone git github.com/imdario/mergo 0.2.1
 clone git github.com/imdario/mergo 0.2.1
 
 

+ 2 - 14
integration-cli/docker_cli_plugins_test.go

@@ -4,9 +4,7 @@ import (
 	"github.com/docker/docker/pkg/integration/checker"
 	"github.com/docker/docker/pkg/integration/checker"
 	"github.com/go-check/check"
 	"github.com/go-check/check"
 
 
-	"io/ioutil"
 	"os"
 	"os"
-	"os/exec"
 	"path/filepath"
 	"path/filepath"
 	"strings"
 	"strings"
 )
 )
@@ -28,17 +26,7 @@ func (s *DockerSuite) TestPluginBasicOps(c *check.C) {
 	c.Assert(out, checker.Contains, pTag)
 	c.Assert(out, checker.Contains, pTag)
 	c.Assert(out, checker.Contains, "true")
 	c.Assert(out, checker.Contains, "true")
 
 
-	out, _, err = dockerCmdWithError("plugin", "inspect", pNameWithTag)
-	c.Assert(err, checker.IsNil)
-	tmpFile, err := ioutil.TempFile("", "inspect.json")
-	c.Assert(err, checker.IsNil)
-	defer tmpFile.Close()
-
-	if _, err := tmpFile.Write([]byte(out)); err != nil {
-		c.Fatal(err)
-	}
-	// FIXME: When `docker plugin inspect` takes a format as input, jq can be replaced.
-	id, err := exec.Command("jq", ".Id", "--raw-output", tmpFile.Name()).CombinedOutput()
+	id, _, err := dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pNameWithTag)
 	c.Assert(err, checker.IsNil)
 	c.Assert(err, checker.IsNil)
 
 
 	out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
 	out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
@@ -51,7 +39,7 @@ func (s *DockerSuite) TestPluginBasicOps(c *check.C) {
 	c.Assert(err, checker.IsNil)
 	c.Assert(err, checker.IsNil)
 	c.Assert(out, checker.Contains, pNameWithTag)
 	c.Assert(out, checker.Contains, pNameWithTag)
 
 
-	_, err = os.Stat(filepath.Join(dockerBasePath, "plugins", string(id)))
+	_, err = os.Stat(filepath.Join(dockerBasePath, "plugins", id))
 	if !os.IsNotExist(err) {
 	if !os.IsNotExist(err) {
 		c.Fatal(err)
 		c.Fatal(err)
 	}
 	}

+ 3 - 0
vendor/src/github.com/docker/engine-api/client/client.go

@@ -18,6 +18,8 @@ const DefaultVersion string = "1.23"
 // Client is the API client that performs all operations
 // Client is the API client that performs all operations
 // against a docker server.
 // against a docker server.
 type Client struct {
 type Client struct {
+	// host holds the server address to connect to
+	host string
 	// proto holds the client protocol i.e. unix.
 	// proto holds the client protocol i.e. unix.
 	proto string
 	proto string
 	// addr holds the client address.
 	// addr holds the client address.
@@ -90,6 +92,7 @@ func NewClient(host string, version string, client *http.Client, httpHeaders map
 	}
 	}
 
 
 	return &Client{
 	return &Client{
+		host:              host,
 		proto:             proto,
 		proto:             proto,
 		addr:              addr,
 		addr:              addr,
 		basePath:          basePath,
 		basePath:          basePath,

+ 5 - 0
vendor/src/github.com/docker/engine-api/client/errors.go

@@ -8,6 +8,11 @@ import (
 // ErrConnectionFailed is an error raised when the connection between the client and the server failed.
 // ErrConnectionFailed is an error raised when the connection between the client and the server failed.
 var ErrConnectionFailed = errors.New("Cannot connect to the Docker daemon. Is the docker daemon running on this host?")
 var ErrConnectionFailed = errors.New("Cannot connect to the Docker daemon. Is the docker daemon running on this host?")
 
 
+// ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed.
+func ErrorConnectionFailed(host string) error {
+	return fmt.Errorf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", host)
+}
+
 type notFound interface {
 type notFound interface {
 	error
 	error
 	NotFound() bool // Is the error a NotFound error
 	NotFound() bool // Is the error a NotFound error

+ 1 - 1
vendor/src/github.com/docker/engine-api/client/interface_experimental.go

@@ -30,7 +30,7 @@ type PluginAPIClient interface {
 	PluginInstall(ctx context.Context, name string, options types.PluginInstallOptions) error
 	PluginInstall(ctx context.Context, name string, options types.PluginInstallOptions) error
 	PluginPush(ctx context.Context, name string, registryAuth string) error
 	PluginPush(ctx context.Context, name string, registryAuth string) error
 	PluginSet(ctx context.Context, name string, args []string) error
 	PluginSet(ctx context.Context, name string, args []string) error
-	PluginInspect(ctx context.Context, name string) (*types.Plugin, error)
+	PluginInspectWithRaw(ctx context.Context, name string) (*types.Plugin, []byte, error)
 }
 }
 
 
 // Ensure that Client always implements APIClient.
 // Ensure that Client always implements APIClient.

+ 15 - 7
vendor/src/github.com/docker/engine-api/client/plugin_inspect.go

@@ -3,20 +3,28 @@
 package client
 package client
 
 
 import (
 import (
+	"bytes"
 	"encoding/json"
 	"encoding/json"
+	"io/ioutil"
 
 
 	"github.com/docker/engine-api/types"
 	"github.com/docker/engine-api/types"
 	"golang.org/x/net/context"
 	"golang.org/x/net/context"
 )
 )
 
 
-// PluginInspect inspects an existing plugin
-func (cli *Client) PluginInspect(ctx context.Context, name string) (*types.Plugin, error) {
-	var p types.Plugin
+// PluginInspectWithRaw inspects an existing plugin
+func (cli *Client) PluginInspectWithRaw(ctx context.Context, name string) (*types.Plugin, []byte, error) {
 	resp, err := cli.get(ctx, "/plugins/"+name, nil, nil)
 	resp, err := cli.get(ctx, "/plugins/"+name, nil, nil)
 	if err != nil {
 	if err != nil {
-		return nil, err
+		return nil, nil, err
 	}
 	}
-	err = json.NewDecoder(resp.body).Decode(&p)
-	ensureReaderClosed(resp)
-	return &p, err
+
+	defer ensureReaderClosed(resp)
+	body, err := ioutil.ReadAll(resp.body)
+	if err != nil {
+		return nil, nil, err
+	}
+	var p types.Plugin
+	rdr := bytes.NewReader(body)
+	err = json.NewDecoder(rdr).Decode(&p)
+	return &p, body, err
 }
 }

+ 2 - 2
vendor/src/github.com/docker/engine-api/client/request.go

@@ -123,11 +123,11 @@ func (cli *Client) sendClientRequest(ctx context.Context, method, path string, q
 
 
 		if err, ok := err.(net.Error); ok {
 		if err, ok := err.(net.Error); ok {
 			if err.Timeout() {
 			if err.Timeout() {
-				return serverResp, ErrConnectionFailed
+				return serverResp, ErrorConnectionFailed(cli.host)
 			}
 			}
 			if !err.Temporary() {
 			if !err.Temporary() {
 				if strings.Contains(err.Error(), "connection refused") || strings.Contains(err.Error(), "dial unix") {
 				if strings.Contains(err.Error(), "connection refused") || strings.Contains(err.Error(), "dial unix") {
-					return serverResp, ErrConnectionFailed
+					return serverResp, ErrorConnectionFailed(cli.host)
 				}
 				}
 			}
 			}
 		}
 		}

+ 1 - 0
vendor/src/github.com/docker/engine-api/types/swarm/task.go

@@ -38,6 +38,7 @@ const (
 type Task struct {
 type Task struct {
 	ID string
 	ID string
 	Meta
 	Meta
+	Annotations
 
 
 	Spec                TaskSpec            `json:",omitempty"`
 	Spec                TaskSpec            `json:",omitempty"`
 	ServiceID           string              `json:",omitempty"`
 	ServiceID           string              `json:",omitempty"`