inspect.go 835 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. "github.com/spf13/cobra"
  7. "github.com/docker/api/client"
  8. "github.com/docker/api/formatter"
  9. )
  10. // InspectCommand inspects into containers
  11. func InspectCommand() *cobra.Command {
  12. cmd := &cobra.Command{
  13. Use: "inspect",
  14. Short: "Inspect containers",
  15. Args: cobra.ExactArgs(1),
  16. RunE: func(cmd *cobra.Command, args []string) error {
  17. return runInspect(cmd.Context(), args[0])
  18. },
  19. }
  20. return cmd
  21. }
  22. func runInspect(ctx context.Context, id string) error {
  23. c, err := client.New(ctx)
  24. if err != nil {
  25. return errors.Wrap(err, "cannot connect to backend")
  26. }
  27. container, err := c.ContainerService().Inspect(ctx, id)
  28. if err != nil {
  29. return err
  30. }
  31. j, err := formatter.ToStandardJSON(container)
  32. if err != nil {
  33. return err
  34. }
  35. fmt.Println(j)
  36. return nil
  37. }