Register all subcommands

This commit is contained in:
sophia 2021-04-12 15:28:20 -05:00 committed by Paul Hinze
parent c676768674
commit 06b1caedf8
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
4 changed files with 33 additions and 28 deletions

1
go.sum
View File

@ -974,7 +974,6 @@ github.com/modern-go/reflect2 v0.0.0-20180320133207-05fbef0ca5da/go.mod h1:bx2lN
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=

View File

@ -87,9 +87,6 @@ func (b *Basis) Init() (result *vagrant_server.Job_InitResult, err error) {
f := b.factories[component.CommandType]
result = &vagrant_server.Job_InitResult{}
for _, name := range f.Registered() {
if name != "box" {
continue
}
c, err := componentCreatorMap[component.CommandType].Create(context.Background(), b, name)
if err != nil {
b.logger.Error("failed to start plugin", "name", name, "error", err)
@ -113,24 +110,7 @@ func (b *Basis) Init() (result *vagrant_server.Job_InitResult, err error) {
if err != nil {
b.logger.Error("failed to get flags for command "+name, "error", err)
}
subcmds, err := cmd.Subcommands()
if subcmds != nil {
for _, scmd := range subcmds {
scmdName, _ := scmd.Name()
scmdHelp, _ := scmd.Help()
scmdSyn, _ := scmd.Synopsis()
scmdFlags, _ := scmd.Flags()
result.Commands = append(
result.Commands,
&vagrant_server.Job_Command{
Name: strings.Join(scmdName, " "),
Synopsis: scmdSyn,
Help: scmdHelp,
Flags: FlagsToProtoMapper(scmdFlags),
},
)
}
}
err = RegisterSubcommands(cmd, result)
if err != nil {
b.logger.Error("subcommand error", err)
}
@ -148,6 +128,29 @@ func (b *Basis) Init() (result *vagrant_server.Job_InitResult, err error) {
return
}
func RegisterSubcommands(cmd sdkcore.Command, result *vagrant_server.Job_InitResult) (err error) {
subcmds, err := cmd.Subcommands()
if len(subcmds) > 0 {
for _, scmd := range subcmds {
scmdName, _ := scmd.Name()
scmdHelp, _ := scmd.Help()
scmdSyn, _ := scmd.Synopsis()
scmdFlags, _ := scmd.Flags()
result.Commands = append(
result.Commands,
&vagrant_server.Job_Command{
Name: strings.Join(scmdName, " "),
Synopsis: scmdSyn,
Help: scmdHelp,
Flags: FlagsToProtoMapper(scmdFlags),
},
)
err = RegisterSubcommands(scmd, result)
}
}
return
}
func FlagsToProtoMapper(input []*option.Option) []*vagrant_server.Job_Flag {
output := []*vagrant_server.Job_Flag{}

View File

@ -1,7 +1,6 @@
module VagrantPlugins
module CommandServe
module Service
LOGGER = Log4r::Logger.new("vagrant::plugin::command::service")
# Simple aliases
SDK = Hashicorp::Vagrant::Sdk
@ -67,12 +66,16 @@ module VagrantPlugins
end
def self.with_info(context)
if context.metadata["command"].nil?
cmd_meta = context.metadata["command"]
if cmd_meta.nil?
command = []
else
command = context.metadata["command"].split(" ")
if cmd_meta.is_a?(String)
command = context.metadata["command"].split(" ")
else
command = context.metadata["command"]
end
end
LOGGER.info("command info: #{command}")
info = new(
basis: context.metadata["basis_resource_id"],
project: context.metadata["project_resource_id"],

View File

@ -50,7 +50,7 @@ module VagrantPlugins
def flags(req, ctx)
ServiceInfo.with_info(ctx) do |info|
options = command_options_for(info.plugin_name)
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)
@ -76,7 +76,7 @@ module VagrantPlugins
def subcommands(req, ctx)
ServiceInfo.with_info(ctx) do |info|
cmds = subcommands_for(info.plugin_name)
cmds = subcommands_for(info.plugin_name, info.command)
SDK::Command::SubcommandResp.new(
commands: cmds.nil? ? [] : cmds.keys,
)