summaryrefslogtreecommitdiffstats
path: root/packages/meshbay-node/src/meshbay_node/cli/dispatch.py
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-node/src/meshbay_node/cli/dispatch.py')
-rw-r--r--packages/meshbay-node/src/meshbay_node/cli/dispatch.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/packages/meshbay-node/src/meshbay_node/cli/dispatch.py b/packages/meshbay-node/src/meshbay_node/cli/dispatch.py
new file mode 100644
index 0000000..849d589
--- /dev/null
+++ b/packages/meshbay-node/src/meshbay_node/cli/dispatch.py
@@ -0,0 +1,68 @@
+"""From the command line to the verb that runs it."""
+
+import logging
+
+from meshbay_node.cli import content, groups, lifecycle, members, settings, setup, status
+from meshbay_node.cli.parser import build_parser
+from meshbay_node.platform import config_dir
+
+
+def start():
+ """Set up the process and read its command line."""
+ from meshbay_node.platform import configure_event_loop, force_utf8_stdio, load_node_env
+ force_utf8_stdio()
+ configure_event_loop()
+ # Before anything reads the environment. On Linux systemd has usually loaded
+ # the same file already via EnvironmentFile=; this is what makes a Windows
+ # run (Startup-folder .vbs, no systemd) and a bare `meshbay-node` behave the
+ # same. Already-set variables are left alone, so it cannot undo either.
+ load_node_env(config_dir())
+
+ parser = build_parser()
+ args = parser.parse_args()
+
+ # Query commands print a report; library logging would interleave with it.
+ quiet = args.command in ("status", "gek-init", "gek", "operator",
+ "member", "group", "root", "file", "video", "chat",
+ "denylist", "stun", "reload", "restart-daemon",
+ "reset")
+ logging.basicConfig(
+ level=logging.ERROR if quiet else getattr(logging, args.log_level),
+ format="%(asctime)s %(levelname)-8s %(name)s: %(message)s",
+ )
+ return args
+
+
+# Each verb and what runs it. A command line naming none of them starts the
+# daemon, which is daemon.main's to do.
+VERBS = {
+ "init": setup.init,
+ "reset": setup.reset,
+ "status": status.status,
+ "gek-init": groups.gek,
+ "gek": groups.gek,
+ "operator": members.operator,
+ "member": members.member,
+ "group": groups.group,
+ "root": groups.root,
+ "file": content.file,
+ "video": content.video,
+ "chat": content.chat,
+ "denylist": settings.denylist,
+ "stun": settings.stun,
+ "transfers": settings.transfers,
+ "reload": lifecycle.reload,
+ "restart-daemon": lifecycle.restart_daemon,
+ "autostart": lifecycle.autostart,
+ "service": lifecycle.service,
+ "calibrate-argon2": setup.calibrate,
+}
+
+
+def run(args) -> bool:
+ """Run the verb on the command line; False when it names none."""
+ verb = VERBS.get(args.command)
+ if verb is None:
+ return False
+ verb(args)
+ return True