policy-reader/modules/contract/list_chain_names.go
Denis Kirillov 7465cf0f4e Allow to use config file
Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
2024-08-28 16:19:23 +03:00

88 lines
2.5 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",
Short: "Invoke 'listChainNames' method",
Long: "Invoke 'listChainNames' method in policy contract and print results to stdout",
Example: `policy-reader contract -r http://localhost:40332 list-chain-names
policy-reader contract -r http://localhost:40332 --policy-hash 81c1a41d09e08087a4b679418b12be5d3ab15742 list-chain-names --type container --name 7h7NcXcF6k6b1yidqEHc1jkyXUm1MfUDrrTuHAefhiDe`,
PreRunE: func(cmd *cobra.Command, _ []string) error {
if viper.IsSet(nameFlag) {
return cmd.Flags().Set(nameFlag, viper.GetString(nameFlag))
}
return nil
},
RunE: runListChainNamesCmd,
}
const (
nameFlag = "name"
nameBase64Flag = "name-base64"
)
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.Flags().Bool(nameBase64Flag, false, "Use this flag if you provide name in base64 format")
_ = listChainNamesCmd.MarkFlagRequired(nameFlag)
}
func runListChainNamesCmd(cmd *cobra.Command, _ []string) error {
typ := viper.GetString(typeFlag)
entity, err := parseTargetType(typ)
if err != nil {
return err
}
entityNameStr := viper.GetString(nameFlag)
var entityName []byte
if viper.GetBool(nameBase64Flag) {
if entityName, err = base64.StdEncoding.DecodeString(entityNameStr); err != nil {
return err
}
} else {
entityName = []byte(entityNameStr)
}
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
}