add -f to plugin inspect

Signed-off-by: Victor Vieux <vieux@docker.com>

Signed-off-by: Victor Vieux <vieux@docker.com>
This commit is contained in:
Victor Vieux 2016-08-17 04:52:27 -07:00
parent ed0dff8f16
commit 63a19edcb1
10 changed files with 70 additions and 50 deletions

View file

@ -3,50 +3,57 @@
package plugin
import (
"encoding/json"
"fmt"
"github.com/docker/docker/api/client"
"github.com/docker/docker/api/client/inspect"
"github.com/docker/docker/cli"
"github.com/docker/docker/reference"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)
type inspectOptions struct {
pluginNames []string
format string
}
func newInspectCommand(dockerCli *client.DockerCli) *cobra.Command {
var opts inspectOptions
cmd := &cobra.Command{
Use: "inspect PLUGIN",
Short: "Inspect a plugin",
Args: cli.ExactArgs(1),
Args: cli.RequiresMinArgs(1),
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
}
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())
}
return client.PluginInspectWithRaw(ctx, ref.String())
}
b, err := json.MarshalIndent(p, "", "\t")
if err != nil {
return err
}
_, err = dockerCli.Out().Write(b)
return err
return inspect.Inspect(dockerCli.Out(), opts.pluginNames, opts.format, getRef)
}

View file

@ -17,7 +17,8 @@ Usage: docker plugin inspect PLUGIN
Inspect a plugin
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
@ -138,6 +139,13 @@ $ docker plugin inspect tiborvass/no-remove:latest
(output formatted for readability)
```bash
$ docker plugin inspect -f '{{.Id}}' tiborvass/no-remove:latest
```
```
8c74c978c434745c3ade82f1bc0acf38d04990eaf494fa507c16d9f1daa99c21
```
## Related information

View file

@ -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-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/imdario/mergo 0.2.1

View file

@ -4,9 +4,7 @@ import (
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
@ -28,17 +26,7 @@ func (s *DockerSuite) TestPluginBasicOps(c *check.C) {
c.Assert(out, checker.Contains, pTag)
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)
out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
@ -51,7 +39,7 @@ func (s *DockerSuite) TestPluginBasicOps(c *check.C) {
c.Assert(err, checker.IsNil)
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) {
c.Fatal(err)
}

View file

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

View file

@ -8,6 +8,11 @@ import (
// 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?")
// 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 {
error
NotFound() bool // Is the error a NotFound error

View file

@ -30,7 +30,7 @@ type PluginAPIClient interface {
PluginInstall(ctx context.Context, name string, options types.PluginInstallOptions) error
PluginPush(ctx context.Context, name string, registryAuth 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.

View file

@ -3,20 +3,28 @@
package client
import (
"bytes"
"encoding/json"
"io/ioutil"
"github.com/docker/engine-api/types"
"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)
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
}

View file

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

View file

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