From 42287b3ad4e6ed91591a0384060e1a1bf3fd26bd Mon Sep 17 00:00:00 2001 From: Eddie Knight Date: Thu, 30 Jan 2025 22:13:45 -0600 Subject: [PATCH] feat: added handling for all plugin command functionality Signed-off-by: Eddie Knight --- command/{command.go => base.go} | 0 command/plugin.go | 99 +++++++++++++++++++++++++++++++++ pluginkit/vessel.go | 14 +++++ 3 files changed, 113 insertions(+) rename command/{command.go => base.go} (100%) create mode 100644 command/plugin.go diff --git a/command/command.go b/command/base.go similarity index 100% rename from command/command.go rename to command/base.go diff --git a/command/plugin.go b/command/plugin.go new file mode 100644 index 0000000..5742397 --- /dev/null +++ b/command/plugin.go @@ -0,0 +1,99 @@ +package command + +import ( + "fmt" + "log" + "os" + "text/tabwriter" + + "github.com/privateerproj/privateer-sdk/config" + "github.com/privateerproj/privateer-sdk/pluginkit" + "github.com/privateerproj/privateer-sdk/shared" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +type Plugin struct{} + +var ActiveVessel pluginkit.Vessel + +// Start will be called by Privateer via gRPC +func (p *Plugin) Start() (err error) { + err = ActiveVessel.Mobilize() + return +} + +func NewPluginCommands( + pluginName, buildVersion, buildGitCommitHash, buildTime string, + armory *pluginkit.Armory, + initializer func(*config.Config) error, + requiredVars []string) *cobra.Command { + + ActiveVessel = pluginkit.NewVessel( + pluginName, + armory, + initializer, + requiredVars, + ) + + runCmd := runCommand(pluginName) + + runCmd.AddCommand(debugCommand()) + + runCmd.AddCommand( + versionCommand(buildVersion, buildGitCommitHash, buildTime)) + + SetBase(runCmd) + return runCmd +} + +func runCommand(pluginName string) *cobra.Command { + return &cobra.Command{ + Use: pluginName, + Short: fmt.Sprintf("Test suite for %s.", pluginName), + PersistentPreRun: func(cmd *cobra.Command, args []string) { + ReadConfig() + }, + Run: func(cmd *cobra.Command, args []string) { + // Serve plugin + plugin := &Plugin{} + serveOpts := &shared.ServeOpts{ + Plugin: plugin, + } + + shared.Serve(pluginName, serveOpts) + }, + } +} + +func debugCommand() *cobra.Command { + return &cobra.Command{ + Use: "debug", + Short: "Run the Plugin in debug mode", + Run: func(cmd *cobra.Command, args []string) { + err := ActiveVessel.Mobilize() + if err != nil { + log.Fatal(err) + } + }, + } +} + +func versionCommand( + buildVersion, buildGitCommitHash, buildTime string) *cobra.Command { + return &cobra.Command{ + Use: "version", + Short: "Display version details.", + Run: func(cmd *cobra.Command, args []string) { + writer := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0) + if viper.GetBool("verbose") { + fmt.Fprintf(writer, "Version:\t%s\n", buildVersion) + fmt.Fprintf(writer, "Commit:\t%s\n", buildGitCommitHash) + fmt.Fprintf(writer, "Build Time:\t%s\n", buildTime) + writer.Flush() + } else { + fmt.Println(buildVersion) + } + }, + } +} diff --git a/pluginkit/vessel.go b/pluginkit/vessel.go index 13c79d3..90b15d0 100644 --- a/pluginkit/vessel.go +++ b/pluginkit/vessel.go @@ -21,6 +21,20 @@ type Vessel struct { executedTestSets *[]string } +func NewVessel( + name string, + armory *Armory, + initializer func(*config.Config) error, + requiredVars []string) Vessel { + + return Vessel{ + PluginName: name, + Armory: armory, + Initializer: initializer, + RequiredVars: requiredVars, + } +} + // StockArmory sets up the armory for the vessel to use func (v *Vessel) StockArmory() error { if v.Armory == nil {