forked from TrueCloudLab/frostfs-node
[#1381] neofs-cli: Move JSON and verbose printing to a common package
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
c57114def3
commit
71d823f192
7 changed files with 57 additions and 39 deletions
23
cmd/neofs-cli/internal/common/json.go
Normal file
23
cmd/neofs-cli/internal/common/json.go
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PrettyPrintJSON prints m as an indented JSON to the cmd output.
|
||||||
|
func PrettyPrintJSON(cmd *cobra.Command, m json.Marshaler, entity string) {
|
||||||
|
data, err := m.MarshalJSON()
|
||||||
|
if err != nil {
|
||||||
|
PrintVerbose("Can't convert %s to json: %w", entity, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
if err := json.Indent(buf, data, "", " "); err != nil {
|
||||||
|
PrintVerbose("Can't pretty print json: %w", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cmd.Println(buf)
|
||||||
|
}
|
15
cmd/neofs-cli/internal/common/verbose.go
Normal file
15
cmd/neofs-cli/internal/common/verbose.go
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PrintVerbose prints to the stdout if the commonflags.Verbose flag is on.
|
||||||
|
func PrintVerbose(format string, a ...interface{}) {
|
||||||
|
if viper.GetBool(commonflags.Verbose) {
|
||||||
|
fmt.Printf(format+"\n", a...)
|
||||||
|
}
|
||||||
|
}
|
|
@ -383,7 +383,7 @@ var getExtendedACLCmd = &cobra.Command{
|
||||||
sig.WriteToV2(&sigV2)
|
sig.WriteToV2(&sigV2)
|
||||||
|
|
||||||
cmd.Println("Signature:")
|
cmd.Println("Signature:")
|
||||||
printJSONMarshaler(cmd, &sigV2, "signature")
|
common.PrettyPrintJSON(cmd, &sigV2, "signature")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -401,7 +401,7 @@ var getExtendedACLCmd = &cobra.Command{
|
||||||
cmd.Println("dumping data to file:", containerPathTo)
|
cmd.Println("dumping data to file:", containerPathTo)
|
||||||
|
|
||||||
cmd.Println("Signature:")
|
cmd.Println("Signature:")
|
||||||
printJSONMarshaler(cmd, &sigV2, "signature")
|
common.PrettyPrintJSON(cmd, &sigV2, "signature")
|
||||||
|
|
||||||
err = os.WriteFile(containerPathTo, data, 0644)
|
err = os.WriteFile(containerPathTo, data, 0644)
|
||||||
common.ExitOnErr(cmd, "could not write eACL to file: %w", err)
|
common.ExitOnErr(cmd, "could not write eACL to file: %w", err)
|
||||||
|
@ -631,7 +631,7 @@ func parseSubnetID(val string) (sub *subnetid.ID, err error) {
|
||||||
func parseContainerPolicy(policyString string) (*netmap.PlacementPolicy, error) {
|
func parseContainerPolicy(policyString string) (*netmap.PlacementPolicy, error) {
|
||||||
_, err := os.Stat(policyString) // check if `policyString` is a path to file with placement policy
|
_, err := os.Stat(policyString) // check if `policyString` is a path to file with placement policy
|
||||||
if err == nil {
|
if err == nil {
|
||||||
printVerbose("Reading placement policy from file: %s", policyString)
|
common.PrintVerbose("Reading placement policy from file: %s", policyString)
|
||||||
|
|
||||||
data, err := os.ReadFile(policyString)
|
data, err := os.ReadFile(policyString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -643,13 +643,13 @@ func parseContainerPolicy(policyString string) (*netmap.PlacementPolicy, error)
|
||||||
|
|
||||||
result, err := policy.Parse(policyString)
|
result, err := policy.Parse(policyString)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
printVerbose("Parsed QL encoded policy")
|
common.PrintVerbose("Parsed QL encoded policy")
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
result = netmap.NewPlacementPolicy()
|
result = netmap.NewPlacementPolicy()
|
||||||
if err = result.UnmarshalJSON([]byte(policyString)); err == nil {
|
if err = result.UnmarshalJSON([]byte(policyString)); err == nil {
|
||||||
printVerbose("Parsed JSON encoded policy")
|
common.PrintVerbose("Parsed JSON encoded policy")
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -704,7 +704,7 @@ func parseBasicACL(basicACL string) (acl.BasicACL, error) {
|
||||||
func parseNonce(nonce string) (uuid.UUID, error) {
|
func parseNonce(nonce string) (uuid.UUID, error) {
|
||||||
if nonce == "" {
|
if nonce == "" {
|
||||||
result := uuid.New()
|
result := uuid.New()
|
||||||
printVerbose("Generating container nonce: %s", result)
|
common.PrintVerbose("Generating container nonce: %s", result)
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
@ -740,12 +740,12 @@ func prettyPrintContainer(cmd *cobra.Command, cnr *container.Container, jsonEnco
|
||||||
if jsonEncoding {
|
if jsonEncoding {
|
||||||
data, err := cnr.MarshalJSON()
|
data, err := cnr.MarshalJSON()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printVerbose("Can't convert container to json: %w", err)
|
common.PrintVerbose("Can't convert container to json: %w", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
if err := json.Indent(buf, data, "", " "); err != nil {
|
if err := json.Indent(buf, data, "", " "); err != nil {
|
||||||
printVerbose("Can't pretty print json: %w", err)
|
common.PrintVerbose("Can't pretty print json: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Println(buf)
|
cmd.Println(buf)
|
||||||
|
@ -794,7 +794,7 @@ func parseEACL(eaclPath string) (*eacl.Table, error) {
|
||||||
return nil, errors.New("incorrect path to file with EACL")
|
return nil, errors.New("incorrect path to file with EACL")
|
||||||
}
|
}
|
||||||
|
|
||||||
printVerbose("Reading EACL from file: %s", eaclPath)
|
common.PrintVerbose("Reading EACL from file: %s", eaclPath)
|
||||||
|
|
||||||
data, err := os.ReadFile(eaclPath)
|
data, err := os.ReadFile(eaclPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -805,13 +805,13 @@ func parseEACL(eaclPath string) (*eacl.Table, error) {
|
||||||
|
|
||||||
if err = table.UnmarshalJSON(data); err == nil {
|
if err = table.UnmarshalJSON(data); err == nil {
|
||||||
validateAndFixEACLVersion(table)
|
validateAndFixEACLVersion(table)
|
||||||
printVerbose("Parsed JSON encoded EACL table")
|
common.PrintVerbose("Parsed JSON encoded EACL table")
|
||||||
return table, nil
|
return table, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = table.Unmarshal(data); err == nil {
|
if err = table.Unmarshal(data); err == nil {
|
||||||
validateAndFixEACLVersion(table)
|
validateAndFixEACLVersion(table)
|
||||||
printVerbose("Parsed binary encoded EACL table")
|
common.PrintVerbose("Parsed binary encoded EACL table")
|
||||||
return table, nil
|
return table, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -825,21 +825,7 @@ func validateAndFixEACLVersion(table *eacl.Table) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func prettyPrintEACL(cmd *cobra.Command, table *eacl.Table) {
|
func prettyPrintEACL(cmd *cobra.Command, table *eacl.Table) {
|
||||||
printJSONMarshaler(cmd, table, "eACL")
|
common.PrettyPrintJSON(cmd, table, "eACL")
|
||||||
}
|
|
||||||
|
|
||||||
func printJSONMarshaler(cmd *cobra.Command, j json.Marshaler, entity string) {
|
|
||||||
data, err := j.MarshalJSON()
|
|
||||||
if err != nil {
|
|
||||||
printVerbose("Can't convert %s to json: %w", entity, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
if err := json.Indent(buf, data, "", " "); err != nil {
|
|
||||||
printVerbose("Can't pretty print json: %w", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cmd.Println(buf)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func prettyPrintBasicACL(cmd *cobra.Command, basicACL acl.BasicACL) {
|
func prettyPrintBasicACL(cmd *cobra.Command, basicACL acl.BasicACL) {
|
||||||
|
|
|
@ -189,7 +189,7 @@ var netInfoCmd = &cobra.Command{
|
||||||
|
|
||||||
func prettyPrintNodeInfo(cmd *cobra.Command, i *netmap.NodeInfo, jsonEncoding bool) {
|
func prettyPrintNodeInfo(cmd *cobra.Command, i *netmap.NodeInfo, jsonEncoding bool) {
|
||||||
if jsonEncoding {
|
if jsonEncoding {
|
||||||
printJSONMarshaler(cmd, i, "node info")
|
common.PrettyPrintJSON(cmd, i, "node info")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,7 +206,7 @@ func prettyPrintNodeInfo(cmd *cobra.Command, i *netmap.NodeInfo, jsonEncoding bo
|
||||||
|
|
||||||
func prettyPrintNetmap(cmd *cobra.Command, nm *control.Netmap, jsonEncoding bool) {
|
func prettyPrintNetmap(cmd *cobra.Command, nm *control.Netmap, jsonEncoding bool) {
|
||||||
if jsonEncoding {
|
if jsonEncoding {
|
||||||
printJSONMarshaler(cmd, nm, "netmap")
|
common.PrettyPrintJSON(cmd, nm, "netmap")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1128,9 +1128,9 @@ func getBearerToken(cmd *cobra.Command, flagname string) (*bearer.Token, error)
|
||||||
return nil, fmt.Errorf("can't decode bearer token: %w", err)
|
return nil, fmt.Errorf("can't decode bearer token: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
printVerbose("Using binary encoded bearer token")
|
common.PrintVerbose("Using binary encoded bearer token")
|
||||||
} else {
|
} else {
|
||||||
printVerbose("Using JSON encoded bearer token")
|
common.PrintVerbose("Using JSON encoded bearer token")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &tok, nil
|
return &tok, nil
|
||||||
|
|
|
@ -133,7 +133,7 @@ func initConfig() {
|
||||||
|
|
||||||
// If a config file is found, read it in.
|
// If a config file is found, read it in.
|
||||||
if err := viper.ReadInConfig(); err == nil {
|
if err := viper.ReadInConfig(); err == nil {
|
||||||
printVerbose("Using config file: %s", viper.ConfigFileUsed())
|
common.PrintVerbose("Using config file: %s", viper.ConfigFileUsed())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,7 +181,7 @@ func prepareBearerPrm(cmd *cobra.Command, prm bearerPrm) {
|
||||||
|
|
||||||
func getTTL() uint32 {
|
func getTTL() uint32 {
|
||||||
ttl := viper.GetUint32(ttl)
|
ttl := viper.GetUint32(ttl)
|
||||||
printVerbose("TTL: %d", ttl)
|
common.PrintVerbose("TTL: %d", ttl)
|
||||||
|
|
||||||
return ttl
|
return ttl
|
||||||
}
|
}
|
||||||
|
@ -196,12 +196,6 @@ func userFromString(id *user.ID, s string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func printVerbose(format string, a ...interface{}) {
|
|
||||||
if viper.GetBool(commonflags.Verbose) {
|
|
||||||
fmt.Printf(format+"\n", a...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseXHeaders() []*session.XHeader {
|
func parseXHeaders() []*session.XHeader {
|
||||||
xs := make([]*session.XHeader, 0, len(xHeaders))
|
xs := make([]*session.XHeader, 0, len(xHeaders))
|
||||||
|
|
||||||
|
|
|
@ -412,7 +412,7 @@ func processKeyer(cmd *cobra.Command, args []string) {
|
||||||
func prettyPrintJSON(cmd *cobra.Command, data []byte) {
|
func prettyPrintJSON(cmd *cobra.Command, data []byte) {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
if err := json.Indent(buf, data, "", " "); err != nil {
|
if err := json.Indent(buf, data, "", " "); err != nil {
|
||||||
printVerbose("Can't pretty print json: %w", err)
|
common.PrintVerbose("Can't pretty print json: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Println(buf)
|
cmd.Println(buf)
|
||||||
|
|
Loading…
Reference in a new issue