Add some subcommands to myplugin

This commit is contained in:
sophia 2021-04-13 17:12:02 -05:00 committed by Paul Hinze
parent 0a57d0ba10
commit e4f6c403e1
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
5 changed files with 245 additions and 41 deletions

View File

@ -1,9 +1,6 @@
package commands
import (
"strings"
"time"
"github.com/DavidGamba/go-getoptions/option"
"github.com/hashicorp/vagrant-plugin-sdk/component"
plugincore "github.com/hashicorp/vagrant-plugin-sdk/core"
@ -79,53 +76,26 @@ func (c *Command) CommandInfo() *plugincore.CommandInfo {
}
func (c *Command) Synopsis() string {
return "I don't really do anything"
return "I don't do much, just hanging around"
}
func (c *Command) Help() string {
return "Output some project information!"
return "I'm here for testing, try running some subcommands"
}
func (c *Command) Flags() []*option.Option {
booltest := option.New("booltest", option.BoolType)
booltest.Description = "a test flag for bools"
booltest.DefaultStr = "true"
booltest.Aliases = append(booltest.Aliases, "bt")
stringflag := option.New("stringflag", option.StringType)
stringflag.Description = "a test flag for strings"
stringflag.DefaultStr = "message"
stringflag.Aliases = append(stringflag.Aliases, "sf")
return []*option.Option{booltest, stringflag}
return []*option.Option{}
}
func (c *Command) Subcommands() []string {
return []string{}
func (c *Command) Subcommands() []component.Command {
return []component.Command{
&DoThing{Command: c},
&Info{Command: c},
}
}
func (c *Command) Execute(trm terminal.UI, env plugincore.Project) int64 {
mn, _ := env.MachineNames()
trm.Output("\nMachines in this project")
trm.Output(strings.Join(mn[:], "\n"))
cwd, _ := env.CWD()
datadir, _ := env.DataDir()
vagrantfileName, _ := env.VagrantfileName()
home, _ := env.Home()
localDataPath, _ := env.LocalData()
defaultPrivateKeyPath, _ := env.DefaultPrivateKey()
trm.Output("\nEnvironment information")
trm.Output("Working directory: " + cwd)
trm.Output("Data directory: " + datadir)
trm.Output("Vagrantfile name: " + vagrantfileName)
trm.Output("Home directory: " + home)
trm.Output("Local data directory: " + localDataPath)
trm.Output("Default private key path: " + defaultPrivateKeyPath)
time.Sleep(1 * time.Second)
func (c *Command) Execute(trm terminal.UI) int64 {
trm.Output(c.Help())
return 0
}

View File

@ -0,0 +1,109 @@
package commands
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"
)
// DoThing is a Command implementation for myplugin
// It is a subcommand of myplugin
type DoThing struct {
*Command
}
func (c *DoThing) ConfigSet(v interface{}) error {
return nil
}
func (c *DoThing) CommandFunc() interface{} {
return nil
}
func (c *DoThing) Config() (interface{}, error) {
return &c.config, nil
}
func (c *DoThing) Documentation() (*docs.Documentation, error) {
doc, err := docs.New(docs.FromConfig(&CommandConfig{}))
if err != nil {
return nil, err
}
return doc, nil
}
// SynopsisFunc implements component.Command
func (c *DoThing) SynopsisFunc() interface{} {
return c.Synopsis
}
// HelpFunc implements component.Command
func (c *DoThing) HelpFunc() interface{} {
return c.Help
}
// FlagsFunc implements component.Command
func (c *DoThing) FlagsFunc() interface{} {
return c.Flags
}
// ExecuteFunc implements component.Command
func (c *DoThing) ExecuteFunc() interface{} {
return c.Execute
}
// SubcommandFunc implements component.Command
func (c *DoThing) SubcommandsFunc() interface{} {
return c.Subcommands
}
// CommandInfoFunc implements component.Command
func (c *DoThing) CommandInfoFunc() interface{} {
return c.CommandInfo
}
func (c *DoThing) CommandInfo() *plugincore.CommandInfo {
return &plugincore.CommandInfo{
Name: []string{"myplugin", "donothing"},
Help: c.Help(),
Synopsis: c.Synopsis(),
Flags: c.Flags(),
}
}
func (c *DoThing) Synopsis() string {
return "Really important *stuff*"
}
func (c *DoThing) Help() string {
return "I do really important work"
}
func (c *DoThing) Flags() []*option.Option {
booltest := option.New("booltest", option.BoolType)
booltest.Description = "a test flag for bools"
booltest.DefaultStr = "true"
booltest.Aliases = append(booltest.Aliases, "bt")
stringflag := option.New("stringflag", option.StringType)
stringflag.Description = "a test flag for strings"
stringflag.DefaultStr = "message"
stringflag.Aliases = append(stringflag.Aliases, "sf")
return []*option.Option{booltest, stringflag}
}
func (c *DoThing) Subcommands() []string {
return []string{}
}
func (c *DoThing) Execute(trm terminal.UI) int64 {
trm.Output("Tricked ya! I actually do nothing :P")
return 0
}
var (
_ component.Command = (*DoThing)(nil)
)

View File

@ -0,0 +1,119 @@
package commands
import (
"strings"
"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"
)
// Info is a Command implementation for myplugin.
// It is a subcommand of myplugin
type Info struct {
*Command
}
func (c *Info) ConfigSet(v interface{}) error {
return nil
}
func (c *Info) CommandFunc() interface{} {
return nil
}
func (c *Info) Config() (interface{}, error) {
return &c.config, nil
}
func (c *Info) Documentation() (*docs.Documentation, error) {
doc, err := docs.New(docs.FromConfig(&CommandConfig{}))
if err != nil {
return nil, err
}
return doc, nil
}
// SynopsisFunc implements component.Command
func (c *Info) SynopsisFunc() interface{} {
return c.Synopsis
}
// HelpFunc implements component.Command
func (c *Info) HelpFunc() interface{} {
return c.Help
}
// FlagsFunc implements component.Command
func (c *Info) FlagsFunc() interface{} {
return c.Flags
}
// ExecuteFunc implements component.Command
func (c *Info) ExecuteFunc() interface{} {
return c.Execute
}
// SubcommandFunc implements component.Command
func (c *Info) SubcommandsFunc() interface{} {
return c.Subcommands
}
// CommandInfoFunc implements component.Command
func (c *Info) CommandInfoFunc() interface{} {
return c.CommandInfo
}
func (c *Info) CommandInfo() *plugincore.CommandInfo {
return &plugincore.CommandInfo{
Name: []string{"myplugin", "info"},
Help: c.Help(),
Synopsis: c.Synopsis(),
Flags: c.Flags(),
}
}
func (c *Info) Synopsis() string {
return "Output some project information!"
}
func (c *Info) Help() string {
return "Output some project information!"
}
func (c *Info) Flags() []*option.Option {
return []*option.Option{}
}
func (c *Info) Subcommands() []string {
return []string{}
}
func (c *Info) Execute(trm terminal.UI, env plugincore.Project) int64 {
mn, _ := env.MachineNames()
trm.Output("\nMachines in this project")
trm.Output(strings.Join(mn[:], "\n"))
cwd, _ := env.CWD()
datadir, _ := env.DataDir()
vagrantfileName, _ := env.VagrantfileName()
home, _ := env.Home()
localDataPath, _ := env.LocalData()
defaultPrivateKeyPath, _ := env.DefaultPrivateKey()
trm.Output("\nEnvironment information")
trm.Output("Working directory: " + cwd)
trm.Output("Data directory: " + datadir)
trm.Output("Vagrantfile name: " + vagrantfileName)
trm.Output("Home directory: " + home)
trm.Output("Local data directory: " + localDataPath)
trm.Output("Default private key path: " + defaultPrivateKeyPath)
return 0
}
var (
_ component.Command = (*Command)(nil)
)

View File

@ -9,5 +9,8 @@ import (
// Options are the SDK options to use for instantiation.
var Options = []sdk.Option{
sdk.WithComponents(&Provider{}, &commands.Command{}),
sdk.WithComponents(
&Provider{},
&commands.Command{},
),
}

View File

@ -87,6 +87,9 @@ 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 != "myplugin" {
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)