Class: Pvectl::PluginLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/plugin_loader.rb

Overview

Discovers and loads commands from built-in sources and external plugins.

Loading order:

  1. Built-in commands (BUILTIN_COMMANDS list)

  2. Gem-based plugins (pvectl-plugin-* gems)

  3. Directory-based plugins (~/.pvectl/plugins/*.rb)

Order matters: built-in commands register first (including their ResourceRegistries), then plugins can extend those registries.

Examples:

Plugin registration from a gem

Pvectl::PluginLoader.register_plugin(MyPlugin::Command)

Constant Summary collapse

BUILTIN_COMMANDS =

Built-in commands that ship with pvectl. Each must implement .register(cli).

[
  Commands::Ping,
  Commands::Config::Command,
  Commands::Cloudinit,
  Commands::Get::Command,
  Commands::Top::Command,
  Commands::Logs::Command,
  Commands::Describe::Command,
  Commands::Start,
  Commands::Stop,
  Commands::Shutdown,
  Commands::Restart,
  Commands::Reset,
  Commands::Suspend,
  Commands::Resume,
  Commands::CreateVm,
  Commands::DeleteVm,
  Commands::EditVm,
  Commands::SetVm,
  Commands::CloneVm,
  Commands::MigrateVm,
  Commands::MoveDiskVm,
  Commands::FeatureVm,
  Commands::TemplateVm,
  Commands::RollbackSnapshot,
  Commands::RestoreBackup,
  Commands::Console,
  Commands::SendkeyVm,
  Commands::Pull,
  Commands::Push,
  Commands::Service,
  Commands::Apt,
  Commands::WakeonlanNode,
  Commands::UnlinkDiskVm,
].freeze

Class Method Summary collapse

Class Method Details

.directory_plugins_pathString

Returns the directory path for local plugins.

Returns:

  • (String)

    plugins directory path



130
131
132
# File 'lib/pvectl/plugin_loader.rb', line 130

def directory_plugins_path
  File.expand_path("~/.pvectl/plugins")
end

.flush_registered_plugins(cli) ⇒ void

This method returns an undefined value.

Calls register on all queued plugins and clears the queue.

Parameters:

  • cli (GLI::App)

    the CLI application object



138
139
140
141
142
143
144
145
146
# File 'lib/pvectl/plugin_loader.rb', line 138

def flush_registered_plugins(cli)
  @registered_plugins.each do |klass|
    klass.register(cli)
  rescue StandardError => e
    warn "Warning: Failed to register plugin #{klass}: #{e.message}"
    warn e.backtrace.join("\n") if ENV["GLI_DEBUG"] == "true"
  end
  @registered_plugins.clear
end

.load_all(cli) ⇒ void

This method returns an undefined value.

Loads all commands: built-in, gem plugins, directory plugins.

Parameters:

  • cli (GLI::App)

    the CLI application object



78
79
80
81
82
# File 'lib/pvectl/plugin_loader.rb', line 78

def load_all(cli)
  load_builtins(cli)
  load_gem_plugins(cli)
  load_directory_plugins(cli)
end

.load_builtins(cli) ⇒ void

This method returns an undefined value.

Loads built-in commands.

Parameters:

  • cli (GLI::App)

    the CLI application object



88
89
90
# File 'lib/pvectl/plugin_loader.rb', line 88

def load_builtins(cli)
  BUILTIN_COMMANDS.each { |cmd| cmd.register(cli) }
end

.load_directory_plugins(cli) ⇒ void

This method returns an undefined value.

Discovers and loads directory-based plugins.

Scans ~/.pvectl/plugins/*.rb for plugin files.

Parameters:

  • cli (GLI::App)

    the CLI application object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/pvectl/plugin_loader.rb', line 115

def load_directory_plugins(cli)
  return unless Dir.exist?(directory_plugins_path)

  Dir.glob(File.join(directory_plugins_path, "*.rb")).sort.each do |plugin_file|
    require plugin_file
  rescue StandardError => e
    warn "Warning: Failed to load plugin #{plugin_file}: #{e.message}"
    warn e.backtrace.join("\n") if ENV["GLI_DEBUG"] == "true"
  end
  flush_registered_plugins(cli)
end

.load_gem_plugins(cli) ⇒ void

This method returns an undefined value.

Discovers and loads gem-based plugins.

Searches for gems matching pvectl-plugin-* via Gem.find_files(“pvectl_plugin/register”).

Parameters:

  • cli (GLI::App)

    the CLI application object



99
100
101
102
103
104
105
106
107
# File 'lib/pvectl/plugin_loader.rb', line 99

def load_gem_plugins(cli)
  Gem.find_files("pvectl_plugin/register").each do |register_file|
    require register_file
  rescue StandardError => e
    warn "Warning: Failed to load plugin #{register_file}: #{e.message}"
    warn e.backtrace.join("\n") if ENV["GLI_DEBUG"] == "true"
  end
  flush_registered_plugins(cli)
end

.register_plugin(klass) ⇒ void

This method returns an undefined value.

Registers an external plugin command for loading.

Parameters:

  • klass (Class)

    plugin class that implements .register(cli)



63
64
65
# File 'lib/pvectl/plugin_loader.rb', line 63

def register_plugin(klass)
  @registered_plugins << klass
end

.registered_pluginsArray<Class>

Returns currently registered plugins (for testing).

Returns:

  • (Array<Class>)

    registered plugin classes



70
71
72
# File 'lib/pvectl/plugin_loader.rb', line 70

def registered_plugins
  @registered_plugins.dup
end

.reset!void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Resets state (for testing).



152
153
154
# File 'lib/pvectl/plugin_loader.rb', line 152

def reset!
  @registered_plugins = []
end