From cbbda7fe8e457d5e309fb1580cf62b7b82f90cdb Mon Sep 17 00:00:00 2001 From: Viacheslav Gonkivskyi Date: Thu, 3 Aug 2023 17:13:45 +0300 Subject: [PATCH] feat: add flag that allows to print blobs data as a plain text --- cmd/celestia/blob.go | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/cmd/celestia/blob.go b/cmd/celestia/blob.go index c600e1acca..3d2a9087e1 100644 --- a/cmd/celestia/blob.go +++ b/cmd/celestia/blob.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "os" + "reflect" "strconv" "github.com/spf13/cobra" @@ -13,8 +14,23 @@ import ( "github.com/celestiaorg/celestia-node/share" ) +var fomatedData bool + func init() { blobCmd.AddCommand(getCmd, getAllCmd, submitCmd, getProofCmd) + + getCmd.PersistentFlags().BoolVar( + &fomatedData, + "data.format", + false, + "printed blob's data as a plain text", + ) + getAllCmd.PersistentFlags().BoolVar( + &fomatedData, + "data.format", + false, + "printed blob's data as a plain text", + ) } var blobCmd = &cobra.Command{ @@ -154,6 +170,10 @@ func printOutput(data interface{}, err error) { data = err } + if fomatedData && err == nil { + data = formatData(data) + } + resp := struct { Result interface{} `json:"result"` }{ @@ -167,3 +187,44 @@ func printOutput(data interface{}, err error) { } fmt.Fprintln(os.Stdout, string(bytes)) } + +func formatData(data interface{}) interface{} { + type tempBlob struct { + Namespace []byte `json:"namespace"` + Data string `json:"data"` + ShareVersion uint32 `json:"share_version"` + Commitment []byte `json:"commitment"` + } + + if reflect.TypeOf(data).Kind() == reflect.Slice { + blobs, ok := data.([]*blob.Blob) + if !ok { + fmt.Fprintln(os.Stderr, "could not cast to []blob.Blob") + os.Exit(1) + } + + result := make([]tempBlob, len(blobs)) + for i, b := range blobs { + result[i] = tempBlob{ + Namespace: b.Namespace(), + Data: string(b.Data), + ShareVersion: b.ShareVersion, + Commitment: b.Commitment, + } + } + return result + } + + b, ok := data.(*blob.Blob) + if !ok { + fmt.Fprintln(os.Stderr, "could not cast to blob.Blob") + os.Exit(1) + } + return tempBlob{ + Namespace: b.Namespace(), + Data: string(b.Data), + ShareVersion: b.ShareVersion, + NamespaceVersion: b.NamespaceVersion, + Commitment: b.Commitment, + } +}