@cryptotaxi247 / kubo / commits / 3d94e89dd

cmd/ipfs2: Made error messages more visible

Matt Bell committed Nov 8, 2014 at 20:13 UTC 3d94e89dd1a9177479ab9866b0ef8c4f4e12401b
1 file changed +24 -16
cmd/ipfs2/main.go
+24 -16
@@ -23,14 +23,17 @@ import (
23 // log is the command logger
24 var log = u.Logger("cmd/ipfs")
25
26 -const heapProfile = "ipfs.mprof"
26 +const (
27 + heapProfile = "ipfs.mprof"
28 + errorFormat = "ERROR: %v\n\n"
29 +)
30
31 func main() {
32 args := os.Args[1:]
33 req, root := createRequest(args)
34 handleOptions(req, root)
35 res := callCommand(req, root)
33 - outputResponse(res)
36 + outputResponse(res, root)
37
38 if u.Debug {
39 err := writeHeapProfileToFile()
@@ -57,17 +60,18 @@ func createRequest(args []string) (cmds.Request, *cmds.Command) {
60 // e.g. incorrect number of args, or nonexistent subcommand)
61 if err != nil {
62 // if the -help flag wasn't specified, show the error message
60 - if options != nil {
61 - opt, _ := options.Option("help")
62 - help, _ := opt.(bool)
63 - if !help {
64 - fmt.Println(err)
63 + // or if a path was returned (user specified a valid subcommand), show the error message
64 + // (this means there was an option or argument error)
65 + if options != nil || path != nil && len(path) > 0 {
66 + help := false
67 + if options != nil {
68 + opt, _ := options.Option("help")
69 + help, _ = opt.(bool)
70 }
71
67 - } else if path != nil && len(path) > 0 {
68 - // if a path was returned (user specified a valid subcommand), show the error message
69 - // (this means there was an option or argument error)
70 - fmt.Println(err)
72 + if !help {
73 + fmt.Printf(errorFormat, err)
74 + }
75 }
76
77 // 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 {
226 return res
227 }
228
225 -func outputResponse(res cmds.Response) {
229 +func outputResponse(res cmds.Response, root *cmds.Command) {
230 if res.Error() != nil {
227 - fmt.Println(res.Error().Error())
231 + fmt.Printf(errorFormat, res.Error().Error())
232
229 - if res.Request().Command().Help != "" && res.Error().Code == cmds.ErrClient {
230 - // TODO: convert from markdown to ANSI terminal format?
231 - fmt.Println(res.Request().Command().Help)
233 + if res.Error().Code == cmds.ErrClient {
234 + helpText, err := cmdsCli.HelpText("ipfs", root, res.Request().Path())
235 + if err != nil {
236 + fmt.Println(err.Error())
237 + } else {
238 + fmt.Println(helpText)
239 + }
240 }
241
242 os.Exit(1)