move model specific code to model file

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

@ -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
}

@ -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}
}
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),
}
ownContainers[i] = &Container{
ID: container.ID,
Name: strings.TrimLeft(container.Names[0], "/"),
ServiceName: serviceName,
Container: container,
Client: c.Client,
}
return err
}
return nil
return ownContainers, nil
}

@ -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()

Loading…
Cancel
Save