mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 02:30:39 +08:00
cmd/ipfs2: Made error messages more visible
This commit is contained in:

committed by
Juan Batiz-Benet

parent
c542cb52aa
commit
3d94e89dd1
@ -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)
|
||||
|
Reference in New Issue
Block a user