diff --git a/pkg/commands/container.go b/pkg/commands/container.go index fb402af8..480d65ec 100644 --- a/pkg/commands/container.go +++ b/pkg/commands/container.go @@ -1,9 +1,14 @@ package commands import ( + "context" + "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 @@ -13,6 +18,7 @@ type Container struct { ID string Container types.Container DisplayString string + Client *client.Client } // GetDisplayStrings returns the dispaly string of Container @@ -39,3 +45,22 @@ func (c *Container) GetColor() color.Attribute { return color.FgWhite } } + +// MustStopContainer tells us that we must stop the container before removing it +const MustStopContainer = iota + +// Remove removes a 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 +} diff --git a/pkg/commands/docker.go b/pkg/commands/docker.go index 50398785..604eaa5a 100644 --- a/pkg/commands/docker.go +++ b/pkg/commands/docker.go @@ -11,7 +11,6 @@ import ( "github.com/jesseduffield/lazydocker/pkg/config" "github.com/jesseduffield/lazydocker/pkg/i18n" "github.com/sirupsen/logrus" - "golang.org/x/xerrors" ) // DockerCommand is our main git interface @@ -56,27 +55,14 @@ func (c *DockerCommand) GetContainers() ([]*Container, error) { serviceName = "" c.Log.Warn("Could not get service name from docker container") } - ownContainers[i] = &Container{ID: container.ID, Name: strings.TrimLeft(container.Names[0], "/"), ServiceName: serviceName, Container: container} + ownContainers[i] = &Container{ + ID: container.ID, + Name: strings.TrimLeft(container.Names[0], "/"), + ServiceName: serviceName, + Container: container, + Client: c.Client, + } } return ownContainers, nil } - -// MustStopContainer tells us that we must stop the container before removing it -const MustStopContainer = iota - -// RemoveContainer removes a container -func (c *DockerCommand) RemoveContainer(containerID string, options types.ContainerRemoveOptions) error { - if err := c.Client.ContainerRemove(context.Background(), containerID, 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 -} diff --git a/pkg/gui/containers_panel.go b/pkg/gui/containers_panel.go index 01982966..2a66a4cc 100644 --- a/pkg/gui/containers_panel.go +++ b/pkg/gui/containers_panel.go @@ -329,13 +329,13 @@ func (gui *Gui) handleContainersRemoveMenu(g *gocui.Gui, v *gocui.View) error { return nil } configOptions := options[index].configOptions - if cerr := gui.DockerCommand.RemoveContainer(container.ID, configOptions); cerr != nil { + if cerr := container.Remove(configOptions); cerr != nil { var originalErr commands.ComplexError if xerrors.As(cerr, &originalErr) { if originalErr.Code == commands.MustStopContainer { return gui.createConfirmationPanel(gui.g, v, gui.Tr.SLocalize("Confirm"), gui.Tr.SLocalize("mustForceToRemove"), func(g *gocui.Gui, v *gocui.View) error { configOptions.Force = true - if err := gui.DockerCommand.RemoveContainer(container.ID, configOptions); err != nil { + if err := container.Remove(configOptions); err != nil { return err } return gui.refreshContainers()