Skip to content

Commit

Permalink
Add health check to container list
Browse files Browse the repository at this point in the history
  • Loading branch information
stirante committed Jan 27, 2020
1 parent 69f32d5 commit 305b963
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
35 changes: 24 additions & 11 deletions pkg/commands/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,18 @@ type Details struct {
Path string `json:"Path"`
Args []string `json:"Args"`
State struct {
Status string `json:"Status"`
Running bool `json:"Running"`
Paused bool `json:"Paused"`
Restarting bool `json:"Restarting"`
OOMKilled bool `json:"OOMKilled"`
Dead bool `json:"Dead"`
Pid int `json:"Pid"`
ExitCode int `json:"ExitCode"`
Error string `json:"Error"`
StartedAt time.Time `json:"StartedAt"`
FinishedAt time.Time `json:"FinishedAt"`
Status string `json:"Status"`
Running bool `json:"Running"`
Paused bool `json:"Paused"`
Restarting bool `json:"Restarting"`
OOMKilled bool `json:"OOMKilled"`
Dead bool `json:"Dead"`
Pid int `json:"Pid"`
ExitCode int `json:"ExitCode"`
Error string `json:"Error"`
StartedAt time.Time `json:"StartedAt"`
FinishedAt time.Time `json:"FinishedAt"`
Health types.Health `json:"Health"`
} `json:"State"`
Image string `json:"Image"`
ResolvConfPath string `json:"ResolvConfPath"`
Expand Down Expand Up @@ -259,6 +260,12 @@ func (c *Container) GetDisplayStatus() string {
if c.Container.State == "exited" {
state += " (" + strconv.Itoa(c.Details.State.ExitCode) + ")"
}
if c.Container.State == "running" && c.Details.State.Health.Status != "" {
state += " (" + c.Details.State.Health.Status + ")";
}
if c.Container.State == "running" && c.Details.State.Health.Status == "unhealthy" {
return utils.MultiColoredString(state, c.GetColor(), color.BgRed)
}

return utils.ColoredString(state, c.GetColor())
}
Expand Down Expand Up @@ -305,6 +312,12 @@ func (c *Container) GetColor() color.Attribute {
case "created":
return color.FgCyan
case "running":
if c.Details.State.Health.Status == "starting" {
return color.FgYellow
}
if c.Details.State.Health.Status == "unhealthy" {
return color.FgBlack
}
return color.FgGreen
case "paused":
return color.FgYellow
Expand Down
7 changes: 7 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ func ColoredString(str string, colorAttribute color.Attribute) string {
return ColoredStringDirect(str, colour)
}

// MultiColoredString takes a string and an array of colour attributes and returns a colored
// string with those attributes
func MultiColoredString(str string, colorAttribute ...color.Attribute) string {
colour := color.New(colorAttribute...)
return ColoredStringDirect(str, colour)
}

// ColoredStringDirect used for aggregating a few color attributes rather than
// just sending a single one
func ColoredStringDirect(str string, colour *color.Color) string {
Expand Down

0 comments on commit 305b963

Please sign in to comment.