From 430600063cea638f91bf7b15830b0e62d7e768ca Mon Sep 17 00:00:00 2001 From: Sergey Ponomarev Date: Sat, 12 Dec 2020 18:40:31 +0200 Subject: [PATCH 1/2] Exec shell: autodetect container shell Some containers may have bash installed but some have only basic sh (Ash/Dash). There is other shells too: ZSh, tsh etc. We can automatically detect what shell is configured. See explanation how it works https://github.com/moby/moby/issues/41702 --- docs/Config.md | 2 +- pkg/config/app_config.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/Config.md b/docs/Config.md index c67562f..5f35dc7 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -47,7 +47,7 @@ customCommands: containers: - name: bash attach: true - command: docker exec -it {{ .Container.ID }} /bin/sh + command: "docker exec -it {{ .Container.ID }} /bin/sh -c 'eval $(grep ^$(id -un): /etc/passwd | cut -d : -f 7-)'" serviceNames: [] oS: openCommand: open {{filename}} diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index edf4591..8c8fa8a 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -344,7 +344,7 @@ func GetDefaultConfig() UserConfig { Containers: []CustomCommand{ { Name: "bash", - Command: "docker exec -it {{ .Container.ID }} /bin/sh", + Command: "docker exec -it {{ .Container.ID }} /bin/sh -c 'eval $(grep ^$(id -un): /etc/passwd | cut -d : -f 7-)'", Attach: true, }, }, From 2688801cbd161b93199bea48cde2f9dbdf0af848 Mon Sep 17 00:00:00 2001 From: Sergey Ponomarev Date: Sat, 12 Dec 2020 18:45:24 +0200 Subject: [PATCH 2/2] Separate menu item for Exec shell Login to container's shell is very popular operation and to make it easier it will be better to make it as a separate menu item with a dedicated hot key. --- pkg/gui/containers_panel.go | 15 +++++++++++++++ pkg/gui/keybindings.go | 7 +++++++ pkg/i18n/english.go | 2 ++ 3 files changed, 24 insertions(+) diff --git a/pkg/gui/containers_panel.go b/pkg/gui/containers_panel.go index ac97ecf..fb19ee8 100644 --- a/pkg/gui/containers_panel.go +++ b/pkg/gui/containers_panel.go @@ -509,6 +509,21 @@ func (gui *Gui) handleContainerViewLogs(g *gocui.Gui, v *gocui.View) error { return gui.Errors.ErrSubProcess } +func (gui *Gui) handleContainersExecShell(g *gocui.Gui, v *gocui.View) error { + container, err := gui.getSelectedContainer() + if err != nil { + return nil + } + commandObject := gui.DockerCommand.NewCommandObject(commands.CommandObject{ + Container: container, + }) + resolvedCommand := utils.ApplyTemplate("docker exec -it {{ .Container.ID }} /bin/sh -c 'eval $(grep ^$(id -un): /etc/passwd | cut -d : -f 7-)'", commandObject) + // attach and return the subprocess error + cmd := gui.OSCommand.ExecutableFromString(resolvedCommand) + gui.SubProcess = cmd + return gui.Errors.ErrSubProcess +} + func (gui *Gui) handleContainersCustomCommand(g *gocui.Gui, v *gocui.View) error { container, err := gui.getSelectedContainer() if err != nil { diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 953ec8e..33b5062 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -248,6 +248,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { Handler: gui.handleContainerViewLogs, Description: gui.Tr.ViewLogs, }, + { + ViewName: "containers", + Key: 'E', + Modifier: gocui.ModNone, + Handler: gui.handleContainersExecShell, + Description: gui.Tr.ExecShell, + }, { ViewName: "containers", Key: 'c', diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index b5c4799..43f3df1 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -80,6 +80,7 @@ type TranslationSet struct { StopAllContainers string RemoveAllContainers string ViewRestartOptions string + ExecShell string RunCustomCommand string ViewBulkCommands string @@ -148,6 +149,7 @@ func englishSet() TranslationSet { StopAllContainers: "stop all containers", RemoveAllContainers: "remove all containers (forced)", ViewRestartOptions: "view restart options", + ExecShell: "exec shell", RunCustomCommand: "run predefined custom command", ViewBulkCommands: "view bulk commands",