package main import ( "fmt" "io/ioutil" "os" "plugin" "git.bullercodeworks.com/brian/helperbot" ) type HelperPlugin struct { p *plugin.Plugin State helperbot.PluginState } func (a *App) LoadPluginsFromDirectory(dir string) error { fmt.Println("Loading Plugins (", dir, ")") files, err := ioutil.ReadDir(dir) if err != nil { fmt.Println("Error loading plugins") fmt.Println(err.Error()) os.Exit(1) } for _, f := range files { p, err := plugin.Open(dir + f.Name()) if err != nil { fmt.Printf("Error loading plugin (%s)\n", f.Name()) fmt.Println(err.Error()) os.Exit(1) } hp, err := NewHelperPlugin(p) if err != nil { fmt.Printf("Error loading plugin (%s)\n", f.Name()) fmt.Println(err.Error()) os.Exit(1) } hp.State.Initialize(a.m) hp.State.Run() a.plugins = append(a.plugins, *hp) fmt.Printf("Plugin loaded: %s\n", f.Name()) } return nil } func NewHelperPlugin(p *plugin.Plugin) (*HelperPlugin, error) { h := &HelperPlugin{ p: p, } // Parse the plugin's state pluginStateSymbol, err := p.Lookup("State") if err != nil { return nil, err } h.State = pluginStateSymbol.(helperbot.PluginState) return h, nil }