支持策略创建、删除,脚本列表、创建、删除、获取、使用
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@ vendor/
|
||||
*.out
|
||||
build/
|
||||
*.lua
|
||||
*.bak
|
||||
|
||||
12
.vscode/settings.json
vendored
Normal file
12
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"go.useLanguageServer": true,
|
||||
"[go]": {
|
||||
"editor.insertSpaces": true,
|
||||
"editor.tabSize": 4,
|
||||
"editor.indentSize": 4
|
||||
},
|
||||
"editor.formatOnSave": true,
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.organizeImports": "explicit"
|
||||
}
|
||||
}
|
||||
99
cmd.proto
99
cmd.proto
@@ -20,12 +20,18 @@ message SaasReq {
|
||||
TaskInfo task_info = 24; // 任务详情
|
||||
|
||||
TargetList target_list = 50; // 列出策略及绑定
|
||||
TargetCreate target_create = 51; // 创建策略
|
||||
TargetDelete target_delete = 52; // 删除策略
|
||||
|
||||
BindSet bind_set = 61; // 设置绑定
|
||||
BindDelete bind_delete = 62; // 解除绑定
|
||||
|
||||
ScriptRun script_run = 90; // 运行脚本
|
||||
ScriptUpdate script_update = 91; // 脚本升级
|
||||
ScriptCreate script_create = 91; // 脚本创建
|
||||
ScriptList script_list = 92; // 列出脚本
|
||||
ScriptDelete script_delete = 93; // 删除脚本
|
||||
ScriptGet script_get = 94; // 获取脚本内容
|
||||
ScriptUse script_use = 95; // 使用脚本
|
||||
|
||||
ExpList exp_list = 100; // 列出实验
|
||||
ExpGet exp_get = 101; // 获取实验报表
|
||||
@@ -159,6 +165,17 @@ message TargetList {
|
||||
bool list_bind = 2; // 是否同时列出绑定信息
|
||||
}
|
||||
|
||||
// TargetCreate 创建策略
|
||||
message TargetCreate {
|
||||
string target_id = 1; // 策略ID
|
||||
string target_description = 2; // 策略描述
|
||||
}
|
||||
|
||||
// TargetDelete 删除策略
|
||||
message TargetDelete {
|
||||
string target_id = 1; // 策略ID
|
||||
}
|
||||
|
||||
// BindSet 设置绑定
|
||||
message BindSet {
|
||||
repeated Bind binds = 2; // 设置绑定内容
|
||||
@@ -178,8 +195,30 @@ message ScriptRun {
|
||||
OS os = 5; // 操作系统
|
||||
}
|
||||
|
||||
// ScriptUpdate 升级脚本
|
||||
message ScriptUpdate {
|
||||
// ScriptCreate 创建脚本
|
||||
message ScriptCreate {
|
||||
string lua_name = 2; // 要上传的脚本名称
|
||||
string lua_script = 1; // 要调试的lua脚本
|
||||
}
|
||||
|
||||
// ScriptList 列出脚本
|
||||
message ScriptList {
|
||||
|
||||
}
|
||||
|
||||
// ScriptDelete 删除脚本
|
||||
message ScriptDelete {
|
||||
string lua_name = 1; // 要删除的脚本名称
|
||||
}
|
||||
|
||||
// ScriptGet 获取脚本
|
||||
message ScriptGet {
|
||||
string lua_name = 1; // 要获取的脚本名称
|
||||
}
|
||||
|
||||
// ScriptUse 使用脚本
|
||||
message ScriptUse {
|
||||
string lua_name = 1; // 要使用的脚本名称
|
||||
}
|
||||
|
||||
// ExpList 列出实验
|
||||
@@ -222,12 +261,18 @@ message SaasRes {
|
||||
Task task_info_res = 24; // 任务详情返回状态
|
||||
|
||||
TargetListRes target_list_res = 50; // 列出策略及绑定返回状态
|
||||
TargetCreateRes target_create_res = 51; // 创建策略返回状态
|
||||
TargetDeleteRes target_delete_res = 52; // 删除策略返回状态
|
||||
|
||||
BindSetRes bind_set_res = 61; // 设置绑定返回状态
|
||||
BindDeleteRes bind_delete_res = 62; // 删除绑定返回状态
|
||||
|
||||
ScriptRunRes script_run_res = 90; // 运行脚本返回
|
||||
ScriptUpdateRes script_update_res = 91; // 升级脚本返回
|
||||
ScriptCreateRes script_create_res = 91; // 创建脚本返回
|
||||
ScriptListRes script_list_res = 92; // 列出脚本返回
|
||||
ScriptDeleteRes script_delete_res = 93; // 删除脚本返回
|
||||
ScriptGetRes script_get_res = 94; // 获取脚本返回
|
||||
ScriptUseRes script_use_res = 95; // 使用脚本返回
|
||||
|
||||
ExpListRes exp_list_res = 100; // 实验列表返回
|
||||
ExpGetRes exp_get_res = 101; // 实验报表返回
|
||||
@@ -281,8 +326,20 @@ message TargetListRes {
|
||||
map<string, Binds> target_list = 1; // 绑定列表
|
||||
}
|
||||
|
||||
// TargetCreateRes 策略创建返回
|
||||
message TargetCreateRes {
|
||||
string target_id = 1; // 策略ID
|
||||
string target_description = 2; // 策略描述
|
||||
}
|
||||
|
||||
// TargetDeleteRes 策略创建返回
|
||||
message TargetDeleteRes {
|
||||
string target_id = 1; // 策略ID
|
||||
string target_description = 2; // 策略描述
|
||||
}
|
||||
|
||||
message Binds {
|
||||
repeated Bind binds = 1;
|
||||
repeated Bind binds = 1; // 绑定列表
|
||||
}
|
||||
|
||||
// Bind 绑定信息
|
||||
@@ -341,8 +398,36 @@ message ScriptRunRes {
|
||||
string dataspace_out = 4; // 数据区输出
|
||||
}
|
||||
|
||||
// ScriptUpdateRes 升级脚本返回
|
||||
message ScriptUpdateRes {
|
||||
message ScriptInfo {
|
||||
string lua_name = 1; // 脚本名称
|
||||
string lua_script = 2; // 脚本内容
|
||||
bool lua_checked = 3; // 脚本校验结果
|
||||
bool lua_used = 4; // 脚本是否被使用
|
||||
}
|
||||
|
||||
// ScriptCreateRes 创建脚本返回
|
||||
message ScriptCreateRes {
|
||||
ScriptInfo script_info = 1; // 脚本信息
|
||||
}
|
||||
|
||||
// ScriptListRes 脚本列表返回
|
||||
message ScriptListRes {
|
||||
repeated ScriptInfo script_info = 1; // 脚本信息
|
||||
}
|
||||
|
||||
// ScriptGetRes 获取脚本返回
|
||||
message ScriptGetRes {
|
||||
ScriptInfo script_info = 1; // 脚本信息
|
||||
}
|
||||
|
||||
// ScriptDeleteRes 删除脚本返回
|
||||
message ScriptDeleteRes {
|
||||
ScriptInfo script_info = 1; // 脚本信息
|
||||
}
|
||||
|
||||
// ScriptUseRes 使用脚本返回
|
||||
message ScriptUseRes {
|
||||
ScriptInfo script_info = 1; // 脚本信息
|
||||
}
|
||||
|
||||
// ExpListRes 实验列表返回
|
||||
|
||||
@@ -109,6 +109,10 @@ func paramLua(fs *flag.FlagSet) *string {
|
||||
return fs.String("lua", "", "LUA file name")
|
||||
}
|
||||
|
||||
func paramName(fs *flag.FlagSet) *string {
|
||||
return fs.String("name", "", "Name")
|
||||
}
|
||||
|
||||
func paramDid(fs *flag.FlagSet) *string {
|
||||
return fs.String("did", "", "device md5 (lower case)")
|
||||
}
|
||||
|
||||
@@ -15,6 +15,16 @@ func RunScript(args ...string) error {
|
||||
return RunScriptHelp(args...)
|
||||
case "run":
|
||||
return RunScriptRun(args...)
|
||||
case "create":
|
||||
return RunScriptCreate(args...)
|
||||
case "list":
|
||||
return RunScriptList(args...)
|
||||
case "delete":
|
||||
return RunScriptDelete(args...)
|
||||
case "get":
|
||||
return RunScriptGet(args...)
|
||||
case "use":
|
||||
return RunScriptUse(args...)
|
||||
default:
|
||||
err := fmt.Errorf(`unknown command "%s"`+"\n"+`Run 'saastool script help' for usage`, name)
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
@@ -29,10 +39,15 @@ func RunScriptHelp(args ...string) error {
|
||||
}
|
||||
|
||||
const scriptUsage = `
|
||||
Usage: saastoola script COMMAND [OPTIONS]
|
||||
Usage: saastool script COMMAND [OPTIONS]
|
||||
|
||||
Commands:
|
||||
run Run lua script test on server
|
||||
create Create lua script on server
|
||||
list List all scripts on server
|
||||
delete Delete a script from server
|
||||
get Get script content from server
|
||||
use Use a script as default
|
||||
|
||||
"help" is the default command.
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
76
cmd/saastool/script_delete.go
Normal file
76
cmd/saastool/script_delete.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"git.algo.com.cn/public/saasapi"
|
||||
"git.algo.com.cn/public/saasapi/pkg/saashttp"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
)
|
||||
|
||||
type scriptDeleteParams struct {
|
||||
luaName string
|
||||
saasHttp *saashttp.SaasClient
|
||||
}
|
||||
|
||||
func RunScriptDelete(args ...string) error {
|
||||
fs := flag.NewFlagSet("delete", flag.ExitOnError)
|
||||
cfgFile := paramConfig(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(*luaName) == 0 {
|
||||
fs.PrintDefaults()
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg, err := LoadConfigFile(*cfgFile)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "LoadConfigFile error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
scriptDeleteParams := scriptDeleteParams{
|
||||
luaName: *luaName,
|
||||
saasHttp: &saashttp.SaasClient{
|
||||
Client: &http.Client{},
|
||||
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
|
||||
Auth: &cfg.Auth,
|
||||
},
|
||||
}
|
||||
|
||||
return doScriptDelete(scriptDeleteParams)
|
||||
}
|
||||
|
||||
func doScriptDelete(scriptDeleteParams scriptDeleteParams) error {
|
||||
saasReq := &saasapi.SaasReq{
|
||||
Cmd: &saasapi.SaasReq_ScriptDelete{
|
||||
ScriptDelete: &saasapi.ScriptDelete{
|
||||
LuaName: scriptDeleteParams.luaName,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
res, err := scriptDeleteParams.saasHttp.ScriptDelete(saasReq)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "submit Delete Script error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if res.Code != saasapi.ErrorCode_SUCC {
|
||||
fmt.Fprintln(os.Stderr, "script delete failed", "code", res.Code, "status", res.Status)
|
||||
return nil
|
||||
}
|
||||
|
||||
scriptRes := res.GetScriptDeleteRes()
|
||||
|
||||
fmt.Printf("script res: %v\n", protojson.Format(scriptRes))
|
||||
return nil
|
||||
}
|
||||
76
cmd/saastool/script_get.go
Normal file
76
cmd/saastool/script_get.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"git.algo.com.cn/public/saasapi"
|
||||
"git.algo.com.cn/public/saasapi/pkg/saashttp"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
)
|
||||
|
||||
type scriptGetParams struct {
|
||||
luaName string
|
||||
saasHttp *saashttp.SaasClient
|
||||
}
|
||||
|
||||
func RunScriptGet(args ...string) error {
|
||||
fs := flag.NewFlagSet("get", flag.ExitOnError)
|
||||
cfgFile := paramConfig(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(*luaName) == 0 {
|
||||
fs.PrintDefaults()
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg, err := LoadConfigFile(*cfgFile)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "LoadConfigFile error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
scriptGetParams := scriptGetParams{
|
||||
luaName: *luaName,
|
||||
saasHttp: &saashttp.SaasClient{
|
||||
Client: &http.Client{},
|
||||
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
|
||||
Auth: &cfg.Auth,
|
||||
},
|
||||
}
|
||||
|
||||
return doScriptGet(scriptGetParams)
|
||||
}
|
||||
|
||||
func doScriptGet(scriptGetParams scriptGetParams) error {
|
||||
saasReq := &saasapi.SaasReq{
|
||||
Cmd: &saasapi.SaasReq_ScriptGet{
|
||||
ScriptGet: &saasapi.ScriptGet{
|
||||
LuaName: scriptGetParams.luaName,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
res, err := scriptGetParams.saasHttp.ScriptGet(saasReq)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "submit Get Script error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if res.Code != saasapi.ErrorCode_SUCC {
|
||||
fmt.Fprintln(os.Stderr, "script get failed", "code", res.Code, "status", res.Status)
|
||||
return nil
|
||||
}
|
||||
|
||||
scriptRes := res.GetScriptGetRes()
|
||||
|
||||
fmt.Printf("script res: %v\n", protojson.Format(scriptRes))
|
||||
return nil
|
||||
}
|
||||
71
cmd/saastool/script_list.go
Normal file
71
cmd/saastool/script_list.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"git.algo.com.cn/public/saasapi"
|
||||
"git.algo.com.cn/public/saasapi/pkg/saashttp"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
)
|
||||
|
||||
type scriptListParams struct {
|
||||
saasHttp *saashttp.SaasClient
|
||||
}
|
||||
|
||||
func RunScriptList(args ...string) error {
|
||||
fs := flag.NewFlagSet("list", flag.ExitOnError)
|
||||
cfgFile := paramConfig(fs)
|
||||
|
||||
if err := fs.Parse(args); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "command line parse error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if fs.NArg() > 0 {
|
||||
fs.PrintDefaults()
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg, err := LoadConfigFile(*cfgFile)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "LoadConfigFile error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
scriptListParams := scriptListParams{
|
||||
saasHttp: &saashttp.SaasClient{
|
||||
Client: &http.Client{},
|
||||
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
|
||||
Auth: &cfg.Auth,
|
||||
},
|
||||
}
|
||||
|
||||
return doScriptList(scriptListParams)
|
||||
}
|
||||
|
||||
func doScriptList(scriptListParams scriptListParams) error {
|
||||
saasReq := &saasapi.SaasReq{
|
||||
Cmd: &saasapi.SaasReq_ScriptList{
|
||||
ScriptList: &saasapi.ScriptList{},
|
||||
},
|
||||
}
|
||||
|
||||
res, err := scriptListParams.saasHttp.ScriptList(saasReq)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "submit List Script error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if res.Code != saasapi.ErrorCode_SUCC {
|
||||
fmt.Fprintln(os.Stderr, "script list failed", "code", res.Code, "status", res.Status)
|
||||
return nil
|
||||
}
|
||||
|
||||
scriptRes := res.GetScriptListRes()
|
||||
|
||||
fmt.Printf("script res: %v\n", protojson.Format(scriptRes))
|
||||
return nil
|
||||
}
|
||||
76
cmd/saastool/script_use.go
Normal file
76
cmd/saastool/script_use.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"git.algo.com.cn/public/saasapi"
|
||||
"git.algo.com.cn/public/saasapi/pkg/saashttp"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
)
|
||||
|
||||
type scriptUseParams struct {
|
||||
luaName string
|
||||
saasHttp *saashttp.SaasClient
|
||||
}
|
||||
|
||||
func RunScriptUse(args ...string) error {
|
||||
fs := flag.NewFlagSet("use", flag.ExitOnError)
|
||||
cfgFile := paramConfig(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(*luaName) == 0 {
|
||||
fs.PrintDefaults()
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg, err := LoadConfigFile(*cfgFile)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "LoadConfigFile error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
scriptUseParams := scriptUseParams{
|
||||
luaName: *luaName,
|
||||
saasHttp: &saashttp.SaasClient{
|
||||
Client: &http.Client{},
|
||||
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
|
||||
Auth: &cfg.Auth,
|
||||
},
|
||||
}
|
||||
|
||||
return doScriptUse(scriptUseParams)
|
||||
}
|
||||
|
||||
func doScriptUse(scriptUseParams scriptUseParams) error {
|
||||
saasReq := &saasapi.SaasReq{
|
||||
Cmd: &saasapi.SaasReq_ScriptUse{
|
||||
ScriptUse: &saasapi.ScriptUse{
|
||||
LuaName: scriptUseParams.luaName,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
res, err := scriptUseParams.saasHttp.ScriptUse(saasReq)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "submit Use Script error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if res.Code != saasapi.ErrorCode_SUCC {
|
||||
fmt.Fprintln(os.Stderr, "script use failed", "code", res.Code, "status", res.Status)
|
||||
return nil
|
||||
}
|
||||
|
||||
scriptRes := res.GetScriptUseRes()
|
||||
|
||||
fmt.Printf("script use res: %v\n", protojson.Format(scriptRes))
|
||||
return nil
|
||||
}
|
||||
@@ -15,6 +15,10 @@ func RunTarget(args ...string) error {
|
||||
return RunTargetHelp(args...)
|
||||
case "list":
|
||||
return RunTargetList(args...)
|
||||
case "create":
|
||||
return RunTargetCreate(args...)
|
||||
case "delete":
|
||||
return RunTargetDelete(args...)
|
||||
default:
|
||||
err := fmt.Errorf(`unknown command "%s"`+"\n"+`Run 'saastool target help' for usage`, name)
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
@@ -33,6 +37,8 @@ Usage: saastoola target COMMAND [OPTIONS]
|
||||
|
||||
Commands:
|
||||
list List targets
|
||||
create Create a new target
|
||||
delete Delete an existing target
|
||||
|
||||
"help" is the default command.
|
||||
|
||||
|
||||
81
cmd/saastool/target_create.go
Normal file
81
cmd/saastool/target_create.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"git.algo.com.cn/public/saasapi"
|
||||
"git.algo.com.cn/public/saasapi/pkg/saashttp"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
)
|
||||
|
||||
type createTargetParams struct {
|
||||
targetId string
|
||||
targetDesc string
|
||||
saasHttp *saashttp.SaasClient
|
||||
}
|
||||
|
||||
func RunTargetCreate(args ...string) error {
|
||||
fs := flag.NewFlagSet("create", flag.ExitOnError)
|
||||
cfgFile := paramConfig(fs)
|
||||
targetId := paramTarget(fs)
|
||||
targetDesc := paramTaskDesc(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(*targetId) == 0 {
|
||||
fs.PrintDefaults()
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg, err := LoadConfigFile(*cfgFile)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "LoadConfigFile error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
createTargetParams := createTargetParams{
|
||||
targetId: *targetId,
|
||||
targetDesc: *targetDesc,
|
||||
saasHttp: &saashttp.SaasClient{
|
||||
Client: &http.Client{},
|
||||
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
|
||||
Auth: &cfg.Auth,
|
||||
},
|
||||
}
|
||||
|
||||
return doTargetCreate(createTargetParams)
|
||||
}
|
||||
|
||||
func doTargetCreate(createTargetParams createTargetParams) error {
|
||||
saasReq := &saasapi.SaasReq{
|
||||
Cmd: &saasapi.SaasReq_TargetCreate{
|
||||
TargetCreate: &saasapi.TargetCreate{
|
||||
TargetId: createTargetParams.targetId,
|
||||
TargetDescription: createTargetParams.targetDesc,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
res, err := createTargetParams.saasHttp.TargetCreate(saasReq)
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "submit Create Target error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if res.Code != saasapi.ErrorCode_SUCC {
|
||||
fmt.Fprintln(os.Stderr, "target create failed", "code", res.Code, "status", res.Status)
|
||||
return nil
|
||||
}
|
||||
|
||||
targetRes := res.GetTargetCreateRes()
|
||||
|
||||
fmt.Printf("target res: %v\n", protojson.Format(targetRes))
|
||||
return nil
|
||||
}
|
||||
76
cmd/saastool/target_delete.go
Normal file
76
cmd/saastool/target_delete.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"git.algo.com.cn/public/saasapi"
|
||||
"git.algo.com.cn/public/saasapi/pkg/saashttp"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
)
|
||||
|
||||
type deleteTargetParams struct {
|
||||
targetId string
|
||||
saasHttp *saashttp.SaasClient
|
||||
}
|
||||
|
||||
func RunTargetDelete(args ...string) error {
|
||||
fs := flag.NewFlagSet("delete", flag.ExitOnError)
|
||||
cfgFile := paramConfig(fs)
|
||||
targetId := paramTarget(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(*targetId) == 0 {
|
||||
fs.PrintDefaults()
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg, err := LoadConfigFile(*cfgFile)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "LoadConfigFile error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
deleteTargetParams := deleteTargetParams{
|
||||
targetId: *targetId,
|
||||
saasHttp: &saashttp.SaasClient{
|
||||
Client: &http.Client{},
|
||||
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
|
||||
Auth: &cfg.Auth,
|
||||
},
|
||||
}
|
||||
|
||||
return doTargetDelete(deleteTargetParams)
|
||||
}
|
||||
|
||||
func doTargetDelete(deleteTargetParams deleteTargetParams) error {
|
||||
saasReq := &saasapi.SaasReq{
|
||||
Cmd: &saasapi.SaasReq_TargetDelete{
|
||||
TargetDelete: &saasapi.TargetDelete{
|
||||
TargetId: deleteTargetParams.targetId,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
res, err := deleteTargetParams.saasHttp.TargetDelete(saasReq)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "submit Target delete error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if res.Code != saasapi.ErrorCode_SUCC {
|
||||
err = fmt.Errorf("target delete failed. code:%v, status:%v", res.Code, res.Status)
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
return err
|
||||
}
|
||||
|
||||
targetDeleteRes := res.GetTargetDeleteRes()
|
||||
fmt.Printf("target res: %v\n", protojson.Format(targetDeleteRes))
|
||||
return nil
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// https://man7.org/linux/man-pages/man2/TIOCSWINSZ.2const.html
|
||||
|
||||
// winSize console窗口大小
|
||||
type winSize struct {
|
||||
wsRow uint16
|
||||
wsCols uint16
|
||||
wsXPixels uint16
|
||||
wxYPixels uint16
|
||||
}
|
||||
|
||||
// getConsoleSize 获取控制台窗口大小
|
||||
func getConsoleSize() (cols, rows int) {
|
||||
var sz winSize
|
||||
_, _, _ = syscall.Syscall(syscall.SYS_IOCTL,
|
||||
os.Stdout.Fd(), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&sz)))
|
||||
return int(sz.wsCols), int(sz.wsRow)
|
||||
}
|
||||
@@ -14,13 +14,25 @@ const (
|
||||
taskDeletePath = "/saas/task/delete"
|
||||
taskRunPath = "/saas/task/run"
|
||||
targetListPath = "/saas/target/list"
|
||||
targetCreatePath = "/saas/target/create"
|
||||
targetDeletePath = "/saas/target/delete"
|
||||
bindSetPath = "/saas/bind/set"
|
||||
bindDeletePath = "/saas/bind/delete"
|
||||
scriptRunPath = "/saas/script/run"
|
||||
scriptCreatePath = "/saas/script/create"
|
||||
scriptListPath = "/saas/script/list"
|
||||
scriptDeletePath = "/saas/script/delete"
|
||||
scriptGetPath = "/saas/script/get"
|
||||
scriptUsePath = "/saas/script/use"
|
||||
expListPath = "/saas/exp/list"
|
||||
expGetPath = "/saas/exp/get"
|
||||
)
|
||||
|
||||
type Auth struct {
|
||||
Account string
|
||||
Token string
|
||||
}
|
||||
|
||||
type ApiUrls struct {
|
||||
BaseUrl string
|
||||
InfoPath string
|
||||
@@ -35,9 +47,16 @@ type ApiUrls struct {
|
||||
TaskUploadPath string
|
||||
TaskDownloadPath string
|
||||
TargetListPath string
|
||||
TargetCreatePath string
|
||||
TargetDeletePath string
|
||||
BindSetPath string
|
||||
BindDeletePath string
|
||||
ScriptRunPath string
|
||||
ScriptCreatePath string
|
||||
ScriptListPath string
|
||||
ScriptDeletePath string
|
||||
ScriptGetPath string
|
||||
ScriptUsePath string
|
||||
ExpListPath string
|
||||
ExpGetPath string
|
||||
}
|
||||
@@ -49,79 +68,76 @@ func InitAPIUrl(c *ApiUrls) *ApiUrls {
|
||||
} else {
|
||||
r.BaseUrl = baseUrl
|
||||
}
|
||||
|
||||
if c.InfoPath != "" {
|
||||
r.InfoPath = c.InfoPath
|
||||
} else {
|
||||
r.InfoPath = infoPath
|
||||
}
|
||||
|
||||
if c.WritePath != "" {
|
||||
r.WritePath = c.WritePath
|
||||
} else {
|
||||
r.WritePath = writePath
|
||||
}
|
||||
|
||||
if c.ReadPath != "" {
|
||||
r.ReadPath = c.ReadPath
|
||||
} else {
|
||||
r.ReadPath = readPath
|
||||
}
|
||||
|
||||
if c.ColumnWritePath != "" {
|
||||
r.ColumnWritePath = c.ColumnWritePath
|
||||
} else {
|
||||
r.ColumnWritePath = columnWritePath
|
||||
}
|
||||
|
||||
if c.TaskCreatePath != "" {
|
||||
r.TaskCreatePath = c.TaskCreatePath
|
||||
} else {
|
||||
r.TaskCreatePath = taskCreatePath
|
||||
}
|
||||
|
||||
if c.TaskListPath != "" {
|
||||
r.TaskListPath = c.TaskListPath
|
||||
} else {
|
||||
r.TaskListPath = taskListPath
|
||||
}
|
||||
|
||||
if c.TaskInfoPath != "" {
|
||||
r.TaskInfoPath = c.TaskInfoPath
|
||||
} else {
|
||||
r.TaskInfoPath = taskInfoPath
|
||||
}
|
||||
|
||||
if c.TaskDeletePath != "" {
|
||||
r.TaskDeletePath = c.TaskDeletePath
|
||||
} else {
|
||||
r.TaskDeletePath = taskDeletePath
|
||||
}
|
||||
|
||||
if c.TaskRunPath != "" {
|
||||
r.TaskRunPath = c.TaskRunPath
|
||||
} else {
|
||||
r.TaskRunPath = taskRunPath
|
||||
}
|
||||
|
||||
if c.TaskUploadPath != "" {
|
||||
r.TaskUploadPath = c.TaskUploadPath
|
||||
} else {
|
||||
r.TaskUploadPath = taskUploadPath
|
||||
}
|
||||
|
||||
if c.TaskDownloadPath != "" {
|
||||
r.TaskDownloadPath = c.TaskDownloadPath
|
||||
} else {
|
||||
r.TaskDownloadPath = taskDownloadPath
|
||||
}
|
||||
|
||||
if c.TargetListPath != "" {
|
||||
r.TargetListPath = c.TargetListPath
|
||||
} else {
|
||||
r.TargetListPath = targetListPath
|
||||
}
|
||||
|
||||
if c.TargetCreatePath != "" {
|
||||
r.TargetCreatePath = c.TargetCreatePath
|
||||
} else {
|
||||
r.TargetCreatePath = targetCreatePath
|
||||
}
|
||||
if c.TargetDeletePath != "" {
|
||||
r.TargetDeletePath = c.TargetDeletePath
|
||||
} else {
|
||||
r.TargetDeletePath = targetDeletePath
|
||||
}
|
||||
if c.BindSetPath != "" {
|
||||
r.BindSetPath = c.BindSetPath
|
||||
} else {
|
||||
@@ -140,6 +156,36 @@ func InitAPIUrl(c *ApiUrls) *ApiUrls {
|
||||
r.ScriptRunPath = scriptRunPath
|
||||
}
|
||||
|
||||
if c.ScriptCreatePath != "" {
|
||||
r.ScriptCreatePath = c.ScriptCreatePath
|
||||
} else {
|
||||
r.ScriptCreatePath = scriptCreatePath
|
||||
}
|
||||
|
||||
if c.ScriptListPath != "" {
|
||||
r.ScriptListPath = c.ScriptListPath
|
||||
} else {
|
||||
r.ScriptListPath = scriptListPath
|
||||
}
|
||||
|
||||
if c.ScriptDeletePath != "" {
|
||||
r.ScriptDeletePath = c.ScriptDeletePath
|
||||
} else {
|
||||
r.ScriptDeletePath = scriptDeletePath
|
||||
}
|
||||
|
||||
if c.ScriptGetPath != "" {
|
||||
r.ScriptGetPath = c.ScriptGetPath
|
||||
} else {
|
||||
r.ScriptGetPath = scriptGetPath
|
||||
}
|
||||
|
||||
if c.ScriptUsePath != "" {
|
||||
r.ScriptUsePath = c.ScriptUsePath
|
||||
} else {
|
||||
r.ScriptUsePath = scriptUsePath
|
||||
}
|
||||
|
||||
if c.ExpListPath != "" {
|
||||
r.ExpListPath = c.ExpListPath
|
||||
} else {
|
||||
@@ -154,8 +200,3 @@ func InitAPIUrl(c *ApiUrls) *ApiUrls {
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
type Auth struct {
|
||||
Account string
|
||||
Token string
|
||||
}
|
||||
|
||||
@@ -93,6 +93,16 @@ func (c *SaasClient) TargetList(saasReq *saasapi.SaasReq) (saasRes *saasapi.Saas
|
||||
return c.post(postUrl, saasReq)
|
||||
}
|
||||
|
||||
func (c *SaasClient) TargetCreate(saasReq *saasapi.SaasReq) (saasRes *saasapi.SaasRes, err error) {
|
||||
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.TargetCreatePath)
|
||||
return c.post(postUrl, saasReq)
|
||||
}
|
||||
|
||||
func (c *SaasClient) TargetDelete(saasReq *saasapi.SaasReq) (saasRes *saasapi.SaasRes, err error) {
|
||||
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.TargetDeletePath)
|
||||
return c.post(postUrl, saasReq)
|
||||
}
|
||||
|
||||
func (c *SaasClient) BindSet(saasReq *saasapi.SaasReq) (saasRes *saasapi.SaasRes, err error) {
|
||||
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.BindSetPath)
|
||||
return c.post(postUrl, saasReq)
|
||||
@@ -356,3 +366,28 @@ func (c *SaasClient) download(url string, file *os.File, offset int64, size int)
|
||||
|
||||
return saasRes, nil
|
||||
}
|
||||
|
||||
func (c *SaasClient) ScriptCreate(saasReq *saasapi.SaasReq) (saasRes *saasapi.SaasRes, err error) {
|
||||
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.ScriptCreatePath)
|
||||
return c.post(postUrl, saasReq)
|
||||
}
|
||||
|
||||
func (c *SaasClient) ScriptList(saasReq *saasapi.SaasReq) (saasRes *saasapi.SaasRes, err error) {
|
||||
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.ScriptListPath)
|
||||
return c.post(postUrl, saasReq)
|
||||
}
|
||||
|
||||
func (c *SaasClient) ScriptDelete(saasReq *saasapi.SaasReq) (saasRes *saasapi.SaasRes, err error) {
|
||||
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.ScriptDeletePath)
|
||||
return c.post(postUrl, saasReq)
|
||||
}
|
||||
|
||||
func (c *SaasClient) ScriptGet(saasReq *saasapi.SaasReq) (saasRes *saasapi.SaasRes, err error) {
|
||||
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.ScriptGetPath)
|
||||
return c.post(postUrl, saasReq)
|
||||
}
|
||||
|
||||
func (c *SaasClient) ScriptUse(saasReq *saasapi.SaasReq) (saasRes *saasapi.SaasRes, err error) {
|
||||
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.ScriptUsePath)
|
||||
return c.post(postUrl, saasReq)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user