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

CLI console output behavior for --json and --verbose #313

Merged
merged 4 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
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
31 changes: 28 additions & 3 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,41 @@ func SetVersionJSON(versionJSON []byte) error {
}

func SetupErrorHandler() {
App.ExitErrHandler = func(c *cli.Context, err error) {
errHandler := func(c *cli.Context, err error) {
if err == nil {
return
}
if c.Bool("json") {
errMessage, err2 := json.MarshalIndent(map[string]string{
"err": err.Error(),
}, "", " ")
if err2 != nil {
panic(err2)
}
_, _ = App.Writer.Write(errMessage)
_, _ = App.Writer.Write([]byte("\n"))
} else {
concise := cliutil.Failure(err.Error()) + "\n"
_, _ = App.Writer.Write([]byte(concise))
}
if c.Bool("verbose") {
report := fmt.Sprintf("%+v\n\n", err)
_, _ = App.ErrWriter.Write([]byte(report))
}
concise := cliutil.Failure(err.Error()) + "\n"
_, _ = App.ErrWriter.Write([]byte(concise))
var exitCoder cli.ExitCoder
if errors.As(err, &exitCoder) {
os.Exit(exitCoder.ExitCode())
} else {
os.Exit(1)
}
}
App.ExitErrHandler = errHandler
App.OnUsageError = func(c *cli.Context, err error, isSubcommand bool) error {
errHandler(c, err)
return err
}
App.CommandNotFound = func(c *cli.Context, command string) {
errHandler(c, errors.Newf("command not found: %s", command))
}
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/cliutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package cliutil

import (
"encoding/json"
"fmt"
"strings"

"github.com/cockroachdb/errors"
"github.com/data-preservation-programs/table"
"github.com/fatih/color"
"github.com/ipfs/go-log/v2"
"github.com/urfave/cli/v2"
)

var logger = log.Logger("cliutil")

var ErrIncorrectNArgs = errors.New("incorrect number of arguments")

func Failure(msg string) string {
Expand Down Expand Up @@ -47,7 +49,7 @@ func HandleReallyDoIt(context *cli.Context) error {
func PrintAsJSON(c *cli.Context, obj any) {
objJSON, err := json.MarshalIndent(obj, "", " ")
if err != nil {
fmt.Println("Error: Unable to marshal object to JSON.")
logger.Errorw("Error: Unable to marshal object to JSON.", "err", err)
return
}
_, _ = c.App.Writer.Write(objJSON)
Expand Down
7 changes: 3 additions & 4 deletions cmd/deal/send-manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import (
)

var SendManualCmd = &cli.Command{
Name: "send-manual",
Usage: "Send a manual deal proposal to boost or legacy market",
ArgsUsage: "<client> <provider> <piece_cid> <piece_size>",
Name: "send-manual",
Usage: "Send a manual deal proposal to boost or legacy market",
Description: `Send a manual deal proposal to boost or legacy market
Example: singularity deal send-manual f01234 f05678 bagaxxxx 32GiB
Example: singularity deal send-manual --client f01234 --provider f05678 --piece-cid bagaxxxx --piece-size 32GiB
Notes:
* The client address must have been imported to the wallet using 'singularity wallet import'
* The deal proposal will not be saved in the database however will eventually be tracked if the deal tracker is running
Expand Down
4 changes: 2 additions & 2 deletions docs/en/cli-reference/deal/send-manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ NAME:
singularity deal send-manual - Send a manual deal proposal to boost or legacy market

USAGE:
singularity deal send-manual [command options] <client> <provider> <piece_cid> <piece_size>
singularity deal send-manual [command options] [arguments...]

DESCRIPTION:
Send a manual deal proposal to boost or legacy market
Example: singularity deal send-manual f01234 f05678 bagaxxxx 32GiB
Example: singularity deal send-manual --client f01234 --provider f05678 --piece-cid bagaxxxx --piece-size 32GiB
Notes:
* The client address must have been imported to the wallet using 'singularity wallet import'
* The deal proposal will not be saved in the database however will eventually be tracked if the deal tracker is running
Expand Down
5 changes: 4 additions & 1 deletion singularity.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,8 @@ func main() {
if err != nil {
log.Fatal(err)
}
_ = cmd.App.RunContext(context.TODO(), os.Args)
err = cmd.App.RunContext(context.TODO(), os.Args)
if err != nil {
log.Fatal(err)
}
}