Add container inspect view

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2020-11-09 20:43:22 +01:00 committed by Guillaume Tardif
parent aa09ecb1f8
commit 49759af15c
3 changed files with 76 additions and 29 deletions

View File

@ -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,
}
}

View File

@ -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"
}

View File

@ -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"
}