aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-cli/src/meshbay_cli/commands/groups.py
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-cli/src/meshbay_cli/commands/groups.py')
-rw-r--r--packages/meshbay-cli/src/meshbay_cli/commands/groups.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/packages/meshbay-cli/src/meshbay_cli/commands/groups.py b/packages/meshbay-cli/src/meshbay_cli/commands/groups.py
new file mode 100644
index 0000000..7595e2b
--- /dev/null
+++ b/packages/meshbay-cli/src/meshbay_cli/commands/groups.py
@@ -0,0 +1,69 @@
+"""
+`groups`: the groups this account belongs to, one line each.
+
+The hub answers alone (`/v1/groups/mine`); no node is dialled. Whether a node
+is up is what the hub sees of its socket, not a promise this machine can reach
+it. "owner" is the account that created the group on the hub — a node's
+operator is another notion.
+"""
+
+import json
+import sys
+from datetime import datetime
+
+from ..client.groups import my_groups
+from ..client.paths import handle
+from ..output import safe, sentence
+from .context import Context
+
+HELP = "list the groups you belong to"
+
+
+def add_parser(sub, parent) -> None:
+ sub.add_parser("groups", parents=[parent], help=HELP, description=sentence(HELP)
+ + ": the node's state, your role, the visibility, the last activity. "
+ "The GROUP column is the path `ls`, `get` and `put` take.")
+
+
+def node_state(g: dict) -> str:
+ if not g.get("hosted", True):
+ return "none"
+ return "up" if g.get("node_online") else "down"
+
+
+def _when(iso: str | None) -> str:
+ if not iso:
+ return ""
+ try:
+ return datetime.fromisoformat(iso).astimezone().strftime("%Y-%m-%d %H:%M")
+ except ValueError:
+ return ""
+
+
+async def run(ctx: Context) -> int:
+ await ctx.hub.check_version()
+ account = ctx.account()
+ try:
+ groups = await my_groups(account)
+ finally:
+ await account.close()
+ groups.sort(key=lambda g: (g["name"].lower(), g.get("owner_username") or ""))
+ if ctx.args.json:
+ json.dump([{"group": g["name"], "owner": g.get("owner_username"), "id": g["id"],
+ "path": handle(g), "node": node_state(g),
+ "role": "owner" if g.get("is_admin") else "member",
+ "visibility": g.get("visibility"), "join_policy": g.get("join_policy"),
+ "muted": bool(g.get("muted")), "description": g.get("description", ""),
+ "last_activity_at": g.get("last_activity_at")}
+ for g in groups], sys.stdout, indent=1)
+ print()
+ return 0
+ if not groups:
+ print("You belong to no group yet.", file=sys.stderr)
+ return 0
+ print(f"{'NODE':<6}{'ROLE':<8}{'VISIBILITY':<12}{'LAST ACTIVITY':<18}GROUP")
+ for g in groups:
+ role = "owner" if g.get("is_admin") else "member"
+ print(f"{node_state(g):<6}{role:<8}{safe(g.get('visibility') or ''):<12}"
+ f"{_when(g.get('last_activity_at')):<18}{safe(handle(g))}")
+ return 0