[#1381] neofs-cli: Move JSON and verbose printing to a common package

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
remotes/fyrchik/cli-container
Evgenii Stratonikov 2022-05-23 18:48:01 +03:00 committed by LeL
parent c57114def3
commit 71d823f192
7 changed files with 57 additions and 39 deletions

View 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)
}

View 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...)
}
}

View File

@ -383,7 +383,7 @@ var getExtendedACLCmd = &cobra.Command{
sig.WriteToV2(&sigV2)
cmd.Println("Signature:")
printJSONMarshaler(cmd, &sigV2, "signature")
common.PrettyPrintJSON(cmd, &sigV2, "signature")
return
}
@ -401,7 +401,7 @@ var getExtendedACLCmd = &cobra.Command{
cmd.Println("dumping data to file:", containerPathTo)
cmd.Println("Signature:")
printJSONMarshaler(cmd, &sigV2, "signature")
common.PrettyPrintJSON(cmd, &sigV2, "signature")
err = os.WriteFile(containerPathTo, data, 0644)
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) {
_, err := os.Stat(policyString) // check if `policyString` is a path to file with placement policy
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)
if err != nil {
@ -643,13 +643,13 @@ func parseContainerPolicy(policyString string) (*netmap.PlacementPolicy, error)
result, err := policy.Parse(policyString)
if err == nil {
printVerbose("Parsed QL encoded policy")
common.PrintVerbose("Parsed QL encoded policy")
return result, nil
}
result = netmap.NewPlacementPolicy()
if err = result.UnmarshalJSON([]byte(policyString)); err == nil {
printVerbose("Parsed JSON encoded policy")
common.PrintVerbose("Parsed JSON encoded policy")
return result, nil
}
@ -704,7 +704,7 @@ func parseBasicACL(basicACL string) (acl.BasicACL, error) {
func parseNonce(nonce string) (uuid.UUID, error) {
if nonce == "" {
result := uuid.New()
printVerbose("Generating container nonce: %s", result)
common.PrintVerbose("Generating container nonce: %s", result)
return result, nil
}
@ -740,12 +740,12 @@ func prettyPrintContainer(cmd *cobra.Command, cnr *container.Container, jsonEnco
if jsonEncoding {
data, err := cnr.MarshalJSON()
if err != nil {
printVerbose("Can't convert container to json: %w", err)
common.PrintVerbose("Can't convert container to json: %w", err)
return
}
buf := new(bytes.Buffer)
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)
@ -794,7 +794,7 @@ func parseEACL(eaclPath string) (*eacl.Table, error) {
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)
if err != nil {
@ -805,13 +805,13 @@ func parseEACL(eaclPath string) (*eacl.Table, error) {
if err = table.UnmarshalJSON(data); err == nil {
validateAndFixEACLVersion(table)
printVerbose("Parsed JSON encoded EACL table")
common.PrintVerbose("Parsed JSON encoded EACL table")
return table, nil
}
if err = table.Unmarshal(data); err == nil {
validateAndFixEACLVersion(table)
printVerbose("Parsed binary encoded EACL table")
common.PrintVerbose("Parsed binary encoded EACL table")
return table, nil
}
@ -825,21 +825,7 @@ func validateAndFixEACLVersion(table *eacl.Table) {
}
func prettyPrintEACL(cmd *cobra.Command, table *eacl.Table) {
printJSONMarshaler(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)
common.PrettyPrintJSON(cmd, table, "eACL")
}
func prettyPrintBasicACL(cmd *cobra.Command, basicACL acl.BasicACL) {

View File

@ -189,7 +189,7 @@ var netInfoCmd = &cobra.Command{
func prettyPrintNodeInfo(cmd *cobra.Command, i *netmap.NodeInfo, jsonEncoding bool) {
if jsonEncoding {
printJSONMarshaler(cmd, i, "node info")
common.PrettyPrintJSON(cmd, i, "node info")
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) {
if jsonEncoding {
printJSONMarshaler(cmd, nm, "netmap")
common.PrettyPrintJSON(cmd, nm, "netmap")
return
}

View File

@ -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)
}
printVerbose("Using binary encoded bearer token")
common.PrintVerbose("Using binary encoded bearer token")
} else {
printVerbose("Using JSON encoded bearer token")
common.PrintVerbose("Using JSON encoded bearer token")
}
return &tok, nil

View File

@ -133,7 +133,7 @@ func initConfig() {
// If a config file is found, read it in.
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 {
ttl := viper.GetUint32(ttl)
printVerbose("TTL: %d", ttl)
common.PrintVerbose("TTL: %d", ttl)
return ttl
}
@ -196,12 +196,6 @@ func userFromString(id *user.ID, s string) error {
return nil
}
func printVerbose(format string, a ...interface{}) {
if viper.GetBool(commonflags.Verbose) {
fmt.Printf(format+"\n", a...)
}
}
func parseXHeaders() []*session.XHeader {
xs := make([]*session.XHeader, 0, len(xHeaders))

View File

@ -412,7 +412,7 @@ func processKeyer(cmd *cobra.Command, args []string) {
func prettyPrintJSON(cmd *cobra.Command, data []byte) {
buf := new(bytes.Buffer)
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)