Files
saasapi/cmd/saastool/script_use.go
2026-01-11 17:24:11 +08:00

72 lines
1.5 KiB
Go

package main
import (
"flag"
"fmt"
"net/http"
"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 {
return fmt.Errorf("Command line parse error: %w", err)
}
if fs.NArg() > 0 || len(*luaName) == 0 {
fs.PrintDefaults()
return nil
}
cfg, err := LoadConfigFile(*cfgFile)
if err != nil {
return fmt.Errorf("LoadConfigFile error: %w", 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 {
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.GetScriptUseRes()
fmt.Printf("script use res: %v\n", protojson.Format(scriptRes))
return nil
}