fix javascript log bug

This commit is contained in:
xiaoyaofenfen 2022-04-20 09:49:16 +08:00
parent e8c370cc86
commit 70443455de
2 changed files with 15 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/dop251/goja"
"github.com/tickstep/library-go/logger"
"strings"
)
type (
@ -14,18 +15,20 @@ type (
}
)
func NewJsPlugin() *JsPlugin {
func NewJsPlugin(log *logger.CmdVerbose) *JsPlugin {
return &JsPlugin{
Name: "JsPlugin",
vm: nil,
logger: nil,
logger: log,
}
}
// jsLog 支持js中的console.log方法
func jsLog(call goja.FunctionCall) goja.Value {
str := call.Argument(0)
fmt.Print(str.String())
buf := &strings.Builder{}
fmt.Fprintf(buf, "%+v", str.Export())
logger.Verboseln(buf.String())
return str
}
@ -39,7 +42,7 @@ func (js *JsPlugin) Start() error {
console.Set("log", jsLog)
js.vm.Set("console", console)
// 内置系统函数
// 内置系统函数sys
sysObj := js.vm.NewObject()
sysObj.Set("httpGet", HttpGet)
sysObj.Set("httpPost", HttpPost)

View File

@ -2,6 +2,7 @@ package plugin
import (
"fmt"
"github.com/tickstep/aliyunpan/internal/config"
"github.com/tickstep/library-go/logger"
"io/ioutil"
"os"
@ -13,11 +14,15 @@ import (
type (
PluginManager struct {
PluginPath string
log *logger.CmdVerbose
}
)
func NewPluginManager() *PluginManager {
return &PluginManager{}
return &PluginManager{
PluginPath: "",
log: logger.New("PLUGIN", config.EnvVerbose),
}
}
func (p *PluginManager) SetPluginPath(pluginPath string) error {
@ -31,9 +36,10 @@ func (p *PluginManager) SetPluginPath(pluginPath string) error {
func (p *PluginManager) GetPlugin() (Plugin, error) {
// js plugin folder
// only support js plugin right now
jsPluginPath := path.Clean(p.PluginPath + string(os.PathSeparator) + "js")
if fi, err := os.Stat(jsPluginPath); err == nil && fi.IsDir() {
jsPlugin := NewJsPlugin()
jsPlugin := NewJsPlugin(p.log)
if jsPlugin.Start() != nil {
logger.Verbosef("初始化JS脚本错误\n")
return interface{}(NewIdlePlugin()).(Plugin), nil