106 lines
2.3 KiB
Go
106 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
|
|
"git.algo.com.cn/public/saasapi"
|
|
"git.algo.com.cn/public/saasapi/pkg/saashttp"
|
|
)
|
|
|
|
type scriptDebugParams struct {
|
|
luaScript string
|
|
did string
|
|
os saasapi.OS
|
|
saasHttp *saashttp.SaasClient
|
|
}
|
|
|
|
func RunScriptDebug(args ...string) error {
|
|
fs := flag.NewFlagSet("debug", flag.ExitOnError)
|
|
cfgFile := paramConfig(fs)
|
|
luaFile := paramLua(fs)
|
|
did := paramDid(fs)
|
|
tos := paramOS(fs)
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
return fmt.Errorf("Command line parse error: %w", err)
|
|
}
|
|
|
|
if fs.NArg() > 0 || len(*luaFile) == 0 || len(*did) == 0 {
|
|
fs.PrintDefaults()
|
|
return nil
|
|
}
|
|
|
|
if !(*tos == 1 || *tos == 2 || *tos == 7) {
|
|
fs.PrintDefaults()
|
|
return nil
|
|
}
|
|
|
|
file, err := os.Open(*luaFile)
|
|
if err != nil {
|
|
return fmt.Errorf("Lua file open error: %w", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
body, err := io.ReadAll(file)
|
|
if err != nil {
|
|
return fmt.Errorf("Lua file read error: %w", err)
|
|
}
|
|
|
|
cfg, err := LoadConfigFile(*cfgFile)
|
|
if err != nil {
|
|
return fmt.Errorf("LoadConfigFile error: %w", err)
|
|
}
|
|
|
|
scriptDebugParams := scriptDebugParams{
|
|
luaScript: string(body),
|
|
did: *did,
|
|
os: saasapi.OS(*tos),
|
|
saasHttp: &saashttp.SaasClient{
|
|
Client: &http.Client{},
|
|
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
|
|
Auth: &cfg.Auth,
|
|
},
|
|
}
|
|
|
|
return doScriptDebug(scriptDebugParams)
|
|
}
|
|
|
|
func doScriptDebug(scriptDebugParams scriptDebugParams) error {
|
|
saasReq := &saasapi.SaasReq{
|
|
Cmd: &saasapi.SaasReq_ScriptDebug{
|
|
ScriptDebug: &saasapi.ScriptDebug{
|
|
LuaScript: scriptDebugParams.luaScript,
|
|
ServerDid: scriptDebugParams.did,
|
|
Os: scriptDebugParams.os,
|
|
},
|
|
},
|
|
}
|
|
|
|
res, err := scriptDebugParams.saasHttp.ScriptDebug(saasReq)
|
|
if err != nil {
|
|
return fmt.Errorf("Submit Command error: %w", err)
|
|
}
|
|
|
|
if res.Code != saasapi.ErrorCode_SUCC {
|
|
return fmt.Errorf("Command failed. code:%v, status:%v", res.Code, res.Status)
|
|
}
|
|
|
|
scriptDebugRes := res.GetScriptDebugRes()
|
|
fmt.Println("ERROROUT_OUTPUT:")
|
|
fmt.Print(scriptDebugRes.GetErrorOutput())
|
|
fmt.Println()
|
|
fmt.Println("PRINT_OUTPUT:")
|
|
fmt.Print(scriptDebugRes.GetPrintOutput())
|
|
fmt.Println()
|
|
fmt.Println("DATASPACE_OUTPUT:")
|
|
fmt.Print(scriptDebugRes.GetDataspaceOut())
|
|
fmt.Println()
|
|
fmt.Println("TARGETS_OUTPUT:")
|
|
fmt.Print(scriptDebugRes.GetTargetsOutput())
|
|
return nil
|
|
}
|