mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 10:49:24 +08:00
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?
This commit is contained in:
@ -202,15 +202,12 @@ func (i *cmdInvocation) requestedHelp() (short bool, long bool, err error) {
|
|||||||
func callCommand(req cmds.Request, root *cmds.Command) (cmds.Response, error) {
|
func callCommand(req cmds.Request, root *cmds.Command) (cmds.Response, error) {
|
||||||
var res cmds.Response
|
var res cmds.Response
|
||||||
|
|
||||||
local, found, err := req.Option("local").Bool()
|
useDaemon, err := commandShouldRunOnDaemon(req, root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
remote := !isLocal(req.Command()) && (!found || !local)
|
if useDaemon {
|
||||||
|
|
||||||
log.Info("Checking if daemon is running...")
|
|
||||||
if remote && daemon.Locked(req.Context().ConfigRoot) {
|
|
||||||
|
|
||||||
cfg, err := req.Context().GetConfig()
|
cfg, err := req.Context().GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -222,6 +219,7 @@ func callCommand(req cmds.Request, root *cmds.Command) (cmds.Response, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Infof("Executing command on daemon running at %s", addr)
|
||||||
_, host, err := manet.DialArgs(addr)
|
_, host, err := manet.DialArgs(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -235,7 +233,7 @@ func callCommand(req cmds.Request, root *cmds.Command) (cmds.Response, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
log.Info("Executing command locally: daemon not running")
|
log.Info("Executing command locally")
|
||||||
|
|
||||||
// this sets up the function that will initialize the node
|
// this sets up the function that will initialize the node
|
||||||
// this is so that we can construct the node lazily.
|
// 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) {
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func commandShouldRunOnDaemon(req cmds.Request, root *cmds.Command) (bool, error) {
|
||||||
|
path := req.Path()
|
||||||
|
// root command.
|
||||||
|
if len(path) < 1 {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd, found := root.Subcommands[path[0]]
|
||||||
|
if !found {
|
||||||
|
return false, fmt.Errorf("subcommand %s should be in root", path[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
details, found := cmdDetailsMap[cmd]
|
||||||
|
if !found {
|
||||||
|
details = cmdDetails{} // defaults
|
||||||
|
}
|
||||||
|
|
||||||
|
if details.cannotRunOnClient && details.cannotRunOnDaemon {
|
||||||
|
return false, fmt.Errorf("command disabled: %s", path[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
if details.doesNotUseRepo && !details.cannotRunOnClient {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// at this point need to know whether daemon is running. we defer
|
||||||
|
// to this point so that some commands dont open files unnecessarily.
|
||||||
|
daemonLocked := daemon.Locked(req.Context().ConfigRoot)
|
||||||
|
log.Info("Daemon is running.")
|
||||||
|
|
||||||
|
if daemonLocked {
|
||||||
|
|
||||||
|
if details.cannotRunOnDaemon {
|
||||||
|
e := "ipfs daemon is running. please stop it to run this command"
|
||||||
|
return false, cmds.ClientError(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if details.cannotRunOnClient {
|
||||||
|
return false, cmds.ClientError("must run on the ipfs daemon")
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
func isClientError(err error) bool {
|
func isClientError(err error) bool {
|
||||||
|
|
||||||
// Somewhat suprisingly, the pointer cast fails to recognize commands.Error
|
// Somewhat suprisingly, the pointer cast fails to recognize commands.Error
|
||||||
|
Reference in New Issue
Block a user