@cryptotaxi247 / kubo / commits / cda68a19d

refactor(ipfs2/main) return err

Brian Tiger Chow committed Nov 10, 2014 at 22:45 UTC cda68a19d01abf3b6eb91df0b02edf4a2672de47
1 file changed +12 -14
cmd/ipfs2/main.go
+12 -14
@@ -76,7 +76,10 @@ func run() error {
76 return nil
77 }
78
79 - res := callCommand(req, root)
79 + res, err := callCommand(req, root)
80 + if err != nil {
81 + return err
82 + }
83 outputResponse(res, root)
84
85 return nil
@@ -157,45 +160,40 @@ func handleHelpOption(req cmds.Request, root *cmds.Command) (helpTextDisplayed b
160 return false, nil
161 }
162
160 -func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
163 +func callCommand(req cmds.Request, root *cmds.Command) (cmds.Response, error) {
164 var res cmds.Response
165
163 - if root == Root {
166 + if root == Root { // TODO explain what it means when root == Root
167 res = root.Call(req)
168
169 } else {
170 local, err := req.Option("local").Bool()
171 if err != nil {
169 - fmt.Println(err)
170 - exit(1)
172 + return nil, err
173 }
174
175 if (!req.Option("local").Found() || !local) && daemon.Locked(req.Context().ConfigRoot) {
176 addr, err := ma.NewMultiaddr(req.Context().Config.Addresses.API)
177 if err != nil {
176 - fmt.Println(err)
177 - exit(1)
178 + return nil, err
179 }
180
181 _, host, err := manet.DialArgs(addr)
182 if err != nil {
182 - fmt.Println(err)
183 - exit(1)
183 + return nil, err
184 }
185
186 client := cmdsHttp.NewClient(host)
187
188 res, err = client.Send(req)
189 if err != nil {
190 - fmt.Println(err)
191 - exit(1)
190 + return nil, err
191 }
192
193 } else {
194 node, err := core.NewIpfsNode(req.Context().Config, false)
195 if err != nil {
197 - fmt.Println(err)
198 - exit(1)
196 + return nil, err
197 }
198 defer node.Close()
199 req.Context().Node = node
@@ -204,7 +202,7 @@ func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
202 }
203 }
204
207 - return res
205 + return res, nil
206 }
207
208 func outputResponse(res cmds.Response, root *cmds.Command) {