1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 19:01:34 +08:00

cmd/ipfs2: Made error messages more visible

This commit is contained in:
Matt Bell
2014-11-08 20:13:20 -08:00
committed by Juan Batiz-Benet
parent c542cb52aa
commit 3d94e89dd1

View File

@ -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) {
// 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
// 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 { if options != nil {
opt, _ := options.Option("help") opt, _ := options.Option("help")
help, _ := opt.(bool) help, _ = opt.(bool)
if !help {
fmt.Println(err)
} }
} 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 {
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)