Add --quiet flag to suppress all status output

Silences stack/endpoint/container resolution messages for scripting
and AI use cases where only raw I/O should be on stdout/stderr.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-22 14:14:29 +01:00
parent 8e45e826ff
commit 56f73f72e0
+14 -9
View File
@@ -22,8 +22,9 @@ module PortainerCli
def run(args) def run(args)
opts = { shell: '/bin/sh' } opts = { shell: '/bin/sh' }
OptionParser.new do |o| 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 } o.on('--run CMD', 'Run command instead of a bare shell') { |v| opts[:run] = v }
o.on('--quiet', 'Suppress status output') { opts[:quiet] = true }
end.parse!(args) end.parse!(args)
stack_name = args.shift or error("Usage: portainer-cli console <stack> [container] [--run CMD] [--shell /bin/bash]") stack_name = args.shift or error("Usage: portainer-cli console <stack> [container] [--run CMD] [--shell /bin/bash]")
@@ -32,11 +33,11 @@ module PortainerCli
stack = resolve_stack(stack_name) stack = resolve_stack(stack_name)
endpoint_id = stack['EndpointId'] endpoint_id = stack['EndpointId']
$stderr.puts dim("Stack: #{stack['Name']} Endpoint: #{endpoint_id}") status("Stack: #{stack['Name']} Endpoint: #{endpoint_id}", opts[:quiet])
container_id = resolve_container(endpoint_id, container_name, stack['Name']) container_id = resolve_container(endpoint_id, container_name, stack['Name'], opts[:quiet])
$stderr.puts dim("Connecting to #{container_name} (#{container_id[0, 12]})...") status("Connecting to #{container_name} (#{container_id[0, 12]})...", opts[:quiet])
exec_id = create_exec(endpoint_id, container_id, opts[:shell], opts[:run]) exec_id = create_exec(endpoint_id, container_id, opts[:shell], opts[:run])
ws_url = exec_websocket_url(exec_id, endpoint_id) ws_url = exec_websocket_url(exec_id, endpoint_id)
@@ -46,7 +47,7 @@ module PortainerCli
WebsocketExec.new(ws_url, @config.auth_header, ssl_verify: @config.ssl_verify, debug: @debug).run WebsocketExec.new(ws_url, @config.auth_header, ssl_verify: @config.ssl_verify, debug: @debug).run
$stderr.puts dim("\r\nSession ended.") status("\r\nSession ended.", opts[:quiet])
rescue PortainerCli::Client::ApiError => e rescue PortainerCli::Client::ApiError => e
error(e.message) error(e.message)
rescue Interrupt rescue Interrupt
@@ -55,6 +56,10 @@ module PortainerCli
private private
def status(msg, quiet)
$stderr.puts dim(msg) unless quiet
end
def resolve_stack(name_or_id) def resolve_stack(name_or_id)
stacks = @client.get('/api/stacks') stacks = @client.get('/api/stacks')
match = stacks.find do |s| match = stacks.find do |s|
@@ -67,7 +72,7 @@ module PortainerCli
match match
end end
def resolve_container(endpoint_id, name_or_id, stack_name = nil) def resolve_container(endpoint_id, name_or_id, stack_name = nil, quiet = false)
containers = @client.get("/api/endpoints/#{endpoint_id}/docker/containers/json", all: 1) containers = @client.get("/api/endpoints/#{endpoint_id}/docker/containers/json", all: 1)
names_for = ->(c) { Array(c['Names']).map { |n| n.delete_prefix('/') } } names_for = ->(c) { Array(c['Names']).map { |n| n.delete_prefix('/') } }
@@ -86,7 +91,7 @@ module PortainerCli
end end
if composed.size == 1 if composed.size == 1
matched_name = names_for.(composed.first).first matched_name = names_for.(composed.first).first
$stderr.puts dim("Matched '#{name_or_id}' → #{matched_name}") status("Matched '#{name_or_id}' → #{matched_name}", quiet)
return composed.first['Id'] return composed.first['Id']
end end
end end
@@ -99,7 +104,7 @@ module PortainerCli
if fuzzy.size == 1 if fuzzy.size == 1
matched_name = names_for.(fuzzy.first).first matched_name = names_for.(fuzzy.first).first
$stderr.puts dim("Matched '#{name_or_id}' → #{matched_name}") status("Matched '#{name_or_id}' → #{matched_name}", quiet)
return fuzzy.first['Id'] return fuzzy.first['Id']
end end
@@ -115,7 +120,7 @@ module PortainerCli
end end
if app_candidates.size == 1 if app_candidates.size == 1
matched_name = names_for.(app_candidates.first).first matched_name = names_for.(app_candidates.first).first
$stderr.puts dim("Matched '#{name_or_id}' → #{matched_name} (fallback to 'app')") status("Matched '#{name_or_id}' → #{matched_name} (fallback to 'app')", quiet)
return app_candidates.first['Id'] return app_candidates.first['Id']
end end
end end