-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* example plugin * plugins * simplify * simplify * use `*cobra.Command` instead of a func
- Loading branch information
1 parent
e5f27ca
commit 556d8c1
Showing
4 changed files
with
174 additions
and
0 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,4 @@ | ||
EXPORT_LOC=$HOME/.spawn/plugins | ||
mkdir -p $EXPORT_LOC | ||
|
||
go build -buildmode=plugin -o $EXPORT_LOC/example.so plugins/example/example-plugin.go |
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,61 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"path" | ||
|
||
"github.com/spf13/cobra" | ||
plugins "gitub.com/strangelove-ventures/spawn/plugins" | ||
) | ||
|
||
// Make the plugin public | ||
var Plugin SpawnMainExamplePlugin | ||
|
||
var _ plugins.SpawnPlugin = &SpawnMainExamplePlugin{} | ||
|
||
const ( | ||
cmdName = "example" | ||
) | ||
|
||
type SpawnMainExamplePlugin struct { | ||
Impl plugins.SpawnPluginBase | ||
} | ||
|
||
// Cmd implements plugins.SpawnPlugin. | ||
func (e *SpawnMainExamplePlugin) Cmd() *cobra.Command { | ||
rootCmd := &cobra.Command{ | ||
Use: cmdName, | ||
Short: cmdName + " plugin command", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if err := cmd.Help(); err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
} | ||
|
||
rootCmd.AddCommand(&cobra.Command{ | ||
Use: "touch-file [name]", | ||
Short: "An example plugin sub command", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fileName := args[0] | ||
|
||
filePath := path.Join(cwd, fileName) | ||
file, err := os.Create(filePath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer file.Close() | ||
|
||
cmd.Printf("Created file: %s\n", filePath) | ||
}, | ||
}) | ||
|
||
return rootCmd | ||
} |
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,25 @@ | ||
package plugins | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
// SpawnPlugin is the interface that we're exposing as a plugin. | ||
type SpawnPlugin interface { | ||
Cmd() *cobra.Command | ||
} | ||
|
||
var _ SpawnPlugin = &SpawnPluginBase{} | ||
|
||
type SpawnPluginBase struct { | ||
cmd *cobra.Command | ||
} | ||
|
||
func NewSpawnPluginBase(cmd *cobra.Command) *SpawnPluginBase { | ||
return &SpawnPluginBase{ | ||
cmd: cmd, | ||
} | ||
} | ||
|
||
// Cmd implements SpawnPlugin. | ||
func (s *SpawnPluginBase) Cmd() *cobra.Command { | ||
return s.cmd | ||
} |