From 3d94e89dd1a9177479ab9866b0ef8c4f4e12401b Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Sat, 8 Nov 2014 20:13:20 -0800 Subject: [PATCH] cmd/ipfs2: Made error messages more visible --- cmd/ipfs2/main.go | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/cmd/ipfs2/main.go b/cmd/ipfs2/main.go index 2660b5b49..11d7c9622 100644 --- a/cmd/ipfs2/main.go +++ b/cmd/ipfs2/main.go @@ -23,14 +23,17 @@ import ( // log is the command logger var log = u.Logger("cmd/ipfs") -const heapProfile = "ipfs.mprof" +const ( + heapProfile = "ipfs.mprof" + errorFormat = "ERROR: %v\n\n" +) func main() { args := os.Args[1:] req, root := createRequest(args) handleOptions(req, root) res := callCommand(req, root) - outputResponse(res) + outputResponse(res, root) if u.Debug { err := writeHeapProfileToFile() @@ -57,17 +60,18 @@ func createRequest(args []string) (cmds.Request, *cmds.Command) { // e.g. incorrect number of args, or nonexistent subcommand) if err != nil { // if the -help flag wasn't specified, show the error message - if options != nil { - opt, _ := options.Option("help") - help, _ := opt.(bool) - if !help { - fmt.Println(err) + // or if a path was returned (user specified a valid subcommand), show the error message + // (this means there was an option or argument error) + if options != nil || path != nil && len(path) > 0 { + help := false + if options != nil { + opt, _ := options.Option("help") + help, _ = opt.(bool) } - } else if path != nil && len(path) > 0 { - // if a path was returned (user specified a valid subcommand), show the error message - // (this means there was an option or argument error) - fmt.Println(err) + if !help { + fmt.Printf(errorFormat, err) + } } // when generating help for the root command, we don't want the autogenerated subcommand text @@ -222,13 +226,17 @@ func callCommand(req cmds.Request, root *cmds.Command) cmds.Response { return res } -func outputResponse(res cmds.Response) { +func outputResponse(res cmds.Response, root *cmds.Command) { if res.Error() != nil { - fmt.Println(res.Error().Error()) + fmt.Printf(errorFormat, res.Error().Error()) - if res.Request().Command().Help != "" && res.Error().Code == cmds.ErrClient { - // TODO: convert from markdown to ANSI terminal format? - fmt.Println(res.Request().Command().Help) + if res.Error().Code == cmds.ErrClient { + helpText, err := cmdsCli.HelpText("ipfs", root, res.Request().Path()) + if err != nil { + fmt.Println(err.Error()) + } else { + fmt.Println(helpText) + } } os.Exit(1)