Files
portainer-cli/lib/portainer_cli.rb
T
grzlus d8e00a4f1a Add console command with automatic endpoint resolution
portainer-cli console <stack> <container> looks up the stack by name to
find its endpoint ID automatically, so the user never needs to specify it.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-22 13:01:41 +01:00

76 lines
2.8 KiB
Ruby

# frozen_string_literal: true
require_relative 'portainer_cli/version'
require_relative 'portainer_cli/config'
require_relative 'portainer_cli/client'
require_relative 'portainer_cli/commands/base'
require_relative 'portainer_cli/commands/configure'
require_relative 'portainer_cli/commands/list'
require_relative 'portainer_cli/commands/create'
require_relative 'portainer_cli/commands/exec'
require_relative 'portainer_cli/commands/console'
module PortainerCli
module CLI
def self.require_config!(config)
return if config.valid?
$stderr.puts "Not configured. Run: portainer-cli configure"
exit 1
end
HELP = <<~HELP
portainer-cli #{PortainerCli::VERSION}
Usage:
portainer-cli configure Set Portainer URL and credentials
portainer-cli list endpoints List environments
portainer-cli list stacks List stacks
portainer-cli list containers <endpoint-id> List containers
portainer-cli list volumes <endpoint-id> List volumes
portainer-cli list networks <endpoint-id> 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 <stack> <container> Open interactive shell (resolves endpoint automatically)
--shell SHELL Shell to use (default: /bin/sh)
portainer-cli exec <endpoint-id> <container> Open interactive shell (manual endpoint)
--shell SHELL Shell to use (default: /bin/sh)
Config file: ~/.portainer-cli/config.yml
HELP
def self.run(argv)
config = Config.load
cmd = argv.shift&.downcase
case cmd
when 'configure', 'config'
Commands::Configure.new(config).run(argv)
when 'list', 'ls'
require_config!(config)
client = Client.new(config)
Commands::List.new(config, client).run(argv)
when 'create', 'add'
require_config!(config)
client = Client.new(config)
Commands::Create.new(config, client).run(argv)
when 'console'
require_config!(config)
client = Client.new(config)
Commands::Console.new(config, client).run(argv)
when 'exec', 'shell'
require_config!(config)
client = Client.new(config)
Commands::Exec.new(config, client).run(argv)
when 'version', '--version', '-v'
puts "portainer-cli #{PortainerCli::VERSION}"
when 'help', '--help', '-h', nil
puts HELP
else
$stderr.puts "Unknown command: #{cmd}"
$stderr.puts HELP
exit 1
end
end
end
end