diff --git a/lib/portainer_cli/commands/console.rb b/lib/portainer_cli/commands/console.rb index 61a4ebd..e7c4645 100644 --- a/lib/portainer_cli/commands/console.rb +++ b/lib/portainer_cli/commands/console.rb @@ -22,8 +22,9 @@ 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 } + o.on('--quiet', 'Suppress status output') { opts[:quiet] = true } end.parse!(args) stack_name = args.shift or error("Usage: portainer-cli console [container] [--run CMD] [--shell /bin/bash]") @@ -32,11 +33,11 @@ module PortainerCli stack = resolve_stack(stack_name) 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]) 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 - $stderr.puts dim("\r\nSession ended.") + status("\r\nSession ended.", opts[:quiet]) rescue PortainerCli::Client::ApiError => e error(e.message) rescue Interrupt @@ -55,6 +56,10 @@ module PortainerCli private + def status(msg, quiet) + $stderr.puts dim(msg) unless quiet + end + def resolve_stack(name_or_id) stacks = @client.get('/api/stacks') match = stacks.find do |s| @@ -67,7 +72,7 @@ module PortainerCli match 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) names_for = ->(c) { Array(c['Names']).map { |n| n.delete_prefix('/') } } @@ -86,7 +91,7 @@ module PortainerCli end if composed.size == 1 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'] end end @@ -99,7 +104,7 @@ module PortainerCli if fuzzy.size == 1 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'] end @@ -115,7 +120,7 @@ module PortainerCli end if app_candidates.size == 1 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'] end end