Add a command to get and print a message

This commit is contained in:
whyrusleeping 2019-10-09 16:26:48 +09:00
parent b8bc54fd5b
commit 3faa8bc88a

View File

@ -19,6 +19,7 @@ var chainCmd = &cli.Command{
chainHeadCmd, chainHeadCmd,
chainGetBlock, chainGetBlock,
chainReadObjCmd, chainReadObjCmd,
chainGetMsgCmd,
}, },
} }
@ -160,3 +161,46 @@ var chainReadObjCmd = &cli.Command{
return nil return nil
}, },
} }
var chainGetMsgCmd = &cli.Command{
Name: "getmessage",
Usage: "Get and print a message by its cid",
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
c, err := cid.Decode(cctx.Args().First())
if err != nil {
return xerrors.Errorf("failed to parse cid input: %w", err)
}
mb, err := api.ChainReadObj(ctx, c)
if err != nil {
return xerrors.Errorf("failed to read object: %w", err)
}
var i interface{}
m, err := types.DecodeMessage(mb)
if err != nil {
sm, err := types.DecodeSignedMessage(mb)
if err != nil {
return xerrors.Errorf("failed to decode object as a message: %w", err)
}
i = sm
} else {
i = m
}
enc, err := json.MarshalIndent(i, "", " ")
if err != nil {
return err
}
fmt.Println(string(enc))
return nil
},
}