From 96c8e44a339499ba26191e75f439ed647de05eaf Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 23 Apr 2021 15:51:15 -0700 Subject: [PATCH] Update myplugin to proxy commands to subcommands --- builtin/myplugin/command/command.go | 59 +++++++++++++++++++++-------- builtin/myplugin/command/dothing.go | 9 ++--- builtin/myplugin/command/info.go | 12 +++--- builtin/myplugin/main.go | 7 +--- 4 files changed, 55 insertions(+), 32 deletions(-) diff --git a/builtin/myplugin/command/command.go b/builtin/myplugin/command/command.go index 749d6a5be..d3ec8b34c 100644 --- a/builtin/myplugin/command/command.go +++ b/builtin/myplugin/command/command.go @@ -8,6 +8,10 @@ import ( "github.com/hashicorp/vagrant-plugin-sdk/terminal" ) +type Subcommand interface { + CommandInfo() (*component.CommandInfo, error) +} + type CommandConfig struct { } @@ -37,22 +41,41 @@ func (c *Command) Documentation() (*docs.Documentation, error) { } // ExecuteFunc implements component.Command -func (c *Command) ExecuteFunc([]string) interface{} { +func (c *Command) ExecuteFunc(cliArgs []string) interface{} { + if len(cliArgs) < 2 { + return c.Execute + } + n := cliArgs[1] + switch n { + case "info": + return c.ExecuteInfo + case "dothing": + return c.ExecuteDoThing + } + return c.Execute } +func (c *Command) ExecuteInfo(trm terminal.UI, env plugincore.Project) int64 { + return (&Info{Command: c}).Execute(trm, env) +} + +func (c *Command) ExecuteDoThing(trm terminal.UI, flags map[string]interface{}) int64 { + return (&DoThing{Command: c}).Execute(trm, flags) +} + // CommandInfoFunc implements component.Command -func (c *Command) CommandInfoFunc([]string) interface{} { +func (c *Command) CommandInfoFunc() interface{} { return c.CommandInfo } -func (c *Command) CommandInfo() *plugincore.CommandInfo { - return &plugincore.CommandInfo{ +func (c *Command) CommandInfo() *component.CommandInfo { + return &component.CommandInfo{ Name: "myplugin", Help: c.Help(), Synopsis: c.Synopsis(), Flags: c.Flags(), - Subcommands: c.Subcommands(), + Subcommands: c.subcommandsInfo(), } } @@ -73,26 +96,32 @@ func (c *Command) Flags() []*option.Option { return []*option.Option{stringflag} } -func (c *Command) Subcommands() []*plugincore.CommandInfo { - doThingCmd := &DoThing{Command: c} - infoCmd := &Info{Command: c} - return []*plugincore.CommandInfo{ - doThingCmd.CommandInfo(), - infoCmd.CommandInfo(), - } -} - func (c *Command) Execute(trm terminal.UI, flags map[string]interface{}) int64 { trm.Output("You gave me the flag: " + flags["hehe"].(string)) trm.Output(c.Help()) trm.Output("My subcommands are: ") - for _, cmd := range c.Subcommands() { + for _, cmd := range c.subcommandsInfo() { trm.Output(" " + cmd.Name) } return 0 } +func (c *Command) subcommandsInfo() (r []*component.CommandInfo) { + for _, cmd := range c.subcommands() { + v, _ := cmd.CommandInfo() + r = append(r, v) + } + return +} + +func (c *Command) subcommands() map[string]Subcommand { + return map[string]Subcommand{ + "info": &Info{Command: c}, + "dothing": &DoThing{Command: c}, + } +} + var ( _ component.Command = (*Command)(nil) ) diff --git a/builtin/myplugin/command/dothing.go b/builtin/myplugin/command/dothing.go index 9026fb9a4..6bae2df21 100644 --- a/builtin/myplugin/command/dothing.go +++ b/builtin/myplugin/command/dothing.go @@ -3,7 +3,6 @@ package command 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" ) @@ -40,17 +39,17 @@ func (c *DoThing) ExecuteFunc([]string) interface{} { } // CommandInfoFunc implements component.Command -func (c *DoThing) CommandInfoFunc([]string) interface{} { +func (c *DoThing) CommandInfoFunc() interface{} { return c.CommandInfo } -func (c *DoThing) CommandInfo() *plugincore.CommandInfo { - return &plugincore.CommandInfo{ +func (c *DoThing) CommandInfo() (*component.CommandInfo, error) { + return &component.CommandInfo{ Name: "dothing", Help: c.Help(), Synopsis: c.Synopsis(), Flags: c.Flags(), - } + }, nil } func (c *DoThing) Synopsis() string { diff --git a/builtin/myplugin/command/info.go b/builtin/myplugin/command/info.go index f3dc05ee9..0e4491ac7 100644 --- a/builtin/myplugin/command/info.go +++ b/builtin/myplugin/command/info.go @@ -37,22 +37,22 @@ func (c *Info) Documentation() (*docs.Documentation, error) { } // ExecuteFunc implements component.Command -func (c *Info) ExecuteFunc([]string) interface{} { +func (c *Info) ExecuteFunc(cliArgs []string) interface{} { return c.Execute } // CommandInfoFunc implements component.Command -func (c *Info) CommandInfoFunc([]string) interface{} { +func (c *Info) CommandInfoFunc() interface{} { return c.CommandInfo } -func (c *Info) CommandInfo() *plugincore.CommandInfo { - return &plugincore.CommandInfo{ +func (c *Info) CommandInfo() (*component.CommandInfo, error) { + return &component.CommandInfo{ Name: "info", Help: c.Help(), Synopsis: c.Synopsis(), Flags: c.Flags(), - } + }, nil } func (c *Info) Synopsis() string { @@ -91,5 +91,5 @@ func (c *Info) Execute(trm terminal.UI, env plugincore.Project) int64 { } var ( - _ component.Command = (*Command)(nil) + _ component.Command = (*Info)(nil) ) diff --git a/builtin/myplugin/main.go b/builtin/myplugin/main.go index 72487ec3b..e2dcb9af7 100644 --- a/builtin/myplugin/main.go +++ b/builtin/myplugin/main.go @@ -2,7 +2,6 @@ package myplugin import ( sdk "github.com/hashicorp/vagrant-plugin-sdk" - "github.com/hashicorp/vagrant-plugin-sdk/component" "github.com/hashicorp/vagrant/builtin/myplugin/command" ) @@ -12,10 +11,6 @@ import ( var CommandOptions = []sdk.Option{ sdk.WithComponents( &Provider{}, - []component.Command{ - &command.Command{}, - &command.Info{}, - &command.DoThing{}, - }, + &command.Command{}, ), }