Files
portainer-cli/lib/portainer_cli.rb
T
grzlusandClaude Opus 5.5 2d45ce3af8 Add cp command to copy files out of containers, binary-safe
`portainer-cli cp <stack> <container>:<path> <dest>` goes through Docker's
archive API, so unlike `console --run "cat …"` there's no TTY in between and
binary files (database dumps) arrive intact. Works like `docker cp` for files
and directories; a dest of "-" writes a single file to stdout.

Stack and container lookup moves from Console into a shared Resolver module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 18:57:40 +02:00

87 lines
3.7 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'
require_relative 'portainer_cli/commands/cp'
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)
--endpoint ID Disambiguate when stack name exists on multiple endpoints
--shell SHELL Shell to use (default: /bin/sh)
--run CMD Run command instead of a bare shell
portainer-cli cp <stack> <container>:<path> <dest> Copy a file/directory out of a container (binary-safe)
--endpoint ID Disambiguate when stack name exists on multiple endpoints
--quiet Suppress status output; <dest> of - writes to stdout
portainer-cli exec <endpoint-id> <container> Open interactive shell (manual endpoint)
--shell SHELL Shell to use (default: /bin/sh)
Config file: ~/.config/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 'cp'
require_config!(config)
client = Client.new(config)
Commands::Cp.new(config, client).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