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
Brian Tiger Chow committed
Nov 10, 2014 at 22:40 UTC
13a90537d615cff1996cb5a78f58aa7cc2f15b0e
1 file changed
+34
-22
cmd/ipfs2/main.go
+34
-22
@@ -32,32 +32,54 @@ const (
32
var ofi io.WriteCloser
33
34
func main() {
35
+ err := run()
36
+ if err != nil {
37
+ fmt.Println(err)
38
+ os.Exit(1)
39
+ }
40
+}
41
+
42
+func run() error {
43
handleInterrupt()
44
45
args := os.Args[1:]
46
req, root, err := createRequest(args)
47
if err != nil {
40
- fmt.Println(err)
41
- exit(1)
48
+ return err
49
+ }
50
+
51
+ debug, err := req.Option("debug").Bool()
52
+ if err != nil {
53
+ return err
54
+ }
55
+ if debug {
56
+ u.Debug = true
57
+ u.SetAllLoggers(logging.DEBUG)
58
}
43
- handleOptions(req, root)
59
60
// if debugging, setup profiling.
61
if u.Debug {
62
var err error
63
ofi, err = os.Create("cpu.prof")
64
if err != nil {
50
- fmt.Println(err)
51
- return
65
+ return err
66
}
67
68
pprof.StartCPUProfile(ofi)
69
}
70
71
+ helpTextDisplayed, err := handleHelpOption(req, root)
72
+ if err != nil {
73
+ return err
74
+ }
75
+ if helpTextDisplayed {
76
+ return nil
77
+ }
78
+
79
res := callCommand(req, root)
80
outputResponse(res, root)
81
60
- exit(0)
82
+ return nil
83
}
84
85
func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
@@ -117,32 +139,22 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
139
return req, root, nil
140
}
141
120
-func handleOptions(req cmds.Request, root *cmds.Command) {
142
+func handleHelpOption(req cmds.Request, root *cmds.Command) (helpTextDisplayed bool, err error) {
143
help, err := req.Option("help").Bool()
144
if err != nil {
123
- fmt.Println(err)
124
- exit(1)
145
+ return false, err
146
}
147
148
if help {
149
helpText, err := cmdsCli.HelpText("ipfs", root, req.Path())
150
if err != nil {
130
- fmt.Println(err.Error())
131
- } else {
132
- fmt.Println(helpText)
151
+ return false, err
152
}
134
- exit(0)
153
+ fmt.Println(helpText)
154
+ return true, nil
155
}
156
137
- debug, err := req.Option("debug").Bool()
138
- if err != nil {
139
- fmt.Println(err)
140
- exit(1)
141
- }
142
- if debug {
143
- u.Debug = true
144
- u.SetAllLoggers(logging.DEBUG)
145
- }
157
+ return false, nil
158
}
159
160
func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {