1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-29 01:12:24 +08:00

refactor(ipfs2/main) output response

This commit is contained in:
Brian Tiger Chow
2014-11-10 22:58:47 -08:00
committed by Juan Batiz-Benet
parent cda68a19d0
commit fa5ca3f27f

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -80,7 +81,11 @@ func run() error {
if err != nil { if err != nil {
return err return err
} }
outputResponse(res, root)
err = outputResponse(res, root)
if err != nil {
return err
}
return nil return nil
} }
@ -205,10 +210,15 @@ func callCommand(req cmds.Request, root *cmds.Command) (cmds.Response, error) {
return res, nil return res, nil
} }
func outputResponse(res cmds.Response, root *cmds.Command) { func outputResponse(res cmds.Response, root *cmds.Command) error {
if res.Error() != nil { if res.Error() != nil {
fmt.Printf(errorFormat, res.Error().Error()) fmt.Printf(errorFormat, res.Error().Error())
if res.Error().Code != cmds.ErrClient {
return res.Error()
}
// if this is a client error, we try to display help text
if res.Error().Code == cmds.ErrClient { if res.Error().Code == cmds.ErrClient {
helpText, err := cmdsCli.HelpText("ipfs", root, res.Request().Path()) helpText, err := cmdsCli.HelpText("ipfs", root, res.Request().Path())
if err != nil { if err != nil {
@ -218,16 +228,17 @@ func outputResponse(res cmds.Response, root *cmds.Command) {
} }
} }
exit(1) emptyErr := errors.New("") // already displayed error text, but want to exit(1)
return emptyErr
} }
out, err := res.Reader() out, err := res.Reader()
if err != nil { if err != nil {
fmt.Println(err.Error()) return err
return
} }
io.Copy(os.Stdout, out) io.Copy(os.Stdout, out)
return nil
} }
func getConfigRoot(req cmds.Request) (string, error) { func getConfigRoot(req cmds.Request) (string, error) {