d3cb4fdebd
Ruby CLI tool for interacting with the Portainer API. Supports listing endpoints/stacks/containers/volumes/networks, creating stacks and containers, and opening an interactive exec session in a container via WebSocket. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
53 lines
1.5 KiB
Ruby
53 lines
1.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module PortainerCli
|
|
module Commands
|
|
module Base
|
|
RESET = "\e[0m"
|
|
BOLD = "\e[1m"
|
|
CYAN = "\e[36m"
|
|
GREEN = "\e[32m"
|
|
YELLOW = "\e[33m"
|
|
RED = "\e[31m"
|
|
DIM = "\e[2m"
|
|
|
|
def colorize?
|
|
$stdout.tty?
|
|
end
|
|
|
|
def bold(str) = colorize? ? "#{BOLD}#{str}#{RESET}" : str
|
|
def cyan(str) = colorize? ? "#{CYAN}#{str}#{RESET}" : str
|
|
def green(str) = colorize? ? "#{GREEN}#{str}#{RESET}" : str
|
|
def yellow(str) = colorize? ? "#{YELLOW}#{str}#{RESET}" : str
|
|
def red(str) = colorize? ? "#{RED}#{str}#{RESET}" : str
|
|
def dim(str) = colorize? ? "#{DIM}#{str}#{RESET}" : str
|
|
|
|
def print_table(headers, rows)
|
|
widths = headers.each_with_index.map do |h, i|
|
|
[h.length, rows.map { |r| r[i].to_s.length }.max || 0].max
|
|
end
|
|
|
|
sep = widths.map { |w| '-' * (w + 2) }.join('+')
|
|
header_row = headers.each_with_index.map { |h, i| " #{bold(h.ljust(widths[i]))} " }.join('|')
|
|
|
|
puts sep
|
|
puts header_row
|
|
puts sep
|
|
rows.each do |row|
|
|
puts row.each_with_index.map { |cell, i| " #{cell.to_s.ljust(widths[i])} " }.join('|')
|
|
end
|
|
puts sep
|
|
end
|
|
|
|
def error(msg)
|
|
$stderr.puts red("Error: #{msg}")
|
|
exit 1
|
|
end
|
|
|
|
def require_config!(config)
|
|
error("Not configured. Run: portainer-cli configure") unless config.valid?
|
|
end
|
|
end
|
|
end
|
|
end
|