Add test go command for interactive input

This commit is contained in:
sophia 2021-05-05 11:58:00 -05:00 committed by Paul Hinze
parent 552a43744e
commit 2dd286ef4f
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
2 changed files with 89 additions and 2 deletions

View File

@ -51,6 +51,8 @@ func (c *Command) ExecuteFunc(cliArgs []string) interface{} {
return c.ExecuteInfo
case "dothing":
return c.ExecuteDoThing
case "interactive":
return c.ExecuteInteractive
}
return c.Execute
@ -64,6 +66,10 @@ func (c *Command) ExecuteDoThing(trm terminal.UI, flags map[string]interface{})
return (&DoThing{Command: c}).Execute(trm, flags)
}
func (c *Command) ExecuteInteractive(trm terminal.UI, flags map[string]interface{}) int64 {
return (&Interactive{Command: c}).Execute(trm)
}
// CommandInfoFunc implements component.Command
func (c *Command) CommandInfoFunc() interface{} {
return c.CommandInfo
@ -117,8 +123,9 @@ func (c *Command) subcommandsInfo() (r []*component.CommandInfo) {
func (c *Command) subcommands() map[string]Subcommand {
return map[string]Subcommand{
"info": &Info{Command: c},
"dothing": &DoThing{Command: c},
"info": &Info{Command: c},
"dothing": &DoThing{Command: c},
"interactive": &Interactive{Command: c},
}
}

View File

@ -0,0 +1,80 @@
package command
import (
"github.com/DavidGamba/go-getoptions/option"
"github.com/hashicorp/vagrant-plugin-sdk/component"
"github.com/hashicorp/vagrant-plugin-sdk/docs"
"github.com/hashicorp/vagrant-plugin-sdk/terminal"
)
// Info is a Command implementation for myplugin.
// It is a subcommand of myplugin
type Interactive struct {
*Command
}
func (c *Interactive) ConfigSet(v interface{}) error {
return nil
}
func (c *Interactive) CommandFunc() interface{} {
return nil
}
func (c *Interactive) Config() (interface{}, error) {
return &c.config, nil
}
func (c *Interactive) Documentation() (*docs.Documentation, error) {
doc, err := docs.New(docs.FromConfig(&CommandConfig{}))
if err != nil {
return nil, err
}
return doc, nil
}
// ExecuteFunc implements component.Command
func (c *Interactive) ExecuteFunc(cliArgs []string) interface{} {
return c.Execute
}
// CommandInfoFunc implements component.Command
func (c *Interactive) CommandInfoFunc() interface{} {
return c.CommandInfo
}
func (c *Interactive) CommandInfo() (*component.CommandInfo, error) {
return &component.CommandInfo{
Name: "interactive",
Help: c.Help(),
Synopsis: c.Synopsis(),
Flags: c.Flags(),
}, nil
}
func (c *Interactive) Synopsis() string {
return "Test out interactive input"
}
func (c *Interactive) Help() string {
return "Test out interactive input!"
}
func (c *Interactive) Flags() []*option.Option {
return []*option.Option{}
}
func (c *Interactive) Execute(trm terminal.UI) int64 {
output, err := trm.Input(&terminal.Input{Prompt: "What do you have to say"})
if err != nil {
trm.Output("Error getting input")
trm.Output(err.Error())
return 1
}
trm.Output("Did you say " + output)
return 0
}
var (
_ component.Command = (*Interactive)(nil)
)