mirror of
https://github.com/tickstep/aliyunpan.git
synced 2025-01-23 14:32:14 +08:00
fix javascript log bug
This commit is contained in:
parent
e8c370cc86
commit
70443455de
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user