Update myplugin to proxy commands to subcommands
This commit is contained in:
parent
c69710329f
commit
96c8e44a33
@ -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)
|
||||
)
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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)
|
||||
)
|
||||
|
||||
@ -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{},
|
||||
),
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user