Skip to content

Commit

Permalink
feat: add flag that allows to print blobs data as a plain text
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Aug 3, 2023
1 parent b69055f commit cbbda7f
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions cmd/celestia/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"os"
"reflect"
"strconv"

"github.com/spf13/cobra"
Expand All @@ -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{
Expand Down Expand Up @@ -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"`
}{
Expand All @@ -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,
}
}

0 comments on commit cbbda7f

Please sign in to comment.