Add --run flag to console command for running a specific command

--run CMD wraps the command in the shell: /bin/sh -c CMD, keeping TTY
mode so interactive commands like 'rails c' or 'bash' work correctly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-22 14:09:33 +01:00
parent 382ba15f15
commit 8e45e826ff
2 changed files with 8 additions and 5 deletions
+7 -5
View File
@@ -22,10 +22,11 @@ module PortainerCli
def run(args)
opts = { shell: '/bin/sh' }
OptionParser.new do |o|
o.on('--shell SHELL', 'Shell to use (default: /bin/sh)') { |v| opts[:shell] = v }
o.on('--shell SHELL', 'Shell to use (default: /bin/sh)') { |v| opts[:shell] = v }
o.on('--run CMD', 'Run command instead of a bare shell') { |v| opts[:run] = v }
end.parse!(args)
stack_name = args.shift or error("Usage: portainer-cli console <stack> [container] [--shell /bin/bash]")
stack_name = args.shift or error("Usage: portainer-cli console <stack> [container] [--run CMD] [--shell /bin/bash]")
container_name = args.shift || stack_name
stack = resolve_stack(stack_name)
@@ -37,7 +38,7 @@ module PortainerCli
$stderr.puts dim("Connecting to #{container_name} (#{container_id[0, 12]})...")
exec_id = create_exec(endpoint_id, container_id, opts[:shell])
exec_id = create_exec(endpoint_id, container_id, opts[:shell], opts[:run])
ws_url = exec_websocket_url(exec_id, endpoint_id)
send_resize(endpoint_id, exec_id) if $stdout.tty?
@@ -123,7 +124,8 @@ module PortainerCli
error("Container '#{name_or_id}' not found. Available:\n#{available}")
end
def create_exec(endpoint_id, container_id, shell)
def create_exec(endpoint_id, container_id, shell, run_cmd = nil)
cmd = run_cmd ? [shell, '-c', run_cmd] : [shell]
result = @client.post(
"/api/endpoints/#{endpoint_id}/docker/containers/#{container_id}/exec",
{
@@ -131,7 +133,7 @@ module PortainerCli
'AttachStdout' => true,
'AttachStderr' => true,
'Tty' => true,
'Cmd' => [shell]
'Cmd' => cmd
}
)
result['Id']