Merge pull request #1710 from filecoin-project/feat/bitfield-decode-shed

add method to decode and print all bitfield entries to lotus shed
This commit is contained in:
Łukasz Magiera 2020-05-12 23:35:42 +02:00 committed by GitHub
commit e3e86ec983

View File

@ -10,6 +10,7 @@ import (
"golang.org/x/xerrors"
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/go-bitfield"
rlepluslazy "github.com/filecoin-project/go-bitfield/rle"
)
@ -26,6 +27,7 @@ var bitFieldCmd = &cli.Command{
Subcommands: []*cli.Command{
bitFieldRunsCmd,
bitFieldStatCmd,
bitFieldDecodeCmd,
},
}
@ -186,3 +188,58 @@ var bitFieldStatCmd = &cli.Command{
return nil
},
}
var bitFieldDecodeCmd = &cli.Command{
Name: "decode",
Description: "decode bitfield and print all numbers in it",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "enc",
Value: "base64",
Usage: "specify input encoding to parse",
},
},
Action: func(cctx *cli.Context) error {
var val string
if cctx.Args().Present() {
val = cctx.Args().Get(0)
} else {
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
val = string(b)
}
var dec []byte
switch cctx.String("enc") {
case "base64":
d, err := base64.StdEncoding.DecodeString(val)
if err != nil {
return fmt.Errorf("decoding base64 value: %w", err)
}
dec = d
case "hex":
d, err := hex.DecodeString(val)
if err != nil {
return fmt.Errorf("decoding hex value: %w", err)
}
dec = d
default:
return fmt.Errorf("unrecognized encoding: %s", cctx.String("enc"))
}
rle, err := bitfield.NewFromBytes(dec)
if err != nil {
return xerrors.Errorf("failed to parse bitfield: %w", err)
}
vals, err := rle.All(100000000000)
if err != nil {
return xerrors.Errorf("getting all items: %w", err)
}
fmt.Println(vals)
return nil
},
}