mirror of https://github.com/docker/docs.git
container: Add Status() and StateString() formatters.
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
parent
c9199cef5a
commit
4cf0a32e4d
|
@ -1,6 +1,12 @@
|
|||
package cluster
|
||||
|
||||
import "github.com/samalba/dockerclient"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/pkg/units"
|
||||
"github.com/samalba/dockerclient"
|
||||
)
|
||||
|
||||
// Container is exported
|
||||
type Container struct {
|
||||
|
@ -10,3 +16,50 @@ type Container struct {
|
|||
Info dockerclient.ContainerInfo
|
||||
Engine *Engine
|
||||
}
|
||||
|
||||
// Status returns a human-readable description of the state
|
||||
// Stoken from docker/docker/daemon/state.go
|
||||
func (c *Container) Status() string {
|
||||
s := c.Info.State
|
||||
if s.Running {
|
||||
if s.Paused {
|
||||
return fmt.Sprintf("Up %s (Paused)", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
||||
}
|
||||
if s.Restarting {
|
||||
return fmt.Sprintf("Restarting (%d) %s ago", s.ExitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
||||
}
|
||||
|
||||
return fmt.Sprintf("Up %s", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
||||
}
|
||||
|
||||
if s.Dead {
|
||||
return "Dead"
|
||||
}
|
||||
|
||||
if s.FinishedAt.IsZero() {
|
||||
return ""
|
||||
}
|
||||
|
||||
return fmt.Sprintf("Exited (%d) %s ago", s.ExitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
||||
}
|
||||
|
||||
// StateString returns a single string to describe state
|
||||
// Stoken from docker/docker/daemon/state.go
|
||||
func (c *Container) StateString() string {
|
||||
s := c.Info.State
|
||||
if s.Running {
|
||||
if s.Paused {
|
||||
return "paused"
|
||||
}
|
||||
if s.Restarting {
|
||||
return "restarting"
|
||||
}
|
||||
return "running"
|
||||
}
|
||||
|
||||
if s.Dead {
|
||||
return "dead"
|
||||
}
|
||||
|
||||
return "exited"
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue