diff --git a/pkg/commands/container.go b/pkg/commands/container.go index 8ccdf040..d97cbdae 100644 --- a/pkg/commands/container.go +++ b/pkg/commands/container.go @@ -310,9 +310,6 @@ func (c *Container) GetColor() color.Attribute { } } -// MustStopContainer tells us that we must stop the container before removing it -const MustStopContainer = iota - // Remove removes the container func (c *Container) Remove(options types.ContainerRemoveOptions) error { if err := c.Client.ContainerRemove(context.Background(), c.ID, options); err != nil { diff --git a/pkg/commands/errors.go b/pkg/commands/errors.go index 7abc8f42..5b91e9fa 100644 --- a/pkg/commands/errors.go +++ b/pkg/commands/errors.go @@ -7,6 +7,11 @@ import ( "golang.org/x/xerrors" ) +const ( + // MustStopContainer tells us that we must stop the container before removing it + MustStopContainer = iota +) + // WrapError wraps an error for the sake of showing a stack trace at the top level // the go-errors package, for some reason, does not return nil when you try to wrap // a non-error, so we're just doing it here @@ -31,9 +36,19 @@ func (ce ComplexError) FormatError(p xerrors.Printer) error { ce.frame.Format(p) return nil } + func (ce ComplexError) Format(f fmt.State, c rune) { xerrors.FormatError(ce, f, c) } + func (ce ComplexError) Error() string { return fmt.Sprint(ce) } + +func HasErrorCode(err error, code int) bool { + var originalErr ComplexError + if xerrors.As(err, &originalErr) { + return originalErr.Code == MustStopContainer + } + return false +} diff --git a/pkg/commands/service.go b/pkg/commands/service.go index 90e2c6de..3ffbf740 100644 --- a/pkg/commands/service.go +++ b/pkg/commands/service.go @@ -51,8 +51,6 @@ func (s *Service) Restart() error { // Attach attaches to the service func (s *Service) Attach() (*exec.Cmd, error) { - // TODO: if you have a custom command for attaching to a service here is the place to use it - return s.Container.Attach() } diff --git a/pkg/gui/containers_panel.go b/pkg/gui/containers_panel.go index 34ad86da..23bb96dd 100644 --- a/pkg/gui/containers_panel.go +++ b/pkg/gui/containers_panel.go @@ -14,7 +14,6 @@ import ( "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazydocker/pkg/commands" "github.com/jesseduffield/lazydocker/pkg/utils" - "golang.org/x/xerrors" ) // list panel functions @@ -423,25 +422,20 @@ func (gui *Gui) handleContainersRemoveMenu(g *gocui.Gui, v *gocui.View) error { configOptions := options[index].configOptions return gui.WithWaitingStatus(gui.Tr.RemovingStatus, func() error { - 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.Confirm, gui.Tr.MustForceToRemoveContainer, func(g *gocui.Gui, v *gocui.View) error { - return gui.WithWaitingStatus(gui.Tr.RemovingStatus, func() error { - configOptions.Force = true - if err := container.Remove(configOptions); err != nil { - return err - } - return gui.refreshContainersAndServices() - }) - }, nil) - } - } else { - return gui.createErrorPanel(gui.g, cerr.Error()) + if err := container.Remove(configOptions); err != nil { + if commands.HasErrorCode(err, commands.MustStopContainer) { + return gui.createConfirmationPanel(gui.g, v, gui.Tr.Confirm, gui.Tr.MustForceToRemoveContainer, func(g *gocui.Gui, v *gocui.View) error { + return gui.WithWaitingStatus(gui.Tr.RemovingStatus, func() error { + configOptions.Force = true + if err := container.Remove(configOptions); err != nil { + return err + } + return gui.refreshContainersAndServices() + }) + }, nil) } + return gui.createErrorPanel(gui.g, err.Error()) } - return gui.refreshContainersAndServices() }) diff --git a/pkg/gui/services_panel.go b/pkg/gui/services_panel.go index 44baabc7..2f4459a5 100644 --- a/pkg/gui/services_panel.go +++ b/pkg/gui/services_panel.go @@ -258,7 +258,12 @@ func (gui *Gui) handleServiceAttach(g *gocui.Gui, v *gocui.View) error { return nil } + if service.Container == nil { + return gui.createErrorPanel(gui.g, gui.Tr.NoContainers) + } + c, err := service.Attach() + if err != nil { return gui.createErrorPanel(gui.g, err.Error()) }