From dc64d85adf2e37d864b145db5e3d8896c6b047de Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Tue, 26 Jan 2021 16:45:10 -0800 Subject: [PATCH] Extract option building to standalone method. Update help to use method. --- .../commands/serve/service/command_service.rb | 69 ++++++++++--------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/plugins/commands/serve/service/command_service.rb b/plugins/commands/serve/service/command_service.rb index 16d015588..9b09b1924 100644 --- a/plugins/commands/serve/service/command_service.rb +++ b/plugins/commands/serve/service/command_service.rb @@ -7,17 +7,9 @@ module VagrantPlugins end def help(*args) - plugin_name = args.last.metadata["plugin_name"] - plugin = Vagrant::Plugin::V2::Plugin.manager.commands[plugin_name.to_sym].to_a.first - if !plugin - raise "Failed to locate command plugin for: #{plugin_name}" - end - $stashed_opts = nil - klass = Class.new(plugin.call) - klass.class_eval { def parse_options(opts); $stashed_opts = opts; nil; end }; - klass.new(['-h'], {}).execute + options = command_options_for(args.last.metadata["plugin_name"]) Hashicorp::Vagrant::Sdk::Command::HelpResp.new( - help: $stashed_opts.help() + help: options.help ) end @@ -42,31 +34,10 @@ module VagrantPlugins end def flags(*args) - plugin_name = args.last.metadata["plugin_name"] - plugin = Vagrant::Plugin::V2::Plugin.manager.commands[plugin_name.to_sym].to_a.first - if !plugin - raise "Failed to locate command plugin for: #{plugin_name}" - end - - # Create a new anonymous class based on the command class - # so we can modify the setup behavior - klass = Class.new(plugin.call) - - # Update the option parsing to store the provided options, and then return - # a nil value. The nil return will force the command to call help and not - # actually execute anything. - klass.class_eval do - def parse_options(opts) - Thread.current.thread_variable_set(:command_options, opts) - nil - end - end - - # Execute the command to populate our options - klass.new([], {}).execute + options = command_options_for(args.last.metadata["plugin_name"]) # Now we can build our list of flags - flags = Thread.current.thread_variable_get(:command_options).top.list.find_all { |o| + flags = options.top.list.find_all { |o| o.is_a?(OptionParser::Switch) }.map { |o| Hashicorp::Vagrant::Sdk::Command::Flag.new( @@ -86,6 +57,38 @@ module VagrantPlugins flags: flags ) end + + def command_options_for(name) + plugin = Vagrant::Plugin::V2::Plugin.manager.commands[name.to_sym].to_a.first + if !plugin + raise "Failed to locate command plugin for: #{name}" + end + + # Create a new anonymous class based on the command class + # so we can modify the setup behavior + klass = Class.new(plugin.call) + + # Update the option parsing to store the provided options, and then return + # a nil value. The nil return will force the command to call help and not + # actually execute anything. + klass.class_eval do + def parse_options(opts) + Thread.current.thread_variable_set(:command_options, opts) + nil + end + end + + # Execute the command to populate our options + klass.new([], {}).execute + + options = Thread.current.thread_variable_get(:command_options) + + # Clean our option data out of the thread + Thread.current.thread_variable_set(:command_options, nil) + + # Send the options back + options + end end end end