inspect.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package secret
  2. import (
  3. "github.com/docker/docker/cli"
  4. "github.com/docker/docker/cli/command"
  5. "github.com/docker/docker/cli/command/inspect"
  6. "github.com/spf13/cobra"
  7. "golang.org/x/net/context"
  8. )
  9. type inspectOptions struct {
  10. name string
  11. format string
  12. }
  13. func newSecretInspectCommand(dockerCli *command.DockerCli) *cobra.Command {
  14. opts := inspectOptions{}
  15. cmd := &cobra.Command{
  16. Use: "inspect [name]",
  17. Short: "Inspect a secret",
  18. Args: cli.ExactArgs(1),
  19. RunE: func(cmd *cobra.Command, args []string) error {
  20. opts.name = args[0]
  21. return runSecretInspect(dockerCli, opts)
  22. },
  23. }
  24. cmd.Flags().StringVarP(&opts.format, "format", "f", "", "Format the output using the given go template")
  25. return cmd
  26. }
  27. func runSecretInspect(dockerCli *command.DockerCli, opts inspectOptions) error {
  28. client := dockerCli.Client()
  29. ctx := context.Background()
  30. // attempt to lookup secret by name
  31. secrets, err := getSecretsByName(ctx, client, []string{opts.name})
  32. if err != nil {
  33. return err
  34. }
  35. id := opts.name
  36. for _, s := range secrets {
  37. if s.Spec.Annotations.Name == opts.name {
  38. id = s.ID
  39. break
  40. }
  41. }
  42. getRef := func(name string) (interface{}, []byte, error) {
  43. return client.SecretInspectWithRaw(ctx, id)
  44. }
  45. return inspect.Inspect(dockerCli.Out(), []string{id}, opts.format, getRef)
  46. }