@cryptotaxi247 / kubo / commits / afa5eedb4

cmds2: commandShouldRunOnDaemon

This commit adds the pretty-complicated decision function to check whether a command should run on the daemon. @maybebtc @mappum double check the logic?

Juan Batiz-Benet committed Nov 13, 2014 at 04:18 UTC afa5eedb44f9226ddba8bef70e107b9e84cc84e8
1 file changed +51 -6
cmd/ipfs2/main.go
+51 -6
@@ -202,15 +202,12 @@ func (i *cmdInvocation) requestedHelp() (short bool, long bool, err error) {
202 func callCommand(req cmds.Request, root *cmds.Command) (cmds.Response, error) {
203 var res cmds.Response
204
205 - local, found, err := req.Option("local").Bool()
205 + useDaemon, err := commandShouldRunOnDaemon(req, root)
206 if err != nil {
207 return nil, err
208 }
209
210 - remote := !isLocal(req.Command()) && (!found || !local)
211 -
212 - log.Info("Checking if daemon is running...")
213 - if remote && daemon.Locked(req.Context().ConfigRoot) {
210 + if useDaemon {
211
212 cfg, err := req.Context().GetConfig()
213 if err != nil {
@@ -222,6 +219,7 @@ func callCommand(req cmds.Request, root *cmds.Command) (cmds.Response, error) {
219 return nil, err
220 }
221
222 + log.Infof("Executing command on daemon running at %s", addr)
223 _, host, err := manet.DialArgs(addr)
224 if err != nil {
225 return nil, err
@@ -235,7 +233,7 @@ func callCommand(req cmds.Request, root *cmds.Command) (cmds.Response, error) {
233 }
234
235 } else {
238 - log.Info("Executing command locally: daemon not running")
236 + log.Info("Executing command locally")
237
238 // this sets up the function that will initialize the node
239 // this is so that we can construct the node lazily.
@@ -262,6 +260,53 @@ func callCommand(req cmds.Request, root *cmds.Command) (cmds.Response, error) {
260 return res, nil
261 }
262
263 +func commandShouldRunOnDaemon(req cmds.Request, root *cmds.Command) (bool, error) {
264 + path := req.Path()
265 + // root command.
266 + if len(path) < 1 {
267 + return false, nil
268 + }
269 +
270 + cmd, found := root.Subcommands[path[0]]
271 + if !found {
272 + return false, fmt.Errorf("subcommand %s should be in root", path[0])
273 + }
274 +
275 + details, found := cmdDetailsMap[cmd]
276 + if !found {
277 + details = cmdDetails{} // defaults
278 + }
279 +
280 + if details.cannotRunOnClient && details.cannotRunOnDaemon {
281 + return false, fmt.Errorf("command disabled: %s", path[0])
282 + }
283 +
284 + if details.doesNotUseRepo && !details.cannotRunOnClient {
285 + return false, nil
286 + }
287 +
288 + // at this point need to know whether daemon is running. we defer
289 + // to this point so that some commands dont open files unnecessarily.
290 + daemonLocked := daemon.Locked(req.Context().ConfigRoot)
291 + log.Info("Daemon is running.")
292 +
293 + if daemonLocked {
294 +
295 + if details.cannotRunOnDaemon {
296 + e := "ipfs daemon is running. please stop it to run this command"
297 + return false, cmds.ClientError(e)
298 + }
299 +
300 + return true, nil
301 + }
302 +
303 + if details.cannotRunOnClient {
304 + return false, cmds.ClientError("must run on the ipfs daemon")
305 + }
306 +
307 + return false, nil
308 +}
309 +
310 func isClientError(err error) bool {
311
312 // Somewhat suprisingly, the pointer cast fails to recognize commands.Error