diff --git a/lib/portainer_cli.rb b/lib/portainer_cli.rb index 16aedc8..b395a81 100644 --- a/lib/portainer_cli.rb +++ b/lib/portainer_cli.rb @@ -30,7 +30,7 @@ module PortainerCli portainer-cli list networks List networks portainer-cli create stack --endpoint ID --name NAME --file FILE portainer-cli create container --endpoint ID --image IMAGE [--name NAME] [--port h:c] [--env K=V] - portainer-cli console Open interactive shell (resolves endpoint automatically) + portainer-cli console [container] Open interactive shell (resolves endpoint automatically) --shell SHELL Shell to use (default: /bin/sh) portainer-cli exec Open interactive shell (manual endpoint) --shell SHELL Shell to use (default: /bin/sh) diff --git a/lib/portainer_cli/commands/console.rb b/lib/portainer_cli/commands/console.rb index 2d041b0..6078bc5 100644 --- a/lib/portainer_cli/commands/console.rb +++ b/lib/portainer_cli/commands/console.rb @@ -25,8 +25,8 @@ module PortainerCli o.on('--shell SHELL', 'Shell to use (default: /bin/sh)') { |v| opts[:shell] = v } end.parse!(args) - stack_name = args.shift or error("Usage: portainer-cli console [--shell /bin/bash]") - container_name = args.shift or error("Usage: portainer-cli console [--shell /bin/bash]") + stack_name = args.shift or error("Usage: portainer-cli console [container] [--shell /bin/bash]") + container_name = args.shift || stack_name stack = resolve_stack(stack_name) endpoint_id = stack['EndpointId'] @@ -107,6 +107,18 @@ module PortainerCli error("Ambiguous container '#{name_or_id}'. Did you mean one of:\n#{list}") end + # 4. Try 'app' as a last resort when the implicit default didn't match + if name_or_id != 'app' && stack_name + app_candidates = containers.select do |c| + names_for.(c).any? { |n| n.match?(/\A#{Regexp.escape(stack_name)}-app-\d+\z/) } + 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')") + return app_candidates.first['Id'] + end + end + available = pool.map { |c| " #{c['Id'][0, 12]} #{names_for.(c).first}" }.join("\n") error("Container '#{name_or_id}' not found. Available:\n#{available}") end