From f88254eb236f7b83f80a84e460514c69afb31c0d Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Thu, 17 Feb 2022 16:54:46 -0800 Subject: [PATCH] Let #with_plugin load plugins by type --- .../commands/serve/service/provider_service.rb | 18 ++++++------------ .../serve/service/synced_folder_service.rb | 15 +++++---------- plugins/commands/serve/util/service_info.rb | 6 ++++-- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/plugins/commands/serve/service/provider_service.rb b/plugins/commands/serve/service/provider_service.rb index 3b5e725e0..4b30500f3 100644 --- a/plugins/commands/serve/service/provider_service.rb +++ b/plugins/commands/serve/service/provider_service.rb @@ -19,8 +19,7 @@ module VagrantPlugins end def usable(req, ctx) - plugins = Vagrant.plugin("2").local_manager.providers - with_plugin(ctx, plugins, broker: broker) do |plugin| + with_plugin(ctx, :providers, broker: broker) do |plugin| is_usable = plugin.usable? SDK::Provider::UsableResp.new( is_usable: is_usable, @@ -33,8 +32,7 @@ module VagrantPlugins end def installed(req, ctx) - plugins = Vagrant.plugin("2").local_manager.providers - with_plugin(ctx, plugins, broker: broker) do |plugin| + with_plugin(ctx, :providers, broker: broker) do |plugin| is_installed = plugin.installed? SDK::Provider::InstalledResp.new( is_installed: is_installed, @@ -52,8 +50,7 @@ module VagrantPlugins end def action(req, ctx) - plugins = Vagrant.plugin("2").local_manager.providers - with_plugin(ctx, plugins, broker: broker) do |plugin| + with_plugin(ctx, :providers, broker: broker) do |plugin| action_name = req.name.to_sym machine, options = mapper.funcspec_map( req.func_args, @@ -94,8 +91,7 @@ module VagrantPlugins end def machine_id_changed(req, ctx) - plugins = Vagrant.plugin("2").local_manager.providers - with_plugin(ctx, plugins, broker: broker) do |plugin| + with_plugin(ctx, :providers, broker: broker) do |plugin| machine = mapper.funcspec_map(req, expect: [Vagrant::Machine]) provider = plugin.new(machine) provider.machine_id_changed @@ -113,8 +109,7 @@ module VagrantPlugins end def ssh_info(req, ctx) - plugins = Vagrant.plugin("2").local_manager.providers - with_plugin(ctx, plugins, broker: broker) do |plugin| + with_plugin(ctx, :providers, broker: broker) do |plugin| machine = mapper.funcspec_map(req, expect: [Vagrant::Machine]) provider = plugin.new(machine) info = provider.ssh_info @@ -133,8 +128,7 @@ module VagrantPlugins end def state(req, ctx) - plugins = Vagrant.plugin("2").local_manager.providers - with_plugin(ctx, plugins, broker: broker) do |plugin| + with_plugin(ctx, :providers, broker: broker) do |plugin| machine = mapper.funcspec_map(req, expect: [Vagrant::Machine]) provider = plugin.new(machine) machine_state = provider.state diff --git a/plugins/commands/serve/service/synced_folder_service.rb b/plugins/commands/serve/service/synced_folder_service.rb index 38f3c5a4c..fb5204763 100644 --- a/plugins/commands/serve/service/synced_folder_service.rb +++ b/plugins/commands/serve/service/synced_folder_service.rb @@ -24,8 +24,7 @@ module VagrantPlugins end def usable(req, ctx) - plugins = Vagrant.plugin("2").local_manager.synced_folders - with_plugin(ctx, plugins, broker: broker) do |plugin| + with_plugin(ctx, :synced_folders, broker: broker) do |plugin| machine = mapper.funcspec_map( req, expect: [Vagrant::Machine] ) @@ -49,8 +48,7 @@ module VagrantPlugins end def prepare(req, ctx) - plugins = Vagrant.plugin("2").local_manager.synced_folders - with_plugin(ctx, plugins, broker: broker) do |plugin| + with_plugin(ctx, :synced_folders, broker: broker) do |plugin| machine, folders, opts = mapper.funcspec_map( req, expect: [Vagrant::Machine, Type::Folders, Type::Options] @@ -75,8 +73,7 @@ module VagrantPlugins end def enable(req, ctx) - plugins = Vagrant.plugin("2").local_manager.synced_folders - with_plugin(ctx, plugins, broker: broker) do |plugin| + with_plugin(ctx, :synced_folders, broker: broker) do |plugin| machine, folders, opts = mapper.funcspec_map( req, expect: [Vagrant::Machine, Type::Folders, Type::Options] @@ -101,8 +98,7 @@ module VagrantPlugins end def disable(req, ctx) - plugins = Vagrant.plugin("2").local_manager.synced_folders - with_plugin(ctx, plugins, broker: broker) do |plugin| + with_plugin(ctx, :synced_folders, broker: broker) do |plugin| machine, folders, opts = mapper.funcspec_map( req, expect: [Vagrant::Machine, Type::Folders, Type::Options] @@ -126,8 +122,7 @@ module VagrantPlugins end def cleanup(req, ctx) - plugins = Vagrant.plugin("2").local_manager.synced_folders - with_plugin(ctx, plugins, broker: broker) do |plugin| + with_plugin(ctx, :synced_folders, broker: broker) do |plugin| machine, opts = mapper.funcspec_map( req, expect: [Vagrant::Machine, Type::Options] diff --git a/plugins/commands/serve/util/service_info.rb b/plugins/commands/serve/util/service_info.rb index 945f1f6be..e9e0ae971 100644 --- a/plugins/commands/serve/util/service_info.rb +++ b/plugins/commands/serve/util/service_info.rb @@ -4,6 +4,9 @@ module VagrantPlugins # Adds service info helper to be used with services module ServiceInfo def with_info(context, broker:, &block) + if broker.nil? + raise "NO BROKER FOR INFO" + end if !context.metadata["plugin_name"] raise KeyError, "plugin name not defined (metadata content: #{context.metadata.inspect})" @@ -37,8 +40,7 @@ module VagrantPlugins def with_plugin(context, plugins, broker:, &block) with_info(context, broker: broker) do |info| - plugin_name = info.plugin_name - plugin = Array(plugins[plugin_name.to_s.to_sym]).first + plugin = Vagrant.plugin("2").local_manager.send(plugins)[info.plugin_name].to_a.first if !plugin raise NameError, "Failed to locate plugin named #{plugin_name}" end