Initial implementation of portainer-cli
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>
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'yaml'
|
||||
require 'fileutils'
|
||||
|
||||
module PortainerCli
|
||||
class Config
|
||||
CONFIG_DIR = File.expand_path('~/.portainer-cli').freeze
|
||||
CONFIG_FILE = File.join(CONFIG_DIR, 'config.yml').freeze
|
||||
|
||||
attr_accessor :url, :token, :api_key
|
||||
|
||||
def self.load
|
||||
new.tap(&:read)
|
||||
end
|
||||
|
||||
def read
|
||||
return unless File.exist?(CONFIG_FILE)
|
||||
|
||||
data = YAML.safe_load(File.read(CONFIG_FILE), permitted_classes: [], symbolize_names: false) || {}
|
||||
@url = data['url']
|
||||
@token = data['token']
|
||||
@api_key = data['api_key']
|
||||
end
|
||||
|
||||
def save
|
||||
FileUtils.mkdir_p(CONFIG_DIR)
|
||||
data = {}
|
||||
data['url'] = @url if @url
|
||||
data['token'] = @token if @token
|
||||
data['api_key'] = @api_key if @api_key
|
||||
File.write(CONFIG_FILE, YAML.dump(data))
|
||||
File.chmod(0o600, CONFIG_FILE)
|
||||
end
|
||||
|
||||
def valid?
|
||||
!@url.nil? && !@url.empty? && auth_set?
|
||||
end
|
||||
|
||||
def auth_header
|
||||
if @api_key && !@api_key.empty?
|
||||
{ 'X-API-Key' => @api_key }
|
||||
elsif @token && !@token.empty?
|
||||
{ 'Authorization' => "Bearer #{@token}" }
|
||||
else
|
||||
{}
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def auth_set?
|
||||
(@token && !@token.empty?) || (@api_key && !@api_key.empty?)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user