Reuse PrintChains function

Replace non-printable symbols by dot (.) when printing chain id

Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
This commit is contained in:
Denis Kirillov 2024-09-03 16:35:36 +03:00
parent 39f8d20cfa
commit fe6f3471f9
4 changed files with 68 additions and 72 deletions

56
internal/output/chains.go Normal file
View file

@ -0,0 +1,56 @@
package output
import (
"encoding/base64"
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/spf13/cobra"
)
const (
minPrintable = 32
maxPrintable = 127
)
func PrintChains(cmd *cobra.Command, list []stackitem.Item, decodeChain, decodeID bool) error {
for _, item := range list {
bytes, err := item.TryBytes()
if err != nil {
return err
}
if !decodeChain {
cmd.Println(string(bytes))
continue
}
var chain apechain.Chain
if err = chain.DecodeBytes(bytes); err != nil {
cmd.PrintErrf("invalid chain format: %s\n", base64.StdEncoding.EncodeToString(bytes))
continue
}
raw, err := chain.MarshalJSON()
if err != nil {
return err
}
if decodeID {
var printableID string
for _, r := range string(chain.ID) {
if minPrintable <= r && r <= maxPrintable {
printableID += string(r)
} else {
printableID += "."
}
}
cmd.Println(printableID, string(raw))
} else {
cmd.Println(string(raw))
}
}
return nil
}

View file

@ -5,6 +5,7 @@ import (
"git.frostfs.info/TrueCloudLab/frostfs-contract/commonclient" "git.frostfs.info/TrueCloudLab/frostfs-contract/commonclient"
policycontract "git.frostfs.info/TrueCloudLab/frostfs-contract/policy" policycontract "git.frostfs.info/TrueCloudLab/frostfs-contract/policy"
"git.frostfs.info/dkirillov/policy-reader/internal/output"
"git.frostfs.info/dkirillov/policy-reader/internal/resolver" "git.frostfs.info/dkirillov/policy-reader/internal/resolver"
"github.com/nspcc-dev/neo-go/pkg/rpcclient" "github.com/nspcc-dev/neo-go/pkg/rpcclient"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker" "github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
@ -63,11 +64,12 @@ func runListContainerCmd(cmd *cobra.Command, _ []string) error {
return err return err
} }
const decodeChain = true
decodeID := viper.GetBool(decodeIDFlag) decodeID := viper.GetBool(decodeIDFlag)
cmd.Println("cid:", cnrID) cmd.Println("cid:", cnrID)
cmd.Printf("namespace '%s' policies: %d\n", ns, len(res)) cmd.Printf("namespace '%s' policies: %d\n", ns, len(res))
if err = printChains(cmd, res, decodeID); err != nil { if err = output.PrintChains(cmd, res, decodeChain, decodeID); err != nil {
return err return err
} }
@ -77,9 +79,5 @@ func runListContainerCmd(cmd *cobra.Command, _ []string) error {
} }
cmd.Printf("container policies: %d\n", len(res)) cmd.Printf("container policies: %d\n", len(res))
if err = printChains(cmd, res, decodeID); err != nil { return output.PrintChains(cmd, res, decodeChain, decodeID)
return err
}
return nil
} }

View file

@ -1,7 +1,6 @@
package chains package chains
import ( import (
"encoding/base64"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
@ -11,11 +10,11 @@ import (
ffsidclient "git.frostfs.info/TrueCloudLab/frostfs-contract/frostfsid/client" ffsidclient "git.frostfs.info/TrueCloudLab/frostfs-contract/frostfsid/client"
policycontract "git.frostfs.info/TrueCloudLab/frostfs-contract/policy" policycontract "git.frostfs.info/TrueCloudLab/frostfs-contract/policy"
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain" apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
"git.frostfs.info/dkirillov/policy-reader/internal/output"
"git.frostfs.info/dkirillov/policy-reader/internal/resolver" "git.frostfs.info/dkirillov/policy-reader/internal/resolver"
neoflags "github.com/nspcc-dev/neo-go/cli/flags" neoflags "github.com/nspcc-dev/neo-go/cli/flags"
"github.com/nspcc-dev/neo-go/pkg/rpcclient" "github.com/nspcc-dev/neo-go/pkg/rpcclient"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker" "github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -93,10 +92,11 @@ func runListCmd(cmd *cobra.Command, _ []string) error {
return err return err
} }
const decodeChain = true
decodeID := viper.GetBool(decodeIDFlag) decodeID := viper.GetBool(decodeIDFlag)
cmd.Printf("user namespace '%s' policies: %d\n", subj.Namespace, len(res)) cmd.Printf("user namespace '%s' policies: %d\n", subj.Namespace, len(res))
if err = printChains(cmd, res, decodeID); err != nil { if err = output.PrintChains(cmd, res, decodeID, decodeID); err != nil {
return err return err
} }
@ -109,7 +109,7 @@ func runListCmd(cmd *cobra.Command, _ []string) error {
} }
cmd.Printf("user policies: %d\n", len(res)) cmd.Printf("user policies: %d\n", len(res))
if err = printChains(cmd, res, decodeID); err != nil { if err = output.PrintChains(cmd, res, decodeChain, decodeID); err != nil {
return err return err
} }
@ -125,7 +125,7 @@ func runListCmd(cmd *cobra.Command, _ []string) error {
} }
cmd.Printf("user group '%s' (id: %d) policies: %d\n", group.Name, group.ID, len(res)) cmd.Printf("user group '%s' (id: %d) policies: %d\n", group.Name, group.ID, len(res))
if err = printChains(cmd, res, decodeID); err != nil { if err = output.PrintChains(cmd, res, decodeChain, decodeID); err != nil {
return err return err
} }
} }
@ -133,34 +133,6 @@ func runListCmd(cmd *cobra.Command, _ []string) error {
return nil return nil
} }
func printChains(cmd *cobra.Command, list []stackitem.Item, decodeID bool) error {
for _, item := range list {
bytes, err := item.TryBytes()
if err != nil {
return err
}
var chain apechain.Chain
if err = chain.DecodeBytes(bytes); err != nil {
cmd.Printf("invalid chain format: %s\n", base64.StdEncoding.EncodeToString(bytes))
continue
}
raw, err := chain.MarshalJSON()
if err != nil {
return err
}
if decodeID {
cmd.Println(string(chain.ID), string(raw))
} else {
cmd.Println(string(raw))
}
}
return nil
}
func resolveSubject(ffsid *ffsidclient.Client, namespace, userName string) (*ffsidclient.SubjectExtended, error) { func resolveSubject(ffsid *ffsidclient.Client, namespace, userName string) (*ffsidclient.SubjectExtended, error) {
if userHash, err := neoflags.ParseAddress(userName); err == nil { if userHash, err := neoflags.ParseAddress(userName); err == nil {
subj, err := ffsid.GetSubject(userHash) subj, err := ffsid.GetSubject(userHash)

View file

@ -4,7 +4,7 @@ import (
"encoding/base64" "encoding/base64"
"git.frostfs.info/TrueCloudLab/frostfs-contract/commonclient" "git.frostfs.info/TrueCloudLab/frostfs-contract/commonclient"
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain" "git.frostfs.info/dkirillov/policy-reader/internal/output"
"git.frostfs.info/dkirillov/policy-reader/internal/resolver" "git.frostfs.info/dkirillov/policy-reader/internal/resolver"
"github.com/nspcc-dev/neo-go/pkg/rpcclient" "github.com/nspcc-dev/neo-go/pkg/rpcclient"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker" "github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
@ -90,36 +90,6 @@ func runListChainsByPrefixCmd(cmd *cobra.Command, _ []string) error {
cmd.Printf("%s target chains names: %d\n", typ, len(res)) cmd.Printf("%s target chains names: %d\n", typ, len(res))
decodeID := viper.GetBool(decodeIDFlag) decodeChain := typ != "iam"
return output.PrintChains(cmd, res, decodeChain, viper.GetBool(decodeIDFlag))
for _, re := range res {
bytes, err := re.TryBytes()
if err != nil {
return err
}
if typ == "iam" {
cmd.Println(string(bytes))
continue
}
var chain apechain.Chain
if err = chain.DecodeBytes(bytes); err != nil {
cmd.Printf("invalid chain format: %s\n", base64.StdEncoding.EncodeToString(bytes))
continue
}
raw, err := chain.MarshalJSON()
if err != nil {
return err
}
if decodeID {
cmd.Println(string(chain.ID), string(raw))
} else {
cmd.Println(string(raw))
}
}
return nil
} }