use provided config struct for removing containers

pull/1/head
Jesse Duffield 5 years ago
parent d0fffd8bca
commit f06279a5e3

@ -62,45 +62,12 @@ func (c *DockerCommand) GetContainers() ([]*Container, error) {
return ownContainers, nil
}
type removeContainerConfig struct {
// RemoveVolumes removes any volumes attached to the container
RemoveVolumes bool
// Force forces the container to be removed, even if it's running
Force bool
}
type RemoveContainerOption func(c *removeContainerConfig)
// RemoveVolumes is an option to remove volumes attached to the container
func RemoveVolumes(c *removeContainerConfig) {
c.RemoveVolumes = true
}
// Force is an option to remove volumes attached to the container
func Force(c *removeContainerConfig) {
c.Force = true
}
// 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 ...RemoveContainerOption) error {
config := &removeContainerConfig{}
for _, option := range options {
option(config)
}
flags := ""
if config.RemoveVolumes {
flags += " --volumes "
}
if config.Force {
flags += " --force "
}
err := c.OSCommand.RunCommand(fmt.Sprintf("docker rm %s %s", flags, containerID))
if err != nil {
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,
@ -110,5 +77,6 @@ func (c *DockerCommand) RemoveContainer(containerID string, options ...RemoveCon
}
return err
}
return nil
}

@ -8,6 +8,7 @@ import (
"os/exec"
"time"
"github.com/docker/docker/api/types"
"github.com/fatih/color"
"github.com/go-errors/errors"
"github.com/jesseduffield/gocui"
@ -289,7 +290,7 @@ func (gui *Gui) handleContainersNextContext(g *gocui.Gui, v *gocui.View) error {
type removeOption struct {
description string
command string
configOptions []commands.RemoveContainerOption
configOptions types.ContainerRemoveOptions
runCommand bool
}
@ -306,14 +307,15 @@ func (gui *Gui) handleContainersRemoveMenu(g *gocui.Gui, v *gocui.View) error {
options := []*removeOption{
{
description: gui.Tr.SLocalize("remove"),
command: "docker rm " + container.ID[1:10],
runCommand: true,
description: gui.Tr.SLocalize("remove"),
command: "docker rm " + container.ID[1:10],
configOptions: types.ContainerRemoveOptions{},
runCommand: true,
},
{
description: gui.Tr.SLocalize("removeWithVolumes"),
command: "docker rm --volumes " + container.ID[1:10],
configOptions: []commands.RemoveContainerOption{commands.RemoveVolumes},
configOptions: types.ContainerRemoveOptions{RemoveVolumes: true},
runCommand: true,
},
{
@ -327,12 +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 := gui.DockerCommand.RemoveContainer(container.ID, 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 {
if err := gui.DockerCommand.RemoveContainer(container.ID, append(configOptions, commands.Force)...); err != nil {
configOptions.Force = true
if err := gui.DockerCommand.RemoveContainer(container.ID, configOptions); err != nil {
return err
}
return gui.refreshContainers()

Loading…
Cancel
Save