mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-29 17:36:38 +08:00
refactor(ipfs2/main)
* bring debug checking back to top level so we have more control over CPU profiling. * bring help text up to top level so we can exit from the program at the top level instead of within an arbitrary function
This commit is contained in:

committed by
Juan Batiz-Benet

parent
2473d2d720
commit
13a90537d6
@ -32,32 +32,54 @@ const (
|
|||||||
var ofi io.WriteCloser
|
var ofi io.WriteCloser
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
err := run()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func run() error {
|
||||||
handleInterrupt()
|
handleInterrupt()
|
||||||
|
|
||||||
args := os.Args[1:]
|
args := os.Args[1:]
|
||||||
req, root, err := createRequest(args)
|
req, root, err := createRequest(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
return err
|
||||||
exit(1)
|
}
|
||||||
|
|
||||||
|
debug, err := req.Option("debug").Bool()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if debug {
|
||||||
|
u.Debug = true
|
||||||
|
u.SetAllLoggers(logging.DEBUG)
|
||||||
}
|
}
|
||||||
handleOptions(req, root)
|
|
||||||
|
|
||||||
// if debugging, setup profiling.
|
// if debugging, setup profiling.
|
||||||
if u.Debug {
|
if u.Debug {
|
||||||
var err error
|
var err error
|
||||||
ofi, err = os.Create("cpu.prof")
|
ofi, err = os.Create("cpu.prof")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pprof.StartCPUProfile(ofi)
|
pprof.StartCPUProfile(ofi)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
helpTextDisplayed, err := handleHelpOption(req, root)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if helpTextDisplayed {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
res := callCommand(req, root)
|
res := callCommand(req, root)
|
||||||
outputResponse(res, root)
|
outputResponse(res, root)
|
||||||
|
|
||||||
exit(0)
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
|
func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
|
||||||
@ -117,32 +139,22 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
|
|||||||
return req, root, nil
|
return req, root, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleOptions(req cmds.Request, root *cmds.Command) {
|
func handleHelpOption(req cmds.Request, root *cmds.Command) (helpTextDisplayed bool, err error) {
|
||||||
help, err := req.Option("help").Bool()
|
help, err := req.Option("help").Bool()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
return false, err
|
||||||
exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if help {
|
if help {
|
||||||
helpText, err := cmdsCli.HelpText("ipfs", root, req.Path())
|
helpText, err := cmdsCli.HelpText("ipfs", root, req.Path())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
return false, err
|
||||||
} else {
|
|
||||||
fmt.Println(helpText)
|
|
||||||
}
|
}
|
||||||
exit(0)
|
fmt.Println(helpText)
|
||||||
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
debug, err := req.Option("debug").Bool()
|
return false, nil
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
exit(1)
|
|
||||||
}
|
|
||||||
if debug {
|
|
||||||
u.Debug = true
|
|
||||||
u.SetAllLoggers(logging.DEBUG)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
|
func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
|
||||||
|
Reference in New Issue
Block a user