mirror of
https://github.com/jesseduffield/lazydocker.git
synced 2026-07-25 08:31:03 +00:00
use provided config struct for removing containers
This commit is contained in:
parent
d0fffd8bca
commit
f06279a5e3
2 changed files with 13 additions and 42 deletions
|
|
@ -62,45 +62,12 @@ func (c *DockerCommand) GetContainers() ([]*Container, error) {
|
||||||
return ownContainers, nil
|
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
|
// MustStopContainer tells us that we must stop the container before removing it
|
||||||
const MustStopContainer = iota
|
const MustStopContainer = iota
|
||||||
|
|
||||||
// RemoveContainer removes a container
|
// RemoveContainer removes a container
|
||||||
func (c *DockerCommand) RemoveContainer(containerID string, options ...RemoveContainerOption) error {
|
func (c *DockerCommand) RemoveContainer(containerID string, options types.ContainerRemoveOptions) error {
|
||||||
config := &removeContainerConfig{}
|
if err := c.Client.ContainerRemove(context.Background(), containerID, options); err != nil {
|
||||||
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 {
|
|
||||||
if strings.Contains(err.Error(), "Stop the container before attempting removal or force remove") {
|
if strings.Contains(err.Error(), "Stop the container before attempting removal or force remove") {
|
||||||
return ComplexError{
|
return ComplexError{
|
||||||
Code: MustStopContainer,
|
Code: MustStopContainer,
|
||||||
|
|
@ -110,5 +77,6 @@ func (c *DockerCommand) RemoveContainer(containerID string, options ...RemoveCon
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/go-errors/errors"
|
"github.com/go-errors/errors"
|
||||||
"github.com/jesseduffield/gocui"
|
"github.com/jesseduffield/gocui"
|
||||||
|
|
@ -289,7 +290,7 @@ func (gui *Gui) handleContainersNextContext(g *gocui.Gui, v *gocui.View) error {
|
||||||
type removeOption struct {
|
type removeOption struct {
|
||||||
description string
|
description string
|
||||||
command string
|
command string
|
||||||
configOptions []commands.RemoveContainerOption
|
configOptions types.ContainerRemoveOptions
|
||||||
runCommand bool
|
runCommand bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -306,14 +307,15 @@ func (gui *Gui) handleContainersRemoveMenu(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
|
||||||
options := []*removeOption{
|
options := []*removeOption{
|
||||||
{
|
{
|
||||||
description: gui.Tr.SLocalize("remove"),
|
description: gui.Tr.SLocalize("remove"),
|
||||||
command: "docker rm " + container.ID[1:10],
|
command: "docker rm " + container.ID[1:10],
|
||||||
runCommand: true,
|
configOptions: types.ContainerRemoveOptions{},
|
||||||
|
runCommand: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: gui.Tr.SLocalize("removeWithVolumes"),
|
description: gui.Tr.SLocalize("removeWithVolumes"),
|
||||||
command: "docker rm --volumes " + container.ID[1:10],
|
command: "docker rm --volumes " + container.ID[1:10],
|
||||||
configOptions: []commands.RemoveContainerOption{commands.RemoveVolumes},
|
configOptions: types.ContainerRemoveOptions{RemoveVolumes: true},
|
||||||
runCommand: true,
|
runCommand: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -327,12 +329,13 @@ func (gui *Gui) handleContainersRemoveMenu(g *gocui.Gui, v *gocui.View) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
configOptions := options[index].configOptions
|
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
|
var originalErr commands.ComplexError
|
||||||
if xerrors.As(cerr, &originalErr) {
|
if xerrors.As(cerr, &originalErr) {
|
||||||
if originalErr.Code == commands.MustStopContainer {
|
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 {
|
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 err
|
||||||
}
|
}
|
||||||
return gui.refreshContainers()
|
return gui.refreshContainers()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue