inspect.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/pkg/errors"
  18. "github.com/spf13/cobra"
  19. "github.com/docker/api/client"
  20. "github.com/docker/api/formatter"
  21. )
  22. // InspectCommand inspects into containers
  23. func InspectCommand() *cobra.Command {
  24. cmd := &cobra.Command{
  25. Use: "inspect",
  26. Short: "Inspect containers",
  27. Args: cobra.ExactArgs(1),
  28. RunE: func(cmd *cobra.Command, args []string) error {
  29. return runInspect(cmd.Context(), args[0])
  30. },
  31. }
  32. return cmd
  33. }
  34. func runInspect(ctx context.Context, id string) error {
  35. c, err := client.New(ctx)
  36. if err != nil {
  37. return errors.Wrap(err, "cannot connect to backend")
  38. }
  39. container, err := c.ContainerService().Inspect(ctx, id)
  40. if err != nil {
  41. return err
  42. }
  43. j, err := formatter.ToStandardJSON(container)
  44. if err != nil {
  45. return err
  46. }
  47. fmt.Println(j)
  48. return nil
  49. }