inspect.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // +build experimental
  2. package plugin
  3. import (
  4. "fmt"
  5. "github.com/docker/docker/api/client"
  6. "github.com/docker/docker/api/client/inspect"
  7. "github.com/docker/docker/cli"
  8. "github.com/docker/docker/reference"
  9. "github.com/spf13/cobra"
  10. "golang.org/x/net/context"
  11. )
  12. type inspectOptions struct {
  13. pluginNames []string
  14. format string
  15. }
  16. func newInspectCommand(dockerCli *client.DockerCli) *cobra.Command {
  17. var opts inspectOptions
  18. cmd := &cobra.Command{
  19. Use: "inspect PLUGIN",
  20. Short: "Inspect a plugin",
  21. Args: cli.RequiresMinArgs(1),
  22. RunE: func(cmd *cobra.Command, args []string) error {
  23. opts.pluginNames = args
  24. return runInspect(dockerCli, opts)
  25. },
  26. }
  27. flags := cmd.Flags()
  28. flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given go template")
  29. return cmd
  30. }
  31. func runInspect(dockerCli *client.DockerCli, opts inspectOptions) error {
  32. client := dockerCli.Client()
  33. ctx := context.Background()
  34. getRef := func(name string) (interface{}, []byte, error) {
  35. named, err := reference.ParseNamed(name) // FIXME: validate
  36. if err != nil {
  37. return nil, nil, err
  38. }
  39. if reference.IsNameOnly(named) {
  40. named = reference.WithDefaultTag(named)
  41. }
  42. ref, ok := named.(reference.NamedTagged)
  43. if !ok {
  44. return nil, nil, fmt.Errorf("invalid name: %s", named.String())
  45. }
  46. return client.PluginInspectWithRaw(ctx, ref.String())
  47. }
  48. return inspect.Inspect(dockerCli.Out(), opts.pluginNames, opts.format, getRef)
  49. }