# 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 List containers portainer-cli list volumes List volumes 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 [container] Open interactive shell (resolves endpoint automatically) --shell SHELL Shell to use (default: /bin/sh) --run CMD Run command instead of a bare shell portainer-cli exec 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