增加lua调试功能

This commit is contained in:
algotao
2025-09-02 15:45:56 +08:00
parent ed06c46bde
commit fff023b56d
10 changed files with 200 additions and 10 deletions

3
.gitignore vendored
View File

@@ -1,3 +1,4 @@
vendor/
*.out
build/
build/
*.lua

View File

@@ -2871,8 +2871,9 @@ func (x *BindError) GetReason() string {
type ScriptRunRes struct {
state protoimpl.MessageState `protogen:"open.v1"`
PrintOutput string `protobuf:"bytes,1,opt,name=print_output,json=printOutput,proto3" json:"print_output,omitempty"` // print输出
Error string `protobuf:"bytes,2,opt,name=error,proto3" json:"error,omitempty"` // 错误信息
ErrorOutput string `protobuf:"bytes,2,opt,name=error_output,json=errorOutput,proto3" json:"error_output,omitempty"` // 错误信息
TargetsOutput string `protobuf:"bytes,3,opt,name=targets_output,json=targetsOutput,proto3" json:"targets_output,omitempty"` // 策略输出
DataspaceOut string `protobuf:"bytes,4,opt,name=dataspace_out,json=dataspaceOut,proto3" json:"dataspace_out,omitempty"` // 数据区输出
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
@@ -2914,9 +2915,9 @@ func (x *ScriptRunRes) GetPrintOutput() string {
return ""
}
func (x *ScriptRunRes) GetError() string {
func (x *ScriptRunRes) GetErrorOutput() string {
if x != nil {
return x.Error
return x.ErrorOutput
}
return ""
}
@@ -2928,6 +2929,13 @@ func (x *ScriptRunRes) GetTargetsOutput() string {
return ""
}
func (x *ScriptRunRes) GetDataspaceOut() string {
if x != nil {
return x.DataspaceOut
}
return ""
}
// ScriptUpdateRes 升级脚本返回
type ScriptUpdateRes struct {
state protoimpl.MessageState `protogen:"open.v1"`
@@ -3158,11 +3166,12 @@ const file_cmd_proto_rawDesc = "" +
"\tBindError\x12\x17\n" +
"\abind_id\x18\x01 \x01(\x03R\x06bindId\x12\x1b\n" +
"\tbind_type\x18\x02 \x01(\x05R\bbindType\x12\x16\n" +
"\x06reason\x18\x03 \x01(\tR\x06reason\"n\n" +
"\x06reason\x18\x03 \x01(\tR\x06reason\"\xa0\x01\n" +
"\fScriptRunRes\x12!\n" +
"\fprint_output\x18\x01 \x01(\tR\vprintOutput\x12\x14\n" +
"\x05error\x18\x02 \x01(\tR\x05error\x12%\n" +
"\x0etargets_output\x18\x03 \x01(\tR\rtargetsOutput\"\x11\n" +
"\fprint_output\x18\x01 \x01(\tR\vprintOutput\x12!\n" +
"\ferror_output\x18\x02 \x01(\tR\verrorOutput\x12%\n" +
"\x0etargets_output\x18\x03 \x01(\tR\rtargetsOutput\x12#\n" +
"\rdataspace_out\x18\x04 \x01(\tR\fdataspaceOut\"\x11\n" +
"\x0fScriptUpdateRes*=\n" +
"\bBindType\x12\x13\n" +
"\x0fUnknownBindType\x10\x00\x12\r\n" +

View File

@@ -301,8 +301,9 @@ message BindError {
// ScriptRunRes 运行脚本返回
message ScriptRunRes {
string print_output = 1; // print输出
string error = 2; // 错误信息
string error_output = 2; // 错误信息
string targets_output = 3; // 策略输出
string dataspace_out = 4; // 数据区输出
}
// ScriptUpdateRes 升级脚本返回

View File

@@ -25,6 +25,7 @@ Commands:
task Task commands
target Target commands
bind Bind commands
script Script commands
"help" is the default command.

View File

@@ -36,6 +36,8 @@ func Run(args ...string) error {
return RunTarget(args...)
case "bind":
return RunBind(args...)
case "script":
return RunScript(args...)
default:
err := fmt.Errorf(`unknown command "%v"`+"\n"+`Run 'saastool help' for usage`, name)
fmt.Fprintln(os.Stderr, err.Error())

View File

@@ -105,6 +105,18 @@ func paramIDs(fs *flag.FlagSet) *string {
return fs.String("ids", "", "IDs for delete. Use commas to separate multiple IDs")
}
func paramLua(fs *flag.FlagSet) *string {
return fs.String("lua", "", "LUA file name")
}
func paramDid(fs *flag.FlagSet) *string {
return fs.String("did", "", "device md5 (lower case)")
}
func paramOS(fs *flag.FlagSet) *uint {
return fs.Uint("os", 2, "1=iOS, 2=Android, default=2")
}
// ParseByteSize 解析字节大小字符串为字节数
func ParseByteSize(sizeStr string) (uint64, error) {
sizeStr = strings.TrimSpace(sizeStr)

40
cmd/saastool/script.go Normal file
View File

@@ -0,0 +1,40 @@
package main
import (
"fmt"
"os"
"strings"
)
func RunScript(args ...string) error {
name, args := ParseCommandName(args)
// 从参数中解析出命令
switch name {
case "", "help":
return RunScriptHelp(args...)
case "run":
return RunScriptRun(args...)
default:
err := fmt.Errorf(`unknown command "%s"`+"\n"+`Run 'saastool script help' for usage`, name)
fmt.Fprintln(os.Stderr, err)
return err
}
}
func RunScriptHelp(args ...string) error {
fmt.Println(strings.TrimSpace(scriptUsage))
return nil
}
const scriptUsage = `
Usage: saastoola script COMMAND [OPTIONS]
Commands:
run Run lua script test on server
"help" is the default command.
Use "saastool script COMMAND -help" for more information about a command.
`

112
cmd/saastool/script_run.go Normal file
View File

@@ -0,0 +1,112 @@
package main
import (
"flag"
"fmt"
"io"
"net/http"
"os"
"git.algo.com.cn/public/saasapi"
"git.algo.com.cn/public/saasapi/pkg/saashttp"
)
type scriptRunParams struct {
luaScript string
did string
os saasapi.OS
saasHttp *saashttp.SaasClient
}
func RunScriptRun(args ...string) error {
fs := flag.NewFlagSet("run", flag.ExitOnError)
cfgFile := paramConfig(fs)
luaFile := paramLua(fs)
did := paramDid(fs)
tos := paramOS(fs)
if err := fs.Parse(args); err != nil {
fmt.Fprintln(os.Stderr, "command line parse error", "err", err)
return err
}
if fs.NArg() > 0 || len(*luaFile) == 0 || len(*did) == 0 {
fs.PrintDefaults()
return nil
}
if !(*tos == 1 || *tos == 2) {
fmt.Fprintln(os.Stderr, "OS error")
return nil
}
file, err := os.Open(*luaFile)
if err != nil {
fmt.Fprintln(os.Stderr, "lua file error", "err", err)
return err
}
defer file.Close()
body, err := io.ReadAll(file)
if err != nil {
fmt.Fprintln(os.Stderr, "lua file read error", "err", err)
return err
}
cfg, err := LoadConfigFile(*cfgFile)
if err != nil {
fmt.Fprintln(os.Stderr, "LoadConfigFile error", "err", err)
return err
}
scriptRunParams := scriptRunParams{
luaScript: string(body),
did: *did,
os: saasapi.OS(*tos),
saasHttp: &saashttp.SaasClient{
Client: &http.Client{},
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
Auth: &cfg.Auth,
},
}
return doScriptRun(scriptRunParams)
}
func doScriptRun(scriptRunParams scriptRunParams) error {
saasReq := &saasapi.SaasReq{
Cmd: &saasapi.SaasReq_ScriptRun{
ScriptRun: &saasapi.ScriptRun{
LuaScript: scriptRunParams.luaScript,
ServerDid: scriptRunParams.did,
Os: scriptRunParams.os,
},
},
}
res, err := scriptRunParams.saasHttp.ScriptRun(saasReq)
if err != nil {
fmt.Fprintln(os.Stderr, "run script error", "err", err)
return err
}
if res.Code != saasapi.ErrorCode_SUCC {
err = fmt.Errorf("run script failed. code:%v, status:%v", res.Code, res.Status)
fmt.Fprintln(os.Stderr, err)
return err
}
scriptRunRes := res.GetScriptRunRes()
fmt.Println("ERROROUT_OUTPUT:")
fmt.Print(scriptRunRes.GetErrorOutput())
fmt.Println()
fmt.Println("PRINT_OUTPUT:")
fmt.Print(scriptRunRes.GetPrintOutput())
fmt.Println()
fmt.Println("DATASPACE_OUTPUT:")
fmt.Print(scriptRunRes.GetDataspaceOut())
fmt.Println()
fmt.Println("TARGETS_OUTPUT:")
fmt.Print(scriptRunRes.GetTargetsOutput())
return nil
}

View File

@@ -8,7 +8,6 @@ const (
columnWritePath = "/saas/column_write"
taskCreatePath = "/saas/task/create"
taskListPath = "/saas/task/list"
taskCancelPath = "/saas/task/delete"
taskInfoPath = "/saas/task/info"
taskUploadPath = "/saas/task/upload"
taskDownloadPath = "/saas/task/download"
@@ -17,6 +16,7 @@ const (
targetListPath = "/saas/target/list"
bindSetPath = "/saas/bind/set"
bindDeletePath = "/saas/bind/delete"
scriptRunPath = "/saas/script/run"
)
type ApiUrls struct {
@@ -35,6 +35,7 @@ type ApiUrls struct {
TargetListPath string
BindSetPath string
BindDeletePath string
ScriptRunPath string
}
func InitAPIUrl(c *ApiUrls) *ApiUrls {
@@ -129,6 +130,12 @@ func InitAPIUrl(c *ApiUrls) *ApiUrls {
r.BindDeletePath = bindDeletePath
}
if c.ScriptRunPath != "" {
r.ScriptRunPath = c.ScriptRunPath
} else {
r.ScriptRunPath = scriptRunPath
}
return r
}

View File

@@ -103,6 +103,11 @@ func (c *SaasClient) BindDelete(saasReq *saasapi.SaasReq) (saasRes *saasapi.Saas
return c.post(postUrl, saasReq)
}
func (c *SaasClient) ScriptRun(saasReq *saasapi.SaasReq) (saasRes *saasapi.SaasRes, err error) {
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.ScriptRunPath)
return c.post(postUrl, saasReq)
}
func (c *SaasClient) makeUrl(baseUrl, path string, params ...string) string {
url, err := url.Parse(baseUrl)
if err != nil {