Plugin updates

This commit is contained in:
Lea Anthony
2024-03-26 20:56:58 +11:00
parent 675f502802
commit 85b1f909a7
10 changed files with 177 additions and 145 deletions
+12 -4
View File
@@ -130,9 +130,12 @@ func New(appOptions Options) *App {
}
result.plugins = NewPluginManager(appOptions.Plugins, srv)
err = result.plugins.Init()
if err != nil {
globalApplication.fatal("Fatal error in plugins initialisation: " + err.Error())
errors := result.plugins.Init()
if len(errors) > 0 {
for _, err := range errors {
globalApplication.error("Error initialising plugin: " + err.Error())
}
globalApplication.fatal("Fatal error in plugins initialisation")
}
err = result.bindings.AddPlugins(appOptions.Plugins)
@@ -541,7 +544,12 @@ func (a *App) Run() error {
return err
}
a.plugins.Shutdown()
errors := a.plugins.Shutdown()
if len(errors) > 0 {
for _, err := range errors {
a.error("Error shutting down plugin: " + err.Error())
}
}
return nil
}
+19 -8
View File
@@ -1,13 +1,17 @@
package application
import (
"github.com/pkg/errors"
"github.com/wailsapp/wails/v3/internal/assetserver"
)
type PluginAPI interface {
}
type Plugin interface {
Name() string
Init() error
Shutdown()
Init(api PluginAPI) error
Shutdown() error
CallableByJS() []string
InjectJS() string
}
@@ -26,13 +30,14 @@ func NewPluginManager(plugins map[string]Plugin, assetServer *assetserver.AssetS
return result
}
func (p *PluginManager) Init() error {
func (p *PluginManager) Init() []error {
api := newPluginAPI()
for _, plugin := range p.plugins {
err := plugin.Init()
err := plugin.Init(api)
if err != nil {
globalApplication.error("Plugin failed to initialise:", "plugin", plugin.Name(), "error", err.Error())
p.Shutdown()
return err
return p.Shutdown()
}
p.initialisedPlugins = append(p.initialisedPlugins, plugin)
injectJS := plugin.InjectJS()
@@ -44,9 +49,15 @@ func (p *PluginManager) Init() error {
return nil
}
func (p *PluginManager) Shutdown() {
func (p *PluginManager) Shutdown() []error {
var errs []error
for _, plugin := range p.initialisedPlugins {
plugin.Shutdown()
err := plugin.Shutdown()
globalApplication.debug("Plugin shutdown: " + plugin.Name())
if err != nil {
err = errors.Wrap(err, "Plugin failed to shutdown: "+plugin.Name())
errs = append(errs, err)
}
}
return errs
}
+7
View File
@@ -0,0 +1,7 @@
package application
type pluginAPI struct{}
func newPluginAPI() *pluginAPI {
return &pluginAPI{}
}
+4 -2
View File
@@ -2,6 +2,7 @@ package kvstore
import (
"encoding/json"
"github.com/pkg/errors"
"github.com/wailsapp/wails/v3/pkg/application"
"io"
"os"
@@ -35,13 +36,14 @@ func NewPlugin(config *Config) *KeyValueStore {
}
// Shutdown will save the store to disk if there are unsaved changes.
func (kvs *KeyValueStore) Shutdown() {
func (kvs *KeyValueStore) Shutdown() error {
if kvs.unsaved {
err := kvs.Save()
if err != nil {
application.Get().Logger.Error("Error saving store: " + err.Error())
return errors.Wrap(err, "Error saving store")
}
}
return nil
}
// Name returns the name of the plugin.
+1 -1
View File
@@ -49,7 +49,7 @@ func NewPlugin() *Plugin {
// Shutdown is called when the app is shutting down
// You can use this to clean up any resources you have allocated
func (p *Plugin) Shutdown() {}
func (p *Plugin) Shutdown() error { return nil }
// Name returns the name of the plugin.
// You should use the go module format e.g. github.com/myuser/myplugin
+6 -4
View File
@@ -26,6 +26,7 @@ type Plugin struct {
config Config
server *http.Server
router *pat.Router
api application.PluginAPI
}
type Config struct {
@@ -69,18 +70,19 @@ func NewPlugin(config Config) *Plugin {
return result
}
func (p *Plugin) Shutdown() {
func (p *Plugin) Shutdown() error {
if p.server != nil {
p.server.Close()
return p.server.Close()
}
return nil
}
func (p *Plugin) Name() string {
return "github.com/wailsapp/wails/v3/plugins/oauth"
}
func (p *Plugin) Init() error {
func (p *Plugin) Init(api application.PluginAPI) error {
p.api = api
store := sessions.NewCookieStore([]byte(p.config.SessionSecret))
store.MaxAge(p.config.MaxAge)
store.Options.Path = "/"
+121 -120
View File
File diff suppressed because it is too large Load Diff
+3 -2
View File
@@ -45,10 +45,11 @@ func NewPlugin(config *Config) *Plugin {
// Shutdown is called when the app is shutting down
// You can use this to clean up any resources you have allocated
func (p *Plugin) Shutdown() {
func (p *Plugin) Shutdown() error {
if p.conn != nil {
p.conn.Close()
return p.conn.Close()
}
return nil
}
// Name returns the name of the plugin.
+1 -1
View File
@@ -19,7 +19,7 @@ func NewPlugin(options Config) *Plugin {
// Shutdown is called when the app is shutting down
// You can use this to clean up any resources you have allocated
func (p *Plugin) Shutdown() {}
func (p *Plugin) Shutdown() error { return nil }
// Name returns the name of the plugin.
// You should use the go module format e.g. github.com/myuser/myplugin
+3 -3
View File
@@ -55,7 +55,7 @@ func (p *Plugin) IsStartAtLogin() (bool, error) {
return result, nil
}
func (p Plugin) autostartFile() string {
func (p *Plugin) autostartFile() string {
homedir, _ := os.UserHomeDir()
exe, _ := os.Executable()
name := filepath.Base(exe)
@@ -63,7 +63,7 @@ func (p Plugin) autostartFile() string {
return strings.Join([]string{homedir, ".config", "autostart", autostartFile}, "/")
}
func (p Plugin) createAutoStartFile(filename string, enabled bool) {
func (p *Plugin) createAutoStartFile(filename string, enabled bool) {
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0600)
defer file.Close()
@@ -73,7 +73,7 @@ func (p Plugin) createAutoStartFile(filename string, enabled bool) {
}
exe, _ := os.Executable()
input := startTpl{
Name: filepath.Base(exe) ,
Name: filepath.Base(exe),
Cmd: exe,
Enabled: enabled,
}