Get help from commands that have subcommands

This commit is contained in:
sophia 2021-04-07 16:36:31 -05:00 committed by Paul Hinze
parent 4343264054
commit 0c1e44f553
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
10 changed files with 71 additions and 19 deletions

View File

@ -100,15 +100,15 @@ func (b *Basis) Init() (result *vagrant_server.Job_InitResult, err error) {
b.logger.Trace("started a new plugin for init", "name", name)
syn, err := cmd.Synopsis()
if err != nil {
b.logger.Error("failed to get synopsis", "error", err)
b.logger.Error("failed to get synopsis for command "+name, "error", err)
}
hlp, err := cmd.Help()
if err != nil {
b.logger.Error("failed to get help", "error", err)
b.logger.Error("failed to get help for command "+name, "error", err)
}
flgs, err := cmd.Flags()
if err != nil {
b.logger.Error("failed to get flags", "error", err)
b.logger.Error("failed to get flags for command "+name, "error", err)
}
result.Commands = append(
result.Commands,

View File

@ -57,6 +57,7 @@ module VagrantPlugins
end
@env.ui.info(opts.help, prefix: false)
opts.help
end
end
end

View File

@ -87,6 +87,7 @@ module VagrantPlugins
end
@env.ui.info(opts.help, prefix: false)
opts.help
end
end
end

View File

@ -65,6 +65,7 @@ module VagrantPlugins
end
@env.ui.info(opts.help, prefix: false)
opts.help
end
end
end

View File

@ -69,6 +69,7 @@ module VagrantPlugins
end
@env.ui.info(opts.help, prefix: false)
opts.help
end
end
end

View File

@ -69,6 +69,7 @@ module VagrantPlugins
end
@env.ui.info(opts.help, prefix: false)
opts.help
end
end
end

View File

@ -97,6 +97,7 @@ module VagrantPlugins
end
@env.ui.info(opts.help, prefix: false)
opts.help
end
end
end

View File

@ -73,6 +73,7 @@ module VagrantPlugins
end
@env.ui.info(opts.help, prefix: false)
opts.help
end
end
end

View File

@ -87,6 +87,7 @@ module VagrantPlugins
end
@env.ui.info(opts.help, prefix: false)
opts.help
end
end
end

View File

@ -16,9 +16,9 @@ module VagrantPlugins
def help(req, ctx)
ServiceInfo.with_info(ctx) do |info|
options = command_options_for(info.plugin_name)
hlp = command_help_for(info.plugin_name)
SDK::Command::HelpResp.new(
help: options.help
help: hlp
)
end
end
@ -49,19 +49,23 @@ module VagrantPlugins
ServiceInfo.with_info(ctx) do |info|
options = command_options_for(info.plugin_name)
# Now we can build our list of flags
flags = options.top.list.find_all { |o|
o.is_a?(OptionParser::Switch)
}.map { |o|
SDK::Command::Flag.new(
description: o.desc.join(" "),
long_name: o.switch_name,
short_name: o.short.first,
type: o.is_a?(OptionParser::Switch::NoArgument) ?
SDK::Command::Flag::Type::BOOL :
SDK::Command::Flag::Type::STRING
)
}
if !options.nil?
# Now we can build our list of flags
flags = options.top.list.find_all { |o|
o.is_a?(OptionParser::Switch)
}.map { |o|
SDK::Command::Flag.new(
description: o.desc.join(" "),
long_name: o.switch_name,
short_name: o.short.first,
type: o.is_a?(OptionParser::Switch::NoArgument) ?
SDK::Command::Flag::Type::BOOL :
SDK::Command::Flag::Type::STRING
)
}
else
flags = []
end
SDK::Command::FlagsResp.new(
flags: flags
@ -89,8 +93,9 @@ module VagrantPlugins
end
end
env = Vagrant::Environment.new()
# Execute the command to populate our options
klass.new([], {}).execute
klass.new([], env).execute
options = Thread.current.thread_variable_get(:command_options)
@ -101,6 +106,45 @@ module VagrantPlugins
options
end
def command_help_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
env = Vagrant::Environment.new()
# Execute the command to populate our options
cmd_plg = klass.new([], env)
begin
return cmd_plg.help
rescue
# The plugin does not have a help command.
# That's fine, will get it from parse args
end
cmd_plg.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.help
end
def execute_spec(req, ctx)
SDK::FuncSpec.new(
name: "execute_spec",