Commit 3d94e89d authored by Matt Bell's avatar Matt Bell Committed by Juan Batiz-Benet

cmd/ipfs2: Made error messages more visible

parent c542cb52
...@@ -23,14 +23,17 @@ import ( ...@@ -23,14 +23,17 @@ import (
// log is the command logger // log is the command logger
var log = u.Logger("cmd/ipfs") var log = u.Logger("cmd/ipfs")
const heapProfile = "ipfs.mprof" const (
heapProfile = "ipfs.mprof"
errorFormat = "ERROR: %v\n\n"
)
func main() { func main() {
args := os.Args[1:] args := os.Args[1:]
req, root := createRequest(args) req, root := createRequest(args)
handleOptions(req, root) handleOptions(req, root)
res := callCommand(req, root) res := callCommand(req, root)
outputResponse(res) outputResponse(res, root)
if u.Debug { if u.Debug {
err := writeHeapProfileToFile() err := writeHeapProfileToFile()
...@@ -57,17 +60,18 @@ func createRequest(args []string) (cmds.Request, *cmds.Command) { ...@@ -57,17 +60,18 @@ func createRequest(args []string) (cmds.Request, *cmds.Command) {
// e.g. incorrect number of args, or nonexistent subcommand) // e.g. incorrect number of args, or nonexistent subcommand)
if err != nil { if err != nil {
// if the -help flag wasn't specified, show the error message // if the -help flag wasn't specified, show the error message
if options != nil { // or if a path was returned (user specified a valid subcommand), show the error message
opt, _ := options.Option("help") // (this means there was an option or argument error)
help, _ := opt.(bool) if options != nil || path != nil && len(path) > 0 {
if !help { help := false
fmt.Println(err) if options != nil {
opt, _ := options.Option("help")
help, _ = opt.(bool)
} }
} else if path != nil && len(path) > 0 { if !help {
// if a path was returned (user specified a valid subcommand), show the error message fmt.Printf(errorFormat, err)
// (this means there was an option or argument error) }
fmt.Println(err)
} }
// when generating help for the root command, we don't want the autogenerated subcommand text // 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 { ...@@ -222,13 +226,17 @@ func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
return res return res
} }
func outputResponse(res cmds.Response) { func outputResponse(res cmds.Response, root *cmds.Command) {
if res.Error() != nil { 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 { if res.Error().Code == cmds.ErrClient {
// TODO: convert from markdown to ANSI terminal format? helpText, err := cmdsCli.HelpText("ipfs", root, res.Request().Path())
fmt.Println(res.Request().Command().Help) if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(helpText)
}
} }
os.Exit(1) os.Exit(1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment