aboutsummaryrefslogtreecommitdiff
path: root/src/commands/groups.js
diff options
context:
space:
mode:
authorLily <[email protected]>2026-07-28 16:12:21 +1000
committerGitHub <[email protected]>2026-07-28 16:12:21 +1000
commitc11e17573334dfb487a8e1cc25fa84fa9e138f5c (patch)
treee91dbd19b524f9432da6bcb86b1fc193e0d02e26 /src/commands/groups.js
parent6a2f098e72e71437154ec00506e8e736aa8e1026 (diff)
downloadbnu-c11e17573334dfb487a8e1cc25fa84fa9e138f5c.tar.gz
bnu-c11e17573334dfb487a8e1cc25fa84fa9e138f5c.zip
Refactor coreutils into single-call command modules (#1)
* Refactor to single call
Diffstat (limited to 'src/commands/groups.js')
-rw-r--r--src/commands/groups.js38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/commands/groups.js b/src/commands/groups.js
new file mode 100644
index 0000000..45f2a76
--- /dev/null
+++ b/src/commands/groups.js
@@ -0,0 +1,38 @@
+#!/usr/bin/env bun
+
+import { userInfo } from "node:os";
+import { groupName, helpVersionOnlyMetaOption, localeQuotedDiagnostic, normalizeHelpVersionOnlyLongOptions, orderedGroupIds, parseOptions, readPasswd, supplementaryGroupsForUser, userNameForUid } from "../shared/common.js";
+import { stderr, stdout } from "../shared/diagnostics.js";
+import { defineCommand, runAsMain } from "../shared/command.js";
+
+export async function groupsCmd(args) {
+ args = normalizeHelpVersionOnlyLongOptions(args);
+ const { operands } = parseOptions(args, { long: { help: false, version: false } });
+ const info = userInfo();
+ const uid = process.getuid?.() ?? info.uid;
+ const gid = process.getgid?.() ?? info.gid;
+ const currentName = await userNameForUid(uid) ?? info.username;
+ if (!operands.length) {
+ const groups = orderedGroupIds(gid, process.getgroups?.() ?? [gid]);
+ stdout(`${(await Promise.all(groups.map(groupName))).join(" ")}\n`);
+ return 0;
+ }
+ let code = 0;
+ const passwd = await readPasswd();
+ for (const user of operands) {
+ const row = passwd.get(user);
+ if (!row) {
+ stderr(`groups: ${localeQuotedDiagnostic(user)}: no such user\n`);
+ code = 1;
+ continue;
+ }
+ const groups = orderedGroupIds(row.gid, await supplementaryGroupsForUser(user));
+ stdout(`${user} : ${(await Promise.all(groups.map(groupName))).join(" ")}\n`);
+ }
+ return code;
+}
+
+const singleCall = defineCommand("groups", groupsCmd, helpVersionOnlyMetaOption);
+export default singleCall;
+
+if (import.meta.main) await runAsMain(singleCall);