Skip to content

mefellows/plugo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Plugo

Simple Plugin abstraction with built-in configuration capability. Plugo provides a simple API to register and lookup plugins, and then inject configuration into them from a YAML/JSON configuration file.

wercker status

Creating a Plugin

First, create your plugin struct:

thing.go:

type Thing struct {
	Name string
	Age  int
}

// Register with plugo with name 'fireThing'
func init() {
	plugo.PluginFactories.Register(func() (interface{}, error) {
		return &Thing{}, nil
	}, "fireThing")
}

func (t *Thing) WhoAmiI() {
	log.Printf("My name is %s and I'm %d", t.Name, t.Age)
}

type PluginThing interface {
	DoSomething()
}
type MockPluginThing struct {
}

func (m MockPluginThing) DoSomething() {
	log.Println("Doing something!")
}

Then in your main class, load the configuration from file (or in this case, hardcoded) and unmarshall the plugins, complete with configuration applied:

main.go:

type MyPluginConfig struct {
	Port   int
	Name   string
	Things []PluginConfig
}

func main() {
	var data = []byte(`
port: 8080
name: Foo
things:
  - name: fireThing
    config:
      name: bar
      age: 21
`)

	// Load Configuration
	var confLoader *plugo.ConfigLoader
	c := &MyPluginConfig{}
	confLoader.Load(data, c)

	// Load all plugins
	things := make([]*Thing, len(c.Things))
	plugins := plugo.LoadPluginsWithConfig(confLoader, c.Things)
	for i, p := range plugins {
		things[i] = p.(*Thing)
		things[i].WhoAmiI()
	}
}

About

Lightweight, configurable plugin system for Go apps

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published