lazydocker/pkg/commands/container.go
2019-05-19 00:19:04 +10:00

84 lines
2 KiB
Go

package commands
import (
"context"
"os/exec"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/fatih/color"
"github.com/jesseduffield/lazydocker/pkg/utils"
"golang.org/x/xerrors"
)
// Container : A git Container
type Container struct {
Name string
ServiceName string
ID string
Container types.Container
DisplayString string
Client *client.Client
OSCommand *OSCommand
}
// GetDisplayStrings returns the dispaly string of Container
func (c *Container) GetDisplayStrings(isFocused bool) []string {
return []string{utils.ColoredString(c.Container.State, c.GetColor()), utils.ColoredString(c.Name, color.FgWhite)}
}
// GetColor Container color
func (c *Container) GetColor() color.Attribute {
switch c.Container.State {
case "exited":
return color.FgRed
case "created":
return color.FgCyan
case "running":
return color.FgGreen
case "paused":
return color.FgYellow
case "dead":
return color.FgRed
case "restarting":
return color.FgBlue
default:
return color.FgWhite
}
}
// MustStopContainer tells us that we must stop the container before removing it
const MustStopContainer = iota
// Remove removes the container
func (c *Container) Remove(options types.ContainerRemoveOptions) error {
if err := c.Client.ContainerRemove(context.Background(), c.ID, options); err != nil {
if strings.Contains(err.Error(), "Stop the container before attempting removal or force remove") {
return ComplexError{
Code: MustStopContainer,
Message: err.Error(),
frame: xerrors.Caller(1),
}
}
return err
}
return nil
}
// Stop stops the container
func (c *Container) Stop() error {
return c.Client.ContainerStop(context.Background(), c.ID, nil)
}
// Restart restarts the container
func (c *Container) Restart() error {
return c.Client.ContainerRestart(context.Background(), c.ID, nil)
}
// Attach attaches the container
func (c *Container) Attach() *exec.Cmd {
cmd := c.OSCommand.PrepareSubProcess("docker", "attach", "--sig-proxy=false", c.ID)
return cmd
}