Skip to content

Commit

Permalink
add printOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Aug 3, 2023
1 parent cbc178b commit b69055f
Showing 1 changed file with 14 additions and 34 deletions.
48 changes: 14 additions & 34 deletions cmd/celestia/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import (
"github.com/celestiaorg/celestia-node/share"
)

type response struct {
Result interface{} `json:"result"`
}

func init() {
blobCmd.AddCommand(getCmd, getAllCmd, submitCmd, getProofCmd)
}
Expand Down Expand Up @@ -54,12 +50,7 @@ var getCmd = &cobra.Command{

blob, err := client.Blob.Get(cmd.Context(), height, namespace, commitment)

output, err := prepareOutput(blob, err)
if err != nil {
return err
}

fmt.Fprintln(os.Stdout, string(output))
printOutput(blob, err)
return nil
},
}
Expand All @@ -84,15 +75,9 @@ var getAllCmd = &cobra.Command{
return fmt.Errorf("error parsing a namespace:%v", err)
}

var output []byte
blobs, err := client.Blob.GetAll(cmd.Context(), height, []share.Namespace{namespace})

output, err = prepareOutput(blobs, err)
if err != nil {
return err
}

fmt.Fprintln(os.Stdout, string(output))
printOutput(blobs, err)
return nil
},
}
Expand Down Expand Up @@ -127,12 +112,7 @@ var submitCmd = &cobra.Command{
Commitment: parsedBlob.Commitment,
}

output, err := prepareOutput(response, err)
if err != nil {
return err
}

fmt.Fprintln(os.Stdout, string(output))
printOutput(response, err)
return nil
},
}
Expand Down Expand Up @@ -164,26 +144,26 @@ var getProofCmd = &cobra.Command{

proof, err := client.Blob.GetProof(cmd.Context(), height, namespace, commitment)

output, err := prepareOutput(proof, err)
if err != nil {
return err
}

fmt.Fprintln(os.Stdout, string(output))
printOutput(proof, err)
return nil
},
}

func prepareOutput(data interface{}, err error) ([]byte, error) {
func printOutput(data interface{}, err error) {
if err != nil {
data = err
}

bytes, err := json.MarshalIndent(response{
resp := struct {
Result interface{} `json:"result"`
}{
Result: data,
}, "", " ")
}

bytes, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return nil, err
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return bytes, nil
fmt.Fprintln(os.Stdout, string(bytes))
}

0 comments on commit b69055f

Please sign in to comment.