110 lines
2.2 KiB
Go
110 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"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 {
|
|
fmt.Fprintln(os.Stderr, "command line parse error", "err", err)
|
|
return 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) == "openid" && len(*appid) == 0 {
|
|
fmt.Fprintln(os.Stderr, "appid must be set when data space is openid")
|
|
return nil
|
|
}
|
|
|
|
cfg, err := LoadConfigFile(*cfgFile)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "load config file error", "err", err)
|
|
return 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 {
|
|
fmt.Fprintln(os.Stderr, "submit read error", "err", err)
|
|
return err
|
|
}
|
|
|
|
if res.GetCode() != saasapi.ErrorCode_SUCC {
|
|
fmt.Fprintln(os.Stderr, protojson.Format(res))
|
|
return err
|
|
} else {
|
|
fmt.Println(protojson.Format(res))
|
|
}
|
|
|
|
return nil
|
|
}
|