inspect.go 865 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package cmd
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/pkg/errors"
  7. "github.com/spf13/cobra"
  8. "github.com/docker/api/client"
  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. b, err := json.MarshalIndent(container, "", " ")
  32. if err != nil {
  33. return err
  34. }
  35. containerString := string(b)
  36. fmt.Println(containerString)
  37. return nil
  38. }