mirror of
https://github.com/jesseduffield/lazydocker.git
synced 2026-07-25 08:31:03 +00:00
move model specific code to model file
This commit is contained in:
parent
f06279a5e3
commit
52ec56289a
3 changed files with 34 additions and 23 deletions
|
|
@ -1,9 +1,14 @@
|
||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/docker/docker/client"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/jesseduffield/lazydocker/pkg/utils"
|
"github.com/jesseduffield/lazydocker/pkg/utils"
|
||||||
|
"golang.org/x/xerrors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Container : A git Container
|
// Container : A git Container
|
||||||
|
|
@ -13,6 +18,7 @@ type Container struct {
|
||||||
ID string
|
ID string
|
||||||
Container types.Container
|
Container types.Container
|
||||||
DisplayString string
|
DisplayString string
|
||||||
|
Client *client.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDisplayStrings returns the dispaly string of Container
|
// GetDisplayStrings returns the dispaly string of Container
|
||||||
|
|
@ -39,3 +45,22 @@ func (c *Container) GetColor() color.Attribute {
|
||||||
return color.FgWhite
|
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/config"
|
||||||
"github.com/jesseduffield/lazydocker/pkg/i18n"
|
"github.com/jesseduffield/lazydocker/pkg/i18n"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"golang.org/x/xerrors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// DockerCommand is our main git interface
|
// DockerCommand is our main git interface
|
||||||
|
|
@ -56,27 +55,14 @@ func (c *DockerCommand) GetContainers() ([]*Container, error) {
|
||||||
serviceName = ""
|
serviceName = ""
|
||||||
c.Log.Warn("Could not get service name from docker container")
|
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
|
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
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -329,13 +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 := container.Remove(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 {
|
||||||
configOptions.Force = true
|
configOptions.Force = true
|
||||||
if err := gui.DockerCommand.RemoveContainer(container.ID, configOptions); err != nil {
|
if err := container.Remove(configOptions); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return gui.refreshContainers()
|
return gui.refreshContainers()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue