generated from TrueCloudLab/basic
85 lines
2 KiB
Go
85 lines
2 KiB
Go
package contract
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"errors"
|
|
"math/big"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/commonclient"
|
|
policycontract "git.frostfs.info/TrueCloudLab/frostfs-contract/policy"
|
|
"git.frostfs.info/dkirillov/policy-reader/internal/resolver"
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var listTargetsCmd = &cobra.Command{
|
|
Use: "list-targets",
|
|
RunE: runListTargetsCmd,
|
|
}
|
|
|
|
var errUnknownTargetType = errors.New("unknown target type")
|
|
|
|
const (
|
|
typeFlag = "type"
|
|
)
|
|
|
|
func initListTargetsCmd() {
|
|
listTargetsCmd.Flags().String(typeFlag, "namespace", "Target type to list (namespace/container/group/user/iam)")
|
|
}
|
|
|
|
func runListTargetsCmd(cmd *cobra.Command, _ []string) error {
|
|
typ := viper.GetString(typeFlag)
|
|
entity, err := parseTargetType(typ)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
endpoint := viper.GetString(rpcEndpointFlag)
|
|
|
|
policyHash, err := resolver.ResolveContractHash(endpoint, viper.GetString(policyHashFlag))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
rpcCli, err := rpcclient.New(cmd.Context(), endpoint, rpcclient.Options{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
res, err := commonclient.ReadIteratorItems(invoker.New(rpcCli, nil), 100, policyHash, "listTargets", entity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.Printf("%s targets: %d\n", typ, len(res))
|
|
|
|
for _, re := range res {
|
|
bytes, err := re.TryBytes()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.Printf("%s\t(base64: '%s')\n", string(bytes), base64.StdEncoding.EncodeToString(bytes))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func parseTargetType(typ string) (*big.Int, error) {
|
|
switch typ {
|
|
case "namespace":
|
|
return big.NewInt(int64(policycontract.Namespace)), nil
|
|
case "container":
|
|
return big.NewInt(int64(policycontract.Container)), nil
|
|
case "user":
|
|
return big.NewInt(int64(policycontract.User)), nil
|
|
case "group":
|
|
return big.NewInt(int64(policycontract.Group)), nil
|
|
case "iam":
|
|
return big.NewInt(int64(policycontract.IAM)), nil
|
|
}
|
|
|
|
return nil, errUnknownTargetType
|
|
}
|