diff --git a/.gitignore b/.gitignore index 9a3e18c97..e34753ce9 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,5 @@ doc/ # Box storage for spec test/vagrant-spec/boxes/*.box + +simple_speed.py diff --git a/builtin/myplugin/command.go b/builtin/myplugin/command.go index a2013939c..7033104ae 100644 --- a/builtin/myplugin/command.go +++ b/builtin/myplugin/command.go @@ -6,7 +6,6 @@ import ( "github.com/DavidGamba/go-getoptions/option" "github.com/hashicorp/vagrant-plugin-sdk/component" - plugincore "github.com/hashicorp/vagrant-plugin-sdk/core" "github.com/hashicorp/vagrant-plugin-sdk/docs" "github.com/hashicorp/vagrant-plugin-sdk/terminal" @@ -60,11 +59,25 @@ func (c *Command) ExecuteFunc() interface{} { return c.Execute } -// HelpFunc implements component.Command +// SubcommandFunc implements component.Command func (c *Command) SubcommandsFunc() interface{} { return c.Subcommands } +// CommandInfoFunc implements component.Command +func (c *Command) CommandInfoFunc() interface{} { + return c.CommandInfo +} + +func (c *Command) CommandInfo() *plugincore.CommandInfo { + return &plugincore.CommandInfo{ + Name: []string{"myplugin"}, + Help: c.Help(), + Synopsis: c.Synopsis(), + Flags: c.Flags(), + } +} + func (c *Command) Synopsis() string { return "I don't really do anything" } @@ -87,8 +100,8 @@ func (c *Command) Flags() []*option.Option { return []*option.Option{booltest, stringflag} } -func (c *Command) Subcommands() []*plugincore.Command { - return nil +func (c *Command) Subcommands() []string { + return []string{} } func (c *Command) Execute(trm terminal.UI, env plugincore.Project) int64 { diff --git a/builtin/myplugin/main.go b/builtin/myplugin/main.go index c1b70c888..8f1bc7324 100644 --- a/builtin/myplugin/main.go +++ b/builtin/myplugin/main.go @@ -4,7 +4,7 @@ import ( sdk "github.com/hashicorp/vagrant-plugin-sdk" ) -//go:generate protoc -I ../../.. --go_opt=plugins=grpc --go_out=../../.. vagrant-agogo/builtin/myplugin/plugin.proto +//go:generate protoc -I ../../.. --go_opt=plugins=grpc --go_out=../../.. vagrant-ruby/builtin/myplugin/plugin.proto // Options are the SDK options to use for instantiation. var Options = []sdk.Option{ diff --git a/builtin/myplugin/plugin.proto b/builtin/myplugin/plugin.proto index 0fb9749ca..0cc37cd6c 100644 --- a/builtin/myplugin/plugin.proto +++ b/builtin/myplugin/plugin.proto @@ -5,4 +5,3 @@ package myplugin; option go_package = "vagrant-agogo/builtin/myplugin"; message UpResult {} - diff --git a/internal/core/basis.go b/internal/core/basis.go index 1edbf6954..cbf945b2b 100644 --- a/internal/core/basis.go +++ b/internal/core/basis.go @@ -98,17 +98,9 @@ func (b *Basis) Init() (result *vagrant_server.Job_InitResult, err error) { continue } b.logger.Trace("started a new plugin for init", "name", name) - syn, err := cmd.Synopsis() + cmdInfo, err := cmd.CommandInfo() if err != nil { - 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 for command "+name, "error", err) - } - flgs, err := cmd.Flags() - if err != nil { - b.logger.Error("failed to get flags for command "+name, "error", err) + b.logger.Error("failed to get command info for command "+name, "error", err) } err = RegisterSubcommands(cmd, result) if err != nil { @@ -118,9 +110,9 @@ func (b *Basis) Init() (result *vagrant_server.Job_InitResult, err error) { result.Commands, &vagrant_server.Job_Command{ Name: name, - Synopsis: syn, - Help: hlp, - Flags: FlagsToProtoMapper(flgs), + Synopsis: cmdInfo.Synopsis, + Help: cmdInfo.Help, + Flags: FlagsToProtoMapper(cmdInfo.Flags), }, ) } @@ -132,17 +124,17 @@ func RegisterSubcommands(cmd sdkcore.Command, result *vagrant_server.Job_InitRes subcmds, err := cmd.Subcommands() if len(subcmds) > 0 { for _, scmd := range subcmds { - scmdName, _ := scmd.Name() - scmdHelp, _ := scmd.Help() - scmdSyn, _ := scmd.Synopsis() - scmdFlags, _ := scmd.Flags() + scmdInfo, err := scmd.CommandInfo() + if err != nil { + return err + } result.Commands = append( result.Commands, &vagrant_server.Job_Command{ - Name: strings.Join(scmdName, " "), - Synopsis: scmdSyn, - Help: scmdHelp, - Flags: FlagsToProtoMapper(scmdFlags), + Name: strings.Join(scmdInfo.Name, " "), + Synopsis: scmdInfo.Synopsis, + Help: scmdInfo.Help, + Flags: FlagsToProtoMapper(scmdInfo.Flags), }, ) err = RegisterSubcommands(scmd, result) diff --git a/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb.rb b/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb.rb index d267ce6f3..de14e1be3 100644 --- a/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb.rb +++ b/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb.rb @@ -326,14 +326,10 @@ Google::Protobuf::DescriptorPool.generated_pool.build do value :STRING, 0 value :BOOL, 2 end - add_message "hashicorp.vagrant.sdk.Command.HelpResp" do + add_message "hashicorp.vagrant.sdk.Command.CommandInfoResp" do optional :help, :string, 1 - end - add_message "hashicorp.vagrant.sdk.Command.SynopsisResp" do - optional :synopsis, :string, 1 - end - add_message "hashicorp.vagrant.sdk.Command.FlagsResp" do - repeated :flags, :message, 1, "hashicorp.vagrant.sdk.Command.Flag" + optional :synopsis, :string, 2 + repeated :flags, :message, 3, "hashicorp.vagrant.sdk.Command.Flag" end add_message "hashicorp.vagrant.sdk.Command.ExecuteResp" do optional :exit_code, :int64, 1 @@ -610,9 +606,7 @@ module Hashicorp Command = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Command").msgclass Command::Flag = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Command.Flag").msgclass Command::Flag::Type = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Command.Flag.Type").enummodule - Command::HelpResp = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Command.HelpResp").msgclass - Command::SynopsisResp = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Command.SynopsisResp").msgclass - Command::FlagsResp = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Command.FlagsResp").msgclass + Command::CommandInfoResp = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Command.CommandInfoResp").msgclass Command::ExecuteResp = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Command.ExecuteResp").msgclass Command::SubcommandResp = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Command.SubcommandResp").msgclass Command::Arguments = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Command.Arguments").msgclass diff --git a/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_services_pb.rb b/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_services_pb.rb index 02b9d465b..c2efbb06b 100644 --- a/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_services_pb.rb +++ b/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_services_pb.rb @@ -141,16 +141,12 @@ module Hashicorp rpc :ConfigStruct, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::Config::StructResp rpc :Configure, ::Hashicorp::Vagrant::Sdk::Config::ConfigureRequest, ::Google::Protobuf::Empty rpc :Documentation, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::Config::Documentation - rpc :SynopsisSpec, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::FuncSpec - rpc :Synopsis, ::Hashicorp::Vagrant::Sdk::FuncSpec::Args, ::Hashicorp::Vagrant::Sdk::Command::SynopsisResp - rpc :HelpSpec, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::FuncSpec - rpc :Help, ::Hashicorp::Vagrant::Sdk::FuncSpec::Args, ::Hashicorp::Vagrant::Sdk::Command::HelpResp - rpc :FlagsSpec, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::FuncSpec - rpc :Flags, ::Hashicorp::Vagrant::Sdk::FuncSpec::Args, ::Hashicorp::Vagrant::Sdk::Command::FlagsResp rpc :ExecuteSpec, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::FuncSpec rpc :Execute, ::Hashicorp::Vagrant::Sdk::FuncSpec::Args, ::Hashicorp::Vagrant::Sdk::Command::ExecuteResp rpc :SubcommandSpec, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::FuncSpec rpc :Subcommands, ::Hashicorp::Vagrant::Sdk::FuncSpec::Args, ::Hashicorp::Vagrant::Sdk::Command::SubcommandResp + rpc :CommandInfoSpec, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::FuncSpec + rpc :CommandInfo, ::Hashicorp::Vagrant::Sdk::FuncSpec::Args, ::Hashicorp::Vagrant::Sdk::Command::CommandInfoResp end Stub = Service.rpc_stub_class diff --git a/plugins/commands/serve/service/command_service.rb b/plugins/commands/serve/service/command_service.rb index de05ab4d7..e53376011 100644 --- a/plugins/commands/serve/service/command_service.rb +++ b/plugins/commands/serve/service/command_service.rb @@ -6,66 +6,50 @@ module VagrantPlugins class CommandService < SDK::CommandService::Service prepend VagrantPlugins::CommandServe::Service::ExceptionLogger - LOGGER = Log4r::Logger.new("vagrant::plugin::command::service::command") - - [:help, :synopsis, :execute, :flags, :subcommands].each do |method| + [:execute, :subcommands, :command_info].each do |method| VagrantPlugins::CommandServe::Service::ExceptionLogger.log_exception method end - def help_spec(*args) + def command_info_spec(*args) SDK::FuncSpec.new end - def help(req, ctx) - ServiceInfo.with_info(ctx) do |info| - LOGGER.info("Getting help for #{info.plugin_name} #{info.command}") - options = command_options_for(info.plugin_name, info.command) - SDK::Command::HelpResp.new( - help: options.help - ) - end - end - - def synopsis_spec(*args) - return SDK::FuncSpec.new - end - - def synopsis(req, ctx) + def command_info(req, ctx) ServiceInfo.with_info(ctx) do |info| plugin_name = info.plugin_name + + options = command_options_for(plugin_name, info.command) + + if options.nil? + hlp_msg = "" + flags = [] + else + hlp_msg = options.help + # 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 + ) + } + end + + 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 klass = plugin.call - SDK::Command::SynopsisResp.new( - synopsis: klass.synopsis - ) - end - end - - def flags_spec(*args) - SDK::FuncSpec.new - end - - def flags(req, ctx) - ServiceInfo.with_info(ctx) do |info| - options = command_options_for(info.plugin_name, info.command) - # 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 - ) - } - SDK::Command::FlagsResp.new( - flags: flags + SDK::Command::CommandInfoResp.new( + help: hlp_msg, + flags: flags, + synopsis: klass.synopsis, ) end end