104 lines
2.1 KiB
Go
104 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"git.algo.com.cn/public/saasapi"
|
|
"git.algo.com.cn/public/saasapi/pkg/saashttp"
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
)
|
|
|
|
const (
|
|
getIdsMax = 100
|
|
)
|
|
|
|
type readParams struct {
|
|
cfg *Config
|
|
appid string
|
|
ds string
|
|
userids []string
|
|
saasHttp *saashttp.SaasClient
|
|
}
|
|
|
|
func RunRead(args ...string) error {
|
|
fs := flag.NewFlagSet("read", flag.ExitOnError)
|
|
cfgFile := paramConfig(fs)
|
|
appid := paramAppid(fs)
|
|
ds := paramDataSpaceId(fs)
|
|
userids := paramUserids(fs)
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
return fmt.Errorf("Command line parse error: %w", err)
|
|
}
|
|
|
|
// 切割字符串
|
|
idsSlice := strings.Split(*userids, ",")
|
|
|
|
if fs.NArg() > 0 || len(idsSlice) == 0 || (len(idsSlice) == 1 && idsSlice[0] == "") || len(idsSlice) > getIdsMax || len(*ds) == 0 {
|
|
fs.PrintDefaults()
|
|
return nil
|
|
}
|
|
|
|
if strings.ToLower(*ds) == "wuid" && len(*appid) == 0 {
|
|
return fmt.Errorf("Appid must be set when data space is wuid.")
|
|
}
|
|
|
|
cfg, err := LoadConfigFile(*cfgFile)
|
|
if err != nil {
|
|
return fmt.Errorf("LoadConfigFile error: %w", err)
|
|
}
|
|
|
|
readParams := readParams{
|
|
cfg: cfg,
|
|
userids: idsSlice,
|
|
appid: *appid,
|
|
ds: *ds,
|
|
saasHttp: &saashttp.SaasClient{
|
|
Client: &http.Client{},
|
|
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
|
|
Auth: &cfg.Auth,
|
|
},
|
|
}
|
|
|
|
return doRead(readParams)
|
|
}
|
|
|
|
func doRead(readParams readParams) error {
|
|
read := &saasapi.Read{
|
|
DataspaceId: readParams.ds,
|
|
Appid: readParams.appid,
|
|
}
|
|
|
|
saasReq := &saasapi.SaasReq{
|
|
Cmd: &saasapi.SaasReq_Read{
|
|
Read: read,
|
|
},
|
|
}
|
|
|
|
saasReadItems := []*saasapi.ReadItem{}
|
|
for _, userid := range readParams.userids {
|
|
saasReadItems = append(saasReadItems, &saasapi.ReadItem{
|
|
Userid: userid,
|
|
})
|
|
}
|
|
|
|
read.ReadItems = saasReadItems
|
|
|
|
res, err := readParams.saasHttp.Read(saasReq)
|
|
if err != nil {
|
|
return fmt.Errorf("Submit Command error: %w", err)
|
|
}
|
|
|
|
if res.GetCode() != saasapi.ErrorCode_SUCC {
|
|
return fmt.Errorf("Command failed. code:%v, status:%v", res.Code, res.Status)
|
|
}
|
|
|
|
readRes := res.GetReadRes()
|
|
fmt.Printf("read res: %v\n", protojson.Format(readRes))
|
|
|
|
return nil
|
|
}
|