Class: Pvectl::PluginLoader
- Inherits:
-
Object
- Object
- Pvectl::PluginLoader
- Defined in:
- lib/pvectl/plugin_loader.rb
Overview
Discovers and loads commands from built-in sources and external plugins.
Loading order:
-
Built-in commands (BUILTIN_COMMANDS list)
-
Gem-based plugins (pvectl-plugin-* gems)
-
Directory-based plugins (~/.pvectl/plugins/*.rb)
Order matters: built-in commands register first (including their ResourceRegistries), then plugins can extend those registries.
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
-
.directory_plugins_path ⇒ String
Returns the directory path for local plugins.
-
.flush_registered_plugins(cli) ⇒ void
Calls register on all queued plugins and clears the queue.
-
.load_all(cli) ⇒ void
Loads all commands: built-in, gem plugins, directory plugins.
-
.load_builtins(cli) ⇒ void
Loads built-in commands.
-
.load_directory_plugins(cli) ⇒ void
Discovers and loads directory-based plugins.
-
.load_gem_plugins(cli) ⇒ void
Discovers and loads gem-based plugins.
-
.register_plugin(klass) ⇒ void
Registers an external plugin command for loading.
-
.registered_plugins ⇒ Array<Class>
Returns currently registered plugins (for testing).
-
.reset! ⇒ void
private
Resets state (for testing).
Class Method Details
.directory_plugins_path ⇒ String
Returns the directory path for local plugins.
130 131 132 |
# File 'lib/pvectl/plugin_loader.rb', line 130 def directory_plugins_path File.("~/.pvectl/plugins") end |
.flush_registered_plugins(cli) ⇒ void
This method returns an undefined value.
Calls register on all queued plugins and clears the queue.
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.}" 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.
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.
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.
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.}" 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”).
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.}" 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.
63 64 65 |
# File 'lib/pvectl/plugin_loader.rb', line 63 def register_plugin(klass) @registered_plugins << klass end |
.registered_plugins ⇒ Array<Class>
Returns currently registered plugins (for testing).
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 |