diff --git a/cli/cmd/inspect.go b/cli/cmd/inspect.go index 2e964799..7d3248ee 100644 --- a/cli/cmd/inspect.go +++ b/cli/cmd/inspect.go @@ -20,10 +20,13 @@ import ( "context" "fmt" + "github.com/Azure/go-autorest/autorest/to" + "github.com/compose-spec/compose-go/types" "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/docker/compose-cli/api/client" + "github.com/docker/compose-cli/api/containers" "github.com/docker/compose-cli/formatter" ) @@ -52,7 +55,9 @@ func runInspect(ctx context.Context, id string) error { return err } - j, err := formatter.ToStandardJSON(container) + view := getInspectView(container) + + j, err := formatter.ToStandardJSON(view) if err != nil { return err } @@ -60,3 +65,71 @@ func runInspect(ctx context.Context, id string) error { return nil } + +type containerInspectView struct { + ID string + Status string + Image string + Command string `json:",omitempty"` + HostConfig *containers.HostConfig `json:",omitempty"` + Ports *[]containers.Port `json:",omitempty"` + Platform string + Healthcheck *containerInspectHealthcheck `json:",omitempty"` +} + +type containerInspectHealthcheck struct { + // Test is the command to be run to check the health of the container + Test *[]string `json:",omitempty"` + // Interval is the period in between the checks + Interval *types.Duration `json:",omitempty"` + // Retries is the number of attempts before declaring the container as healthy or unhealthy + Retries *int `json:",omitempty"` + // StartPeriod is the start delay before starting the checks + StartPeriod *types.Duration `json:",omitempty"` + // Timeout is the timeout in between checks + Timeout *types.Duration `json:",omitempty"` +} + +func getInspectView(container containers.Container) containerInspectView { + var ( + healthcheck *containerInspectHealthcheck + test *[]string + retries *int + ports *[]containers.Port + ) + + if !container.Healthcheck.Disable && len(container.Healthcheck.Test) > 0 { + test = &container.Healthcheck.Test + if container.Healthcheck.Retries != 0 { + retries = to.IntPtr(container.Healthcheck.Retries) + } + if len(container.Ports) > 0 { + ports = &container.Ports + } + getDurationPtr := func(d types.Duration) *types.Duration { + if d == types.Duration(0) { + return nil + } + return &d + } + + healthcheck = &containerInspectHealthcheck{ + Test: test, + Retries: retries, + Interval: getDurationPtr(container.Healthcheck.Interval), + StartPeriod: getDurationPtr(container.Healthcheck.StartPeriod), + Timeout: getDurationPtr(container.Healthcheck.Timeout), + } + } + + return containerInspectView{ + ID: container.ID, + Status: container.Status, + Image: container.Image, + Command: container.Command, + HostConfig: container.HostConfig, + Ports: ports, + Platform: container.Platform, + Healthcheck: healthcheck, + } +} diff --git a/cli/cmd/testdata/inspect-out-id.golden b/cli/cmd/testdata/inspect-out-id.golden index 517629f4..1200c98e 100644 --- a/cli/cmd/testdata/inspect-out-id.golden +++ b/cli/cmd/testdata/inspect-out-id.golden @@ -2,11 +2,6 @@ "ID": "id", "Status": "", "Image": "nginx", - "Command": "", - "CPUTime": 0, - "MemoryUsage": 0, - "PidsCurrent": 0, - "PidsLimit": 0, "HostConfig": { "RestartPolicy": "none", "CPUReservation": 0, @@ -15,13 +10,5 @@ "MemoryLimit": 0, "AutoRemove": false }, - "Platform": "Linux", - "Healthcheck": { - "Disable": false, - "Test": null, - "Interval": "0s", - "Retries": 0, - "StartPeriod": "0s", - "Timeout": "0s" - } + "Platform": "Linux" } diff --git a/tests/e2e/testdata/inspect-id.golden b/tests/e2e/testdata/inspect-id.golden index 517629f4..1200c98e 100644 --- a/tests/e2e/testdata/inspect-id.golden +++ b/tests/e2e/testdata/inspect-id.golden @@ -2,11 +2,6 @@ "ID": "id", "Status": "", "Image": "nginx", - "Command": "", - "CPUTime": 0, - "MemoryUsage": 0, - "PidsCurrent": 0, - "PidsLimit": 0, "HostConfig": { "RestartPolicy": "none", "CPUReservation": 0, @@ -15,13 +10,5 @@ "MemoryLimit": 0, "AutoRemove": false }, - "Platform": "Linux", - "Healthcheck": { - "Disable": false, - "Test": null, - "Interval": "0s", - "Retries": 0, - "StartPeriod": "0s", - "Timeout": "0s" - } + "Platform": "Linux" }