Skip to content

Commit

Permalink
Merge pull request #497 from ipfs-force-community/feat/ouput-deal-wit…
Browse files Browse the repository at this point in the history
…h-json-format

Feat/ouput deal with json format
  • Loading branch information
simlecode authored Dec 5, 2023
2 parents dd49bab + 3a5e4cd commit 98a40eb
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions cli/storage-deals.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ part states:
Name: "oldest",
Usage: "sort by oldest first",
},
&cli.BoolFlag{
Name: "json",
Usage: "output deal info as json format",
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := NewMarketNode(cctx)
Expand Down Expand Up @@ -451,7 +455,7 @@ part states:
tm.Clear()
tm.MoveCursor(1, 1)

err = outputStorageDeals(tm.Output, deals, verbose)
err = outputStorageDeals(tm.Output, deals, verbose, cctx.Bool("json"))
if err != nil {
return err
}
Expand All @@ -477,7 +481,7 @@ part states:
}
}

return outputStorageDeals(os.Stdout, deals, verbose)
return outputStorageDeals(os.Stdout, deals, verbose, cctx.Bool("json"))
},
}

Expand Down Expand Up @@ -635,6 +639,10 @@ var getDealCmd = &cli.Command{
Name: "proposal-cid",
Usage: "cid of deal proposal",
},
&cli.BoolFlag{
Name: "json",
Usage: "output deal info as json format",
},
},
Action: func(cliCtx *cli.Context) error {
if !cliCtx.IsSet("deal-id") && !cliCtx.IsSet("proposal-cid") {
Expand Down Expand Up @@ -682,6 +690,15 @@ var getDealCmd = &cli.Command{
}
}

if cliCtx.Bool("json") {
data, err := json.MarshalIndent(deal, "", " ")
if err != nil {
return err
}
fmt.Println(string(data))
return nil
}

return outputStorageDeal(deal)
},
}
Expand Down Expand Up @@ -743,11 +760,20 @@ func printStates(data interface{}) error {
return nil
}

func outputStorageDeals(out io.Writer, deals []market.MinerDeal, verbose bool) error {
func outputStorageDeals(out io.Writer, deals []market.MinerDeal, verbose bool, inJson bool) error {
sort.Slice(deals, func(i, j int) bool {
return deals[i].CreationTime.Time().Before(deals[j].CreationTime.Time())
})

if inJson {
data, err := json.MarshalIndent(deals, "", " ")
if err != nil {
return err
}
_, err = fmt.Fprintln(out, string(data))
return err
}

w := tabwriter.NewWriter(out, 2, 4, 2, ' ', 0)

if verbose {
Expand Down

0 comments on commit 98a40eb

Please sign in to comment.