yao/plugin/plugin.go
Max c425b74383 Refactor error handling in various components to improve logging and message formatting
- Updated error messages to use fmt.Errorf with %s for better clarity and consistency.
- Enhanced logging statements to utilize formatted strings for improved readability.
- Refactored multiple Load functions across different modules to standardize error handling practices.
2025-09-28 10:24:17 +08:00

66 lines
1.2 KiB
Go

package plugin
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/yaoapp/gou/plugin"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/share"
)
// Load 加载业务插件
func Load(cfg config.Config) error {
root, err := Root(cfg)
if err != nil {
return err
}
// Ignore if the plugins directory does not exist
if _, err := os.Stat(root); os.IsNotExist(err) {
return nil
}
messages := []string{}
err = filepath.Walk(root, func(file string, info fs.FileInfo, err error) error {
if info == nil || info.IsDir() {
return nil
}
if !strings.HasSuffix(file, ".so") && !strings.HasSuffix(file, ".dll") {
return nil
}
_, err = plugin.Load(file, share.ID(root, file))
if err != nil {
messages = append(messages, err.Error())
}
return err
})
if len(messages) > 0 {
return fmt.Errorf("%s", strings.Join(messages, ";\n"))
}
return err
}
// Root return plugin root
func Root(cfg config.Config) (string, error) {
root := filepath.Join(cfg.ExtensionRoot, "plugins")
if cfg.ExtensionRoot == "" {
root = filepath.Join(cfg.Root, "plugins")
}
root, err := filepath.Abs(root)
if err != nil {
return "", err
}
return root, nil
}