diff --git a/pkg/commands/container.go b/pkg/commands/container.go index f3f300e5..3ad3e602 100644 --- a/pkg/commands/container.go +++ b/pkg/commands/container.go @@ -2,6 +2,7 @@ package commands import ( "context" + "os/exec" "strings" "github.com/docker/docker/api/types" @@ -19,6 +20,7 @@ type Container struct { Container types.Container DisplayString string Client *client.Client + OSCommand *OSCommand } // GetDisplayStrings returns the dispaly string of Container @@ -74,3 +76,9 @@ func (c *Container) Stop() error { func (c *Container) Restart() error { return c.Client.ContainerRestart(context.Background(), c.ID, nil) } + +// Attach attaches the container +func (c *Container) Attach() *exec.Cmd { + cmd := c.OSCommand.PrepareSubProcess("docker", "attach", "--sig-proxy=false", c.ID) + return cmd +} diff --git a/pkg/commands/docker.go b/pkg/commands/docker.go index 604eaa5a..e3dff420 100644 --- a/pkg/commands/docker.go +++ b/pkg/commands/docker.go @@ -61,6 +61,7 @@ func (c *DockerCommand) GetContainers() ([]*Container, error) { ServiceName: serviceName, Container: container, Client: c.Client, + OSCommand: c.OSCommand, } } diff --git a/pkg/gui/containers_panel.go b/pkg/gui/containers_panel.go index 9b24c88d..af5b8814 100644 --- a/pkg/gui/containers_panel.go +++ b/pkg/gui/containers_panel.go @@ -384,3 +384,18 @@ func (gui *Gui) handleContainerRestart(g *gocui.Gui, v *gocui.View) error { return gui.refreshContainers() }) } + +func (gui *Gui) handleContainerAttach(g *gocui.Gui, v *gocui.View) error { + container, err := gui.getSelectedContainer(g) + if err != nil { + return nil + } + + subProcess := container.Attach() + if subProcess != nil { + gui.SubProcess = subProcess + return gui.Errors.ErrSubProcess + } + + return nil +} diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 5521baee..2576ca4d 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -172,6 +172,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { Handler: gui.handleContainerRestart, Description: gui.Tr.SLocalize("restartContainer"), }, + { + ViewName: "containers", + Key: 'a', + Modifier: gocui.ModNone, + Handler: gui.handleContainerAttach, + Description: gui.Tr.SLocalize("attachContainer"), + }, } // TODO: add more views here diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 433df208..4025e3d5 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -806,5 +806,9 @@ func addEnglish(i18nObject *i18n.Bundle) error { ID: "nextContext", Other: "next context", }, + &i18n.Message{ + ID: "attachContainer", + Other: "attach to container", + }, ) }