• Stars
    star
    747
  • Rank 60,741 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

a toolkit for creating plugins for Go applications

pie GoDoc Codeship Status for natefinch/pie

import "github.com/natefinch/pie"

package pie provides a toolkit for creating plugins for Go applications.

pie

Why is it called pie?

Because if you pronounce API like "a pie", then all this consuming and serving of APIs becomes a lot more palatable. Also, pies are the ultimate pluggable interface - depending on what's inside, you can get dinner, dessert, a snack, or even breakfast. Plus, then I get to say that plugins in Go are as easy as... well, you know.

If you have to explain it to your boss, just say it's an acronym for Plug In Executables. (but it's not, really)

About Pie

Plugins using this toolkit and the applications managing those plugins communicate via RPC over the plugin application's Stdin and Stdout.

Functions in this package with the prefix New are intended to be used by the plugin to set up its end of the communication. Functions in this package with the prefix Start are intended to be used by the main application to set up its end of the communication and start a plugin executable.

This package provides two conceptually different types of plugins, based on which side of the communication is the server and which is the client. Plugins which provide an API server for the main application to call are called Providers. Plugins which consume an API provided by the main application are called Consumers.

The default codec for RPC for this package is Go's gob encoding, however you may provide your own codec, such as JSON-RPC provided by net/rpc/jsonrpc.

There is no requirement that plugins for applications using this toolkit be written in Go. As long as the plugin application can consume or provide an RPC API of the correct codec, it can interoperate with main applications using this process. For example, if your main application uses JSON-RPC, many languages are capable of producing an executable that can provide a JSON-RPC API for your application to use.

Included in this repo are some simple examples of a master process and a plugin process, to see how the library can be used. An example of the standard plugin that provides an API the master process consumes is in the examples/provider directory. master_provider expects plugin_provider to be in the same directory or in your $PATH. You can just go install both of them, and it'll work correctly.

In addition to a regular plugin that provides an API, this package can be used for plugins that consume an API provided by the main process. To see an example of this, look in the examples/consumer folder.

func NewConsumer

func NewConsumer() *rpc.Client

NewConsumer returns an rpc.Client that will consume an API from the host process over this application's Stdin and Stdout using gob encoding.

func NewConsumerCodec

func NewConsumerCodec(f func(io.ReadWriteCloser) rpc.ClientCodec) *rpc.Client

NewConsumerCodec returns an rpc.Client that will consume an API from the host process over this application's Stdin and Stdout using the ClientCodec returned by f.

func StartProvider

func StartProvider(output io.Writer, path string, args ...string) (*rpc.Client, error)

StartProvider start a provider-style plugin application at the given path and args, and returns an RPC client that communicates with the plugin using gob encoding over the plugin's Stdin and Stdout. The writer passed to output will receive output from the plugin's stderr. Closing the RPC client returned from this function will shut down the plugin application.

func StartProviderCodec

func StartProviderCodec(
    f func(io.ReadWriteCloser) rpc.ClientCodec,
    output io.Writer,
    path string,
    args ...string,
) (*rpc.Client, error)

StartProviderCodec starts a provider-style plugin application at the given path and args, and returns an RPC client that communicates with the plugin using the ClientCodec returned by f over the plugin's Stdin and Stdout. The writer passed to output will receive output from the plugin's stderr. Closing the RPC client returned from this function will shut down the plugin application.

type Server

type Server struct {
    // contains filtered or unexported fields
}

Server is a type that represents an RPC server that serves an API over stdin/stdout.

func NewProvider

func NewProvider() Server

NewProvider returns a Server that will serve RPC over this application's Stdin and Stdout. This method is intended to be run by the plugin application.

func StartConsumer

func StartConsumer(output io.Writer, path string, args ...string) (Server, error)

StartConsumer starts a consumer-style plugin application with the given path and args, writing its stderr to output. The plugin consumes an API this application provides. The function returns the Server for this host application, which should be used to register APIs for the plugin to consume.

func (Server) Close

func (s Server) Close() error

Close closes the connection with the client. If the client is a plugin process, the process will be stopped. Further communication using this Server will fail.

func (Server) Register

func (s Server) Register(rcvr interface{}) error

Register publishes in the provider the set of methods of the receiver value that satisfy the following conditions:

- exported method
- two arguments, both of exported type
- the second argument is a pointer
- one return value, of type error

It returns an error if the receiver is not an exported type or has no suitable methods. It also logs the error using package log. The client accesses each method using a string of the form "Type.Method", where Type is the receiver's concrete type.

func (Server) RegisterName

func (s Server) RegisterName(name string, rcvr interface{}) error

RegisterName is like Register but uses the provided name for the type instead of the receiver's concrete type.

func (Server) Serve

func (s Server) Serve()

Serve starts the Server's RPC server, serving via gob encoding. This call will block until the client hangs up.

func (Server) ServeCodec

func (s Server) ServeCodec(f func(io.ReadWriteCloser) rpc.ServerCodec)

ServeCodec starts the Server's RPC server, serving via the encoding returned by f. This call will block until the client hangs up.

More Repositories

1

lumberjack

lumberjack is a log rolling package for Go
Go
4,272
star
2

gorram

It's like go run for any go function
Go
1,038
star
3

npipe

A Windows named pipe implementation written in pure Go.
Go
282
star
4

deputy

deputy is a go package that adds smarts on top of os/exec
Go
230
star
5

atomic

atomic is a go package for atomic file writing
Go
190
star
6

godocgo

An example of good godoc documentation.
Go
154
star
7

gocog

Generate code for any language, with any language.
Go
68
star
8

sh

Package sh makes working with shell commands more shell-like. Inspired by https://github.com/amoffat/sh
Go
52
star
9

npf

code to host a professional site
CSS
52
star
10

graffiti

graffiti is a tool to automatically add struct tags to fields in your go code
Go
38
star
11

claymud

Highly configurable and performant MUD written in Go
Go
28
star
12

keep

Go bindings wrapping the Google Keep API
Go
22
star
13

diceware

an implementation of the diceware passphrase generation algorithm
Go
21
star
14

blogimport

A tool to import from Blogger to Hugo
Go
20
star
15

tree

A statically typed binary tree in Go without casts or reflection
Go
19
star
16

pcgrep

Go
15
star
17

covergen

autogenerate a coverage badge for your README with with one dumb trick
Go
10
star
18

convert

An application to convert and resize images written in Go.
Go
10
star
19

plugman

a plugin manifest manager for go
9
star
20

q

Q is a CLI tool for developers that can do anything.
Go
8
star
21

avl

Go
6
star
22

natefinch.github.io

nate's sweet website
HTML
5
star
23

tod

A Hugo site for my D&D campaign
CSS
4
star
24

filterr

go package that helps filter returned errors to a specific list
Go
4
star
25

wrap

Wrap contains a method for wrapping one Go error with another.
Go
4
star
26

Ashes2Ashes

A modified CircleMUD that ran in the late 90's.
C
3
star
27

cavalier

a command line interface generator for Go
Go
3
star
28

emojis-and-dragons

2
star
29

nolog

just a dumb little tool to run juju tests and filter out all the logging
Go
2
star
30

natefinch

1
star
31

harvard-4-h

CSS
1
star
32

rpg

a personal website about RPGs
CSS
1
star
33

circle2json

converts CircleMUD room definitions to json equivalents
Go
1
star
34

go-quickstart

Go
1
star
35

burningchrome

A website for the in-development Burning Chrome RPG.
CSS
1
star
36

eg

An enhanced error package for Go
Go
1
star
37

tabletop.design

HTML
1
star
38

treesample

Sample project using natefinch/tree
Go
1
star