-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: build bin on new chain if which not exist * bump poa to patch * `tidy` * attempt * WIP: this runs plugins more like local-ic nested bins * fix for loop ref * quick plugin docs * example plugin cleanup * remove build flags * add docs/PLUGINS.md
- Loading branch information
1 parent
54f1062
commit 2c9b62e
Showing
9 changed files
with
178 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Plugins | ||
|
||
Reference: [Plugins](../plugins/README.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Plugins | ||
|
||
Spawn plugins allow you to build custom functionality on top of Spawn. To accomplish this, you build a cobra CLI binary using the `github.com/rollchains/spawn` import, then build a bianry off of it. Saving this to `$HOME/.spawn/plugins` will allow you to use the binary as a plugin with spawn. Opening the opertunity to closed source plugins and add on features across the stack. | ||
|
||
## Getting Started | ||
|
||
Reference the [example spawn plugin](./example/example-plugin.go) to get started. | ||
|
||
## Running a Plugin | ||
|
||
Note that to use flags, you must use a `--` before flags for the child command context. Flags before the `--` apply to the root of the plugin command. | ||
|
||
- `spawn plugin <name> [arguments] -- [--flags]` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
# sh plugins/example/build.sh | ||
|
||
EXPORT_LOC=$HOME/.spawn/plugins | ||
mkdir -p $EXPORT_LOC | ||
|
||
go build -buildmode=plugin -o $EXPORT_LOC/example.so plugins/example/example-plugin.go | ||
NAME="example-plugin" | ||
|
||
go build -gcflags="all=-N -l" -mod=readonly -trimpath -o $EXPORT_LOC/$NAME plugins/example/$NAME.go | ||
echo "Plugin built and exported to $EXPORT_LOC/$NAME" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,68 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"fmt" | ||
"os" | ||
"path" | ||
"strconv" | ||
|
||
plugins "github.com/rollchains/spawn/plugins" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// Make the plugin public | ||
var Plugin SpawnMainExamplePlugin | ||
|
||
var _ plugins.SpawnPlugin = &SpawnMainExamplePlugin{} | ||
var rootCmd = &cobra.Command{ | ||
Use: "example-plugin", | ||
Short: "Info About the spawn example-plugin", | ||
} | ||
|
||
const ( | ||
cmdName = "example" | ||
) | ||
func main() { | ||
rootCmd.AddCommand(AddCmd(), FlagTestCmd()) | ||
|
||
type SpawnMainExamplePlugin struct { | ||
Impl plugins.SpawnPluginBase | ||
// hides 'completion' command | ||
rootCmd.Root().CompletionOptions.DisableDefaultCmd = true | ||
if err := rootCmd.Execute(); err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
// Cmd implements plugins.SpawnPlugin. | ||
func (e *SpawnMainExamplePlugin) Cmd() *cobra.Command { | ||
rootCmd := &cobra.Command{ | ||
Use: cmdName, | ||
Short: cmdName + " plugin command", | ||
func FlagTestCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "flags-test", | ||
Short: "Test using flags with a plugin", | ||
Example: `spawn plugin example-plugin flags-test -- --value 7`, | ||
Args: cobra.ExactArgs(0), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if err := cmd.Help(); err != nil { | ||
log.Fatal(err) | ||
} | ||
myValue, _ := cmd.Flags().GetInt("value") | ||
fmt.Printf("my-value: %v", myValue) | ||
}, | ||
} | ||
|
||
rootCmd.AddCommand(&cobra.Command{ | ||
Use: "touch-file [name]", | ||
Short: "An example plugin sub command", | ||
Args: cobra.ExactArgs(1), | ||
cmd.Flags().Int("value", 0, "A value you can set") | ||
|
||
return cmd | ||
} | ||
|
||
func AddCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "add", | ||
Short: "A command you can use to perform addition of 2 numbers!", | ||
Example: `spawn plugin example-plugin add 1 2`, | ||
Args: cobra.ExactArgs(2), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cwd, err := os.Getwd() | ||
num1, err := strconv.Atoi(args[0]) | ||
if err != nil { | ||
log.Fatal(err) | ||
fmt.Println("Error parsing the first number") | ||
os.Exit(1) | ||
} | ||
|
||
fileName := args[0] | ||
|
||
filePath := path.Join(cwd, fileName) | ||
file, err := os.Create(filePath) | ||
num2, err := strconv.Atoi(args[1]) | ||
if err != nil { | ||
log.Fatal(err) | ||
fmt.Println("Error parsing the second number") | ||
os.Exit(1) | ||
} | ||
defer file.Close() | ||
|
||
cmd.Printf("Created file: %s\n", filePath) | ||
fmt.Println("add called") | ||
fmt.Println("Performing the addition of the following numbers: ") | ||
fmt.Printf("Num1: %v\n", num1) | ||
fmt.Printf("Num2: %v\n", num2) | ||
fmt.Printf("Addition of those 2 numbers is: %v\n", num1+num2) | ||
}, | ||
}) | ||
|
||
return rootCmd | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package spawn | ||
|
||
import ( | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
type CLIInfo struct { | ||
Description string | ||
Cmds map[string]string | ||
} | ||
|
||
// Parses the CLI commands from a cobra binary to showcase values within the `plugins` subcommand. | ||
func ParseCobraCLICmd(binAbsPath string) (CLIInfo, error) { | ||
output, err := exec.Command(binAbsPath).Output() | ||
if err != nil { | ||
return CLIInfo{}, err | ||
} | ||
|
||
sl := strings.Split(string(output), "\n") | ||
|
||
ci := CLIInfo{ | ||
Cmds: make(map[string]string), | ||
} | ||
isAvailableCmds := false | ||
for idx, line := range sl { | ||
if idx == 0 { | ||
ci.Description = line | ||
if ci.Description == "" { | ||
ci.Description = "No description" | ||
} | ||
} | ||
|
||
// if line stars with `Available Commands:`, next lines are commands | ||
if strings.Contains(line, "Available Commands:") { | ||
isAvailableCmds = true | ||
continue | ||
} | ||
|
||
if isAvailableCmds { | ||
content := []string{} | ||
for _, item := range strings.Split(line, " ") { | ||
if item != "" { | ||
content = append(content, item) | ||
} | ||
} | ||
|
||
if len(content) >= 2 { | ||
ci.Cmds[content[0]] = strings.Join(content[1:], " ") | ||
} | ||
|
||
if line == "Flags:" { | ||
isAvailableCmds = false | ||
continue | ||
} | ||
} | ||
} | ||
return ci, nil | ||
} |