From 71d823f192324baafb56bd75792228b1cc6724bf Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 23 May 2022 18:48:01 +0300 Subject: [PATCH] [#1381] neofs-cli: Move JSON and verbose printing to a common package Signed-off-by: Evgenii Stratonikov --- cmd/neofs-cli/internal/common/json.go | 23 ++++++++++++++ cmd/neofs-cli/internal/common/verbose.go | 15 ++++++++++ cmd/neofs-cli/modules/container.go | 38 ++++++++---------------- cmd/neofs-cli/modules/netmap.go | 4 +-- cmd/neofs-cli/modules/object.go | 4 +-- cmd/neofs-cli/modules/root.go | 10 ++----- cmd/neofs-cli/modules/util.go | 2 +- 7 files changed, 57 insertions(+), 39 deletions(-) create mode 100644 cmd/neofs-cli/internal/common/json.go create mode 100644 cmd/neofs-cli/internal/common/verbose.go diff --git a/cmd/neofs-cli/internal/common/json.go b/cmd/neofs-cli/internal/common/json.go new file mode 100644 index 00000000..5664b185 --- /dev/null +++ b/cmd/neofs-cli/internal/common/json.go @@ -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) +} diff --git a/cmd/neofs-cli/internal/common/verbose.go b/cmd/neofs-cli/internal/common/verbose.go new file mode 100644 index 00000000..48f9a2bb --- /dev/null +++ b/cmd/neofs-cli/internal/common/verbose.go @@ -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...) + } +} diff --git a/cmd/neofs-cli/modules/container.go b/cmd/neofs-cli/modules/container.go index 0572188d..7e202c7f 100644 --- a/cmd/neofs-cli/modules/container.go +++ b/cmd/neofs-cli/modules/container.go @@ -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) { diff --git a/cmd/neofs-cli/modules/netmap.go b/cmd/neofs-cli/modules/netmap.go index e53ada98..52478f25 100644 --- a/cmd/neofs-cli/modules/netmap.go +++ b/cmd/neofs-cli/modules/netmap.go @@ -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 } diff --git a/cmd/neofs-cli/modules/object.go b/cmd/neofs-cli/modules/object.go index 0c41c101..aa50edd2 100644 --- a/cmd/neofs-cli/modules/object.go +++ b/cmd/neofs-cli/modules/object.go @@ -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 diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index 1c2d1d27..14b265fb 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -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)) diff --git a/cmd/neofs-cli/modules/util.go b/cmd/neofs-cli/modules/util.go index 423317d3..2e919d90 100644 --- a/cmd/neofs-cli/modules/util.go +++ b/cmd/neofs-cli/modules/util.go @@ -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)