From f06279a5e3044a533a2a75132458e5d25f255278 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 18 May 2019 22:08:12 +1000 Subject: [PATCH] use provided config struct for removing containers --- pkg/commands/docker.go | 38 +++---------------------------------- pkg/gui/containers_panel.go | 17 ++++++++++------- 2 files changed, 13 insertions(+), 42 deletions(-) diff --git a/pkg/commands/docker.go b/pkg/commands/docker.go index e48e6c1f..50398785 100644 --- a/pkg/commands/docker.go +++ b/pkg/commands/docker.go @@ -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 } diff --git a/pkg/gui/containers_panel.go b/pkg/gui/containers_panel.go index 047379e9..01982966 100644 --- a/pkg/gui/containers_panel.go +++ b/pkg/gui/containers_panel.go @@ -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()