Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/ouput deal with json format #497

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading