Files
portainer-cli/lib/portainer_cli.rb
T
grzlus 382ba15f15 Make container name optional in console command
When omitted, tries: 1) same name as stack, 2) 'app'.
So 'portainer-cli console librarian' resolves to librarian-librarian-1,
and 'portainer-cli console myapp' falls back to myapp-app-1.

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

77 lines
2.9 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)
debug = argv.delete('--debug')
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, debug: debug).run(argv)
when 'exec', 'shell'
require_config!(config)
client = Client.new(config)
Commands::Exec.new(config, client, debug: debug).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