This commit is contained in:
Jesse Duffield 2019-06-08 13:41:40 +10:00
parent 7e45881dc3
commit 71f05ff80a
5 changed files with 32 additions and 23 deletions

View file

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

View file

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

View file

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

View file

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

View file

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