Files
saasapi/cmd/saastool/script_list.go

72 lines
1.5 KiB
Go

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
}