From e4f6c403e10d02805b2aa4e64f020f87fb739342 Mon Sep 17 00:00:00 2001 From: sophia Date: Tue, 13 Apr 2021 17:12:02 -0500 Subject: [PATCH] Add some subcommands to myplugin --- builtin/myplugin/commands/command.go | 50 +++-------- builtin/myplugin/commands/dothing.go | 109 ++++++++++++++++++++++++ builtin/myplugin/commands/info.go | 119 +++++++++++++++++++++++++++ builtin/myplugin/main.go | 5 +- internal/core/basis.go | 3 + 5 files changed, 245 insertions(+), 41 deletions(-) create mode 100644 builtin/myplugin/commands/dothing.go create mode 100644 builtin/myplugin/commands/info.go diff --git a/builtin/myplugin/commands/command.go b/builtin/myplugin/commands/command.go index bb5720017..a6b07bb79 100644 --- a/builtin/myplugin/commands/command.go +++ b/builtin/myplugin/commands/command.go @@ -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 } diff --git a/builtin/myplugin/commands/dothing.go b/builtin/myplugin/commands/dothing.go new file mode 100644 index 000000000..ba535f04f --- /dev/null +++ b/builtin/myplugin/commands/dothing.go @@ -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) +) diff --git a/builtin/myplugin/commands/info.go b/builtin/myplugin/commands/info.go new file mode 100644 index 000000000..59bbf6f99 --- /dev/null +++ b/builtin/myplugin/commands/info.go @@ -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) +) diff --git a/builtin/myplugin/main.go b/builtin/myplugin/main.go index 88b4a67ca..abe785b7b 100644 --- a/builtin/myplugin/main.go +++ b/builtin/myplugin/main.go @@ -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{}, + ), } diff --git a/internal/core/basis.go b/internal/core/basis.go index 516f93377..173cbead2 100644 --- a/internal/core/basis.go +++ b/internal/core/basis.go @@ -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)