Skip to content

Commit

Permalink
feat: output detail in json format
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZexiao committed Feb 24, 2023
1 parent 5312a79 commit cbf6b8a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 72 deletions.
10 changes: 10 additions & 0 deletions cli/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helper

import (
"context"
"encoding/json"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -202,3 +203,12 @@ func (e *PrintHelpErr) Is(o error) bool {
func NewTabWriter(w io.Writer) *tabwriter.Writer {
return tabwriter.NewWriter(w, 2, 4, 2, ' ', 0)
}

func PrintJSON(v interface{}) error {
bytes, err := json.MarshalIndent(v, " ", "\t")
if err != nil {
return err
}
fmt.Println(string(bytes))
return nil
}
104 changes: 32 additions & 72 deletions cli/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@ package cli

import (
"encoding/hex"
"encoding/json"
"fmt"
"reflect"
"time"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-fil-markets/storagemarket"
"github.com/filecoin-project/go-fil-markets/storagemarket/network"
"github.com/filecoin-project/go-state-types/builtin/v8/market"
"github.com/filecoin-project/go-state-types/builtin/v8/paych"
"github.com/filecoin-project/venus-wallet/cli/helper"
"github.com/filecoin-project/venus-wallet/storage/wallet"
"github.com/filecoin-project/venus/venus-shared/types"
"github.com/urfave/cli/v2"

types2 "github.com/filecoin-project/venus/venus-shared/types/wallet"
)

var recordCmd = &cli.Command{
Expand Down Expand Up @@ -136,22 +131,29 @@ var recordQuery = &cli.Command{
if err != nil {
return fmt.Errorf("query sign record: %w", err)
}
// output in table format
w := helper.NewTabWriter(cctx.App.Writer)

if cctx.Bool("verbose") {
fmt.Fprintln(w, "ID\tSIGNER\tTYPE\tTIME\tDETAIL\tERROR")
for _, r := range records {
errStr := "no error"
if r.Err != nil {
errStr = r.Err.Error()
}
detail, err := getDetail(&r)
output := make([]interface{}, len(records))
type temp struct {
types.SignRecord
Detail json.RawMessage
}

for i, r := range records {
detail, err := GetDetailInJsonRawMessage(&r)
if err != nil {
return fmt.Errorf("get detail: %w", err)
return err
}
output[i] = temp{
SignRecord: r,
Detail: detail,
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n", r.ID, r.Signer, r.Type, r.CreateAt, detail, errStr)
}

return helper.PrintJSON(output)
} else {
// output in table format
w := helper.NewTabWriter(cctx.App.Writer)
fmt.Fprintln(w, "SIGNER\tTYPE\tTIME\tERROR")
for _, r := range records {
errStr := "no error"
Expand All @@ -160,84 +162,42 @@ var recordQuery = &cli.Command{
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", r.Signer, r.Type, r.CreateAt, errStr)
}
w.Flush()
}
w.Flush()

return nil
},
}

func getDetail(r *types.SignRecord) (string, error) {
var ret string
func GetDetailInJsonRawMessage(r *types.SignRecord) (json.RawMessage, error) {
t, ok := wallet.SupportedMsgTypes[r.Type]
if !ok {
return "", fmt.Errorf("unsupported type %s", r.Type)
return nil, fmt.Errorf("unsupported type %s", r.Type)
}

wrap := func(err error) error {
return fmt.Errorf("get detail: %w", err)
}

if r.Msg == nil {
return "", wrap(fmt.Errorf("msg is nil"))
return nil, wrap(fmt.Errorf("msg is nil"))
}

if r.Type == types.MTVerifyAddress || r.Type == types.MTUnknown {
// encode into hex string
hs := hex.EncodeToString(r.Msg)
return fmt.Sprintf("Hex:%s.", hs), nil
output := struct {
Hex string
}{
Hex: hex.EncodeToString(r.Msg),
}

return json.Marshal(output)
}

signObj := reflect.New(t.Type).Interface()
if err := wallet.CborDecodeInto(r.Msg, signObj); err != nil {
return "", fmt.Errorf("decode msg:%w", err)
}
switch r.Type {
case types.MTDealProposal:
deal := signObj.(*market.DealProposal)
cid, err := deal.Cid()
if err != nil {
return "", wrap(err)
}
ret = fmt.Sprintf("DealProposal:%s; Client:%s; Provider:%s.", cid.String(), deal.Client.String(), deal.Provider.String())
case types.MTClientDeal:
deal := signObj.(*market.ClientDealProposal)
cid, err := deal.Proposal.Cid()
if err != nil {
return "", wrap(err)
}
ret = fmt.Sprintf("ClientDeal:%s; Client:%s; Provider:%s.", cid.String(), deal.Proposal.Client.String(), deal.Proposal.Provider.String())
case types.MTDrawRandomParam:
param := signObj.(*types2.DrawRandomParams)
ret = fmt.Sprintf("Pers:%d ; Round:%s; Entropy :%s.", param.Pers, param.Round, param.Entropy)
case types.MTSignedVoucher:
voucher := signObj.(*paych.SignedVoucher)
ret = fmt.Sprintf("Channel:%s; Amount:%s; Lane:%d .", voucher.ChannelAddr.String(), voucher.Amount.String(), voucher.Lane)
case types.MTStorageAsk:
ask := signObj.(*storagemarket.StorageAsk)
ret = fmt.Sprintf("Miner:%s; Price:%s; VerifiedPrice:%s.", ask.Miner.String(), ask.Price.String(), ask.VerifiedPrice.String())
case types.MTAskResponse:
resp := signObj.(*network.AskResponse)
ret = fmt.Sprintf("Miner:%s; Price:%s; VerifiedPrice:%s.", resp.Ask.Ask.Miner.String(), resp.Ask.Ask.Price.String(), resp.Ask.Ask.VerifiedPrice.String())
return ret, nil
case types.MTNetWorkResponse:
resp := signObj.(*network.Response)
if resp.State != storagemarket.StorageDealUnknown {
resp := signObj.(network.Response)
ret = fmt.Sprintf("State:%s; ProposalCid:%s.", storagemarket.DealStates[resp.State], resp.Proposal)
}
case types.MTBlock:
block := signObj.(*types.BlockHeader)
ret = fmt.Sprintf("Height:%d ; Miner:%s.", block.Height, block.Miner.String())
case types.MTChainMsg:
msg := signObj.(*types.Message)
ret = fmt.Sprintf("To:%s; Value:%s; Method:%s.", msg.To.String(), msg.Value.String(), msg.Method)
case types.MTProviderDealState:
deal := signObj.(*storagemarket.ProviderDealState)
ret = fmt.Sprintf("ProposalCid:%s; State:%s.", deal.ProposalCid.String(), storagemarket.DealStates[deal.State])
default:
ret = "unknown message type"
return nil, fmt.Errorf("decode msg:%w", err)
}
return json.Marshal(signObj)

return ret, nil
}

0 comments on commit cbf6b8a

Please sign in to comment.