Skip to content

Commit e1a3d63

Browse files
LexLuthrdirkmc
andauthored
feat: add json output to boostd pieces command (#806)
* piece json output * small fixes * Apply suggestions from code review Co-authored-by: dirkmc <[email protected]> * move json flag to boostd Co-authored-by: dirkmc <[email protected]>
1 parent 207b5cc commit e1a3d63

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

cmd/boostd/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"os"
55

6+
"github.com/filecoin-project/boost/cmd"
67
logging "github.com/ipfs/go-log/v2"
78
"github.com/urfave/cli/v2"
89

@@ -29,6 +30,7 @@ func main() {
2930
Usage: "boost repo path",
3031
Value: "~/.boost",
3132
},
33+
cmd.FlagJson,
3234
cliutil.FlagVeryVerbose,
3335
},
3436
Commands: []*cli.Command{

cmd/boostd/pieces.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"text/tabwriter"
77

88
bcli "github.com/filecoin-project/boost/cli"
9+
"github.com/filecoin-project/boost/cmd"
910
lcli "github.com/filecoin-project/lotus/cli"
1011
"github.com/filecoin-project/lotus/lib/tablewriter"
1112
"github.com/ipfs/go-cid"
@@ -40,9 +41,18 @@ var piecesListPiecesCmd = &cli.Command{
4041
return err
4142
}
4243

44+
if cctx.Bool("json") {
45+
46+
pieceCidsJson := map[string]interface{}{
47+
"pieceCids": pieceCids,
48+
}
49+
return cmd.PrintJson(pieceCidsJson)
50+
}
51+
4352
for _, pc := range pieceCids {
4453
fmt.Println(pc)
4554
}
55+
4656
return nil
4757
},
4858
}
@@ -69,6 +79,14 @@ var piecesListCidInfosCmd = &cli.Command{
6979
return err
7080
}
7181

82+
if cctx.Bool("json") && !cctx.Bool("verbose") {
83+
dataCidsJson := map[string]interface{}{
84+
"dataCids": cids,
85+
}
86+
87+
return cmd.PrintJson(dataCidsJson)
88+
}
89+
7290
w := tablewriter.New(tablewriter.Col("CID"),
7391
tablewriter.Col("Piece"),
7492
tablewriter.Col("BlockOffset"),
@@ -79,12 +97,18 @@ var piecesListCidInfosCmd = &cli.Command{
7997
tablewriter.Col("DealLen"),
8098
)
8199

100+
type dataCids map[string]interface{}
101+
var out []dataCids
102+
82103
for _, c := range cids {
83104
if !cctx.Bool("verbose") {
84105
fmt.Println(c)
85106
continue
86107
}
87108

109+
type pbl map[string]interface{}
110+
var pbls []pbl
111+
88112
ci, err := nodeApi.PiecesGetCIDInfo(ctx, c)
89113
if err != nil {
90114
fmt.Printf("Error getting CID info: %s\n", err)
@@ -109,8 +133,34 @@ var piecesListCidInfosCmd = &cli.Command{
109133
"DealOffset": deal.Offset,
110134
"DealLen": deal.Length,
111135
})
136+
137+
deal := map[string]interface{}{
138+
"ID": deal.DealID,
139+
"Sector": deal.SectorID,
140+
"Offset": deal.Offset,
141+
"Length": deal.Length,
142+
}
143+
tpbl := pbl{
144+
"PieceCid": pi.PieceCID,
145+
"BlockOffset": location.RelOffset,
146+
"BlockLength": location.BlockSize,
147+
"Deal": deal,
148+
}
149+
150+
pbls = append(pbls, tpbl)
112151
}
113152
}
153+
154+
tdataCids := dataCids{
155+
"DataCid": c,
156+
"PieceBlockLocation": pbls,
157+
}
158+
159+
out = append(out, tdataCids)
160+
}
161+
162+
if cctx.Bool("json") && cctx.Bool("verbose") {
163+
return cmd.PrintJson(out)
114164
}
115165

116166
if cctx.Bool("verbose") {
@@ -146,6 +196,28 @@ var piecesInfoCmd = &cli.Command{
146196
return err
147197
}
148198

199+
if cctx.Bool("json") {
200+
201+
type deal map[string]interface{}
202+
var deals []deal
203+
for _, d := range pi.Deals {
204+
dl := deal{
205+
"ID": d.DealID,
206+
"SectorID": d.SectorID,
207+
"Offset": d.Offset,
208+
"Length": d.Length,
209+
}
210+
deals = append(deals, dl)
211+
}
212+
213+
pieceinfo := map[string]interface{}{
214+
"PieceCid": pi.PieceCID,
215+
"Deals": deals,
216+
}
217+
218+
return cmd.PrintJson(pieceinfo)
219+
}
220+
149221
fmt.Println("Piece: ", pi.PieceCID)
150222
w := tabwriter.NewWriter(os.Stdout, 4, 4, 2, ' ', 0)
151223
fmt.Fprintln(w, "Deals:\nDealID\tSectorID\tLength\tOffset")
@@ -181,6 +253,31 @@ var piecesCidInfoCmd = &cli.Command{
181253
return err
182254
}
183255

256+
if cctx.Bool("json") {
257+
258+
type pbl map[string]interface{}
259+
type bl map[string]interface{}
260+
var pbls []pbl
261+
for _, d := range ci.PieceBlockLocations {
262+
tbp := bl{
263+
"RelOffset": d.RelOffset,
264+
"BlockSize": d.BlockSize,
265+
}
266+
tpbl := pbl{
267+
"PieceCid": d.PieceCID,
268+
"BlockLocation": tbp,
269+
}
270+
pbls = append(pbls, tpbl)
271+
}
272+
273+
pieceinfo := map[string]interface{}{
274+
"DataCid": ci.CID,
275+
"PieceBlockLocation": pbls,
276+
}
277+
278+
return cmd.PrintJson(pieceinfo)
279+
}
280+
184281
fmt.Println("Info for: ", ci.CID)
185282

186283
w := tabwriter.NewWriter(os.Stdout, 4, 4, 2, ' ', 0)

0 commit comments

Comments
 (0)