Skip to content

Commit 98a40eb

Browse files
authored
Merge pull request #497 from ipfs-force-community/feat/ouput-deal-with-json-format
Feat/ouput deal with json format
2 parents dd49bab + 3a5e4cd commit 98a40eb

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

cli/storage-deals.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,10 @@ part states:
397397
Name: "oldest",
398398
Usage: "sort by oldest first",
399399
},
400+
&cli.BoolFlag{
401+
Name: "json",
402+
Usage: "output deal info as json format",
403+
},
400404
},
401405
Action: func(cctx *cli.Context) error {
402406
api, closer, err := NewMarketNode(cctx)
@@ -451,7 +455,7 @@ part states:
451455
tm.Clear()
452456
tm.MoveCursor(1, 1)
453457

454-
err = outputStorageDeals(tm.Output, deals, verbose)
458+
err = outputStorageDeals(tm.Output, deals, verbose, cctx.Bool("json"))
455459
if err != nil {
456460
return err
457461
}
@@ -477,7 +481,7 @@ part states:
477481
}
478482
}
479483

480-
return outputStorageDeals(os.Stdout, deals, verbose)
484+
return outputStorageDeals(os.Stdout, deals, verbose, cctx.Bool("json"))
481485
},
482486
}
483487

@@ -635,6 +639,10 @@ var getDealCmd = &cli.Command{
635639
Name: "proposal-cid",
636640
Usage: "cid of deal proposal",
637641
},
642+
&cli.BoolFlag{
643+
Name: "json",
644+
Usage: "output deal info as json format",
645+
},
638646
},
639647
Action: func(cliCtx *cli.Context) error {
640648
if !cliCtx.IsSet("deal-id") && !cliCtx.IsSet("proposal-cid") {
@@ -682,6 +690,15 @@ var getDealCmd = &cli.Command{
682690
}
683691
}
684692

693+
if cliCtx.Bool("json") {
694+
data, err := json.MarshalIndent(deal, "", " ")
695+
if err != nil {
696+
return err
697+
}
698+
fmt.Println(string(data))
699+
return nil
700+
}
701+
685702
return outputStorageDeal(deal)
686703
},
687704
}
@@ -743,11 +760,20 @@ func printStates(data interface{}) error {
743760
return nil
744761
}
745762

746-
func outputStorageDeals(out io.Writer, deals []market.MinerDeal, verbose bool) error {
763+
func outputStorageDeals(out io.Writer, deals []market.MinerDeal, verbose bool, inJson bool) error {
747764
sort.Slice(deals, func(i, j int) bool {
748765
return deals[i].CreationTime.Time().Before(deals[j].CreationTime.Time())
749766
})
750767

768+
if inJson {
769+
data, err := json.MarshalIndent(deals, "", " ")
770+
if err != nil {
771+
return err
772+
}
773+
_, err = fmt.Fprintln(out, string(data))
774+
return err
775+
}
776+
751777
w := tabwriter.NewWriter(out, 2, 4, 2, ' ', 0)
752778

753779
if verbose {

0 commit comments

Comments
 (0)