policy-reader/modules/contract/list_chain_names.go
Denis Kirillov ee7a08ef1f Support contract method invocation
Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
2024-08-28 14:57:04 +03:00

68 lines
1.6 KiB
Go

package contract
import (
"encoding/base64"
"git.frostfs.info/TrueCloudLab/frostfs-contract/commonclient"
"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 listChainNamesCmd = &cobra.Command{
Use: "list-chain-names",
RunE: runListChainNamesCmd,
}
const (
nameFlag = "name"
)
func initListChainNamesCmd() {
listChainNamesCmd.Flags().String(typeFlag, "namespace", "Target type to list (namespace/container/group/user/iam)")
listChainNamesCmd.Flags().String(nameFlag, "", "Target name to list")
_ = listChainNamesCmd.MarkFlagRequired(nameFlag)
}
func runListChainNamesCmd(cmd *cobra.Command, _ []string) error {
typ := viper.GetString(typeFlag)
entity, err := parseTargetType(typ)
if err != nil {
return err
}
entityName := viper.GetString(nameFlag)
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, "listChainNames", entity, entityName)
if err != nil {
return err
}
cmd.Printf("%s target chain names: %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
}