支持策略创建、删除,脚本列表、创建、删除、获取、使用
This commit is contained in:
94
cmd/saastool/script_create.go
Normal file
94
cmd/saastool/script_create.go
Normal file
@@ -0,0 +1,94 @@
|
||||
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 {
|
||||
fmt.Fprintln(os.Stderr, "command line parse error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if fs.NArg() > 0 || len(*luaFile) == 0 || len(*luaName) == 0 {
|
||||
fs.PrintDefaults()
|
||||
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
|
||||
}
|
||||
|
||||
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 {
|
||||
fmt.Fprintln(os.Stderr, "submit Create Script error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if res.Code != saasapi.ErrorCode_SUCC {
|
||||
fmt.Fprintln(os.Stderr, "script create failed", "code", res.Code, "status", res.Status)
|
||||
return nil
|
||||
}
|
||||
|
||||
scriptRes := res.GetScriptCreateRes()
|
||||
|
||||
fmt.Printf("script res: %v\n", protojson.Format(scriptRes))
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user