Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added handling for all plugin command functionality #61

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
99 changes: 99 additions & 0 deletions command/plugin.go
Original file line number Diff line number Diff line change
@@ -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)
}
},
}
}
14 changes: 14 additions & 0 deletions pluginkit/vessel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading