89 lines
1.9 KiB
Go
89 lines
1.9 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"
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
)
|
|
|
|
type scriptCreateParams struct {
|
|
luaName string
|
|
luaScript string
|
|
saasHttp *saashttp.SaasClient
|
|
}
|
|
|
|
func RunScriptCreate(args ...string) error {
|
|
fs := flag.NewFlagSet("create", flag.ExitOnError)
|
|
cfgFile := paramConfig(fs)
|
|
luaFile := paramLua(fs)
|
|
luaName := paramName(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(*luaName) == 0 {
|
|
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)
|
|
}
|
|
|
|
scriptCreateParams := scriptCreateParams{
|
|
luaName: *luaName,
|
|
luaScript: string(body),
|
|
saasHttp: &saashttp.SaasClient{
|
|
Client: &http.Client{},
|
|
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
|
|
Auth: &cfg.Auth,
|
|
},
|
|
}
|
|
|
|
return doScriptCreate(scriptCreateParams)
|
|
}
|
|
|
|
func doScriptCreate(scriptCreateParams scriptCreateParams) error {
|
|
saasReq := &saasapi.SaasReq{
|
|
Cmd: &saasapi.SaasReq_ScriptCreate{
|
|
ScriptCreate: &saasapi.ScriptCreate{
|
|
LuaName: scriptCreateParams.luaName,
|
|
LuaScript: scriptCreateParams.luaScript,
|
|
},
|
|
},
|
|
}
|
|
|
|
res, err := scriptCreateParams.saasHttp.ScriptCreate(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)
|
|
}
|
|
|
|
scriptRes := res.GetScriptCreateRes()
|
|
|
|
fmt.Printf("script res: %v\n", protojson.Format(scriptRes))
|
|
return nil
|
|
}
|