From 2dd286ef4f982e3ff6aaaa1701c09188d0b4eb6c Mon Sep 17 00:00:00 2001 From: sophia Date: Wed, 5 May 2021 11:58:00 -0500 Subject: [PATCH] Add test go command for interactive input --- builtin/myplugin/command/command.go | 11 +++- builtin/myplugin/command/interactive.go | 80 +++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 builtin/myplugin/command/interactive.go diff --git a/builtin/myplugin/command/command.go b/builtin/myplugin/command/command.go index d3ec8b34c..4b22bc713 100644 --- a/builtin/myplugin/command/command.go +++ b/builtin/myplugin/command/command.go @@ -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}, } } diff --git a/builtin/myplugin/command/interactive.go b/builtin/myplugin/command/interactive.go new file mode 100644 index 000000000..316b47aad --- /dev/null +++ b/builtin/myplugin/command/interactive.go @@ -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) +)