inspect.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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/docker/compose-cli/cmd/formatter"
  18. "github.com/Azure/go-autorest/autorest/to"
  19. "github.com/compose-spec/compose-go/types"
  20. "github.com/pkg/errors"
  21. "github.com/spf13/cobra"
  22. "github.com/docker/compose-cli/api/client"
  23. "github.com/docker/compose-cli/api/containers"
  24. )
  25. // InspectCommand inspects into containers
  26. func InspectCommand() *cobra.Command {
  27. cmd := &cobra.Command{
  28. Use: "inspect",
  29. Short: "Inspect containers",
  30. Args: cobra.ExactArgs(1),
  31. RunE: func(cmd *cobra.Command, args []string) error {
  32. return runInspect(cmd.Context(), args[0])
  33. },
  34. }
  35. return cmd
  36. }
  37. func runInspect(ctx context.Context, id string) error {
  38. c, err := client.New(ctx)
  39. if err != nil {
  40. return errors.Wrap(err, "cannot connect to backend")
  41. }
  42. container, err := c.ContainerService().Inspect(ctx, id)
  43. if err != nil {
  44. return err
  45. }
  46. view := getInspectView(container)
  47. j, err := formatter.ToStandardJSON(view)
  48. if err != nil {
  49. return err
  50. }
  51. fmt.Print(j)
  52. return nil
  53. }
  54. // ContainerInspectView inspect view
  55. type ContainerInspectView struct {
  56. ID string
  57. Status string
  58. Image string
  59. Command string `json:",omitempty"`
  60. HostConfig *containers.HostConfig `json:",omitempty"`
  61. Ports []containers.Port `json:",omitempty"`
  62. Config *containers.RuntimeConfig `json:",omitempty"`
  63. Platform string
  64. Healthcheck *containerInspectHealthcheck `json:",omitempty"`
  65. }
  66. type containerInspectHealthcheck struct {
  67. // Test is the command to be run to check the health of the container
  68. Test []string `json:",omitempty"`
  69. // Interval is the period in between the checks
  70. Interval *types.Duration `json:",omitempty"`
  71. // Retries is the number of attempts before declaring the container as healthy or unhealthy
  72. Retries *int `json:",omitempty"`
  73. // StartPeriod is the start delay before starting the checks
  74. StartPeriod *types.Duration `json:",omitempty"`
  75. // Timeout is the timeout in between checks
  76. Timeout *types.Duration `json:",omitempty"`
  77. }
  78. func getInspectView(container containers.Container) ContainerInspectView {
  79. var (
  80. healthcheck *containerInspectHealthcheck
  81. test []string
  82. retries *int
  83. ports []containers.Port
  84. )
  85. if len(container.Ports) > 0 {
  86. ports = container.Ports
  87. }
  88. if !container.Healthcheck.Disable && len(container.Healthcheck.Test) > 0 {
  89. test = container.Healthcheck.Test
  90. if container.Healthcheck.Retries != 0 {
  91. retries = to.IntPtr(container.Healthcheck.Retries)
  92. }
  93. getDurationPtr := func(d types.Duration) *types.Duration {
  94. if d == types.Duration(0) {
  95. return nil
  96. }
  97. return &d
  98. }
  99. healthcheck = &containerInspectHealthcheck{
  100. Test: test,
  101. Retries: retries,
  102. Interval: getDurationPtr(container.Healthcheck.Interval),
  103. StartPeriod: getDurationPtr(container.Healthcheck.StartPeriod),
  104. Timeout: getDurationPtr(container.Healthcheck.Timeout),
  105. }
  106. }
  107. return ContainerInspectView{
  108. ID: container.ID,
  109. Status: container.Status,
  110. Image: container.Image,
  111. Command: container.Command,
  112. Config: container.Config,
  113. HostConfig: container.HostConfig,
  114. Ports: ports,
  115. Platform: container.Platform,
  116. Healthcheck: healthcheck,
  117. }
  118. }