-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.ts
42 lines (38 loc) · 1.13 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import {
getEnvironmentVariable,
cleanWindowsCommand,
exec,
makeUsernameFromId,
} from "./utils.ts";
/**
* Get the username of the current user.
*
* It first tries to get the username from the `SUDO_USER` `LOGNAME` `USER` `LNAME` `USERNAME`
* environment variables.
*
* Then falls back to `$ id -un` on macOS / Linux and `$ whoami` on Windows,
* in the rare case none of the environment variables are set.
*
* Requires the `--allow-env` and `--allow-run` flags.
*
* @returns The username.
*/
export default async function username(): Promise<string | undefined> {
const envVariable = getEnvironmentVariable();
if (envVariable) {
return envVariable;
}
// First we try to get the ID of the user and then the actual username.
// We do this because in `docker run --user <uid>:<gid>` context,
// we don't have "username" available.
try {
if (Deno.build.os === "windows") {
return cleanWindowsCommand(await exec(["whoami"]));
}
const userId = await exec(["id", "-u"]);
try {
return await exec(["id", "-un", userId]);
} catch (_) {}
return makeUsernameFromId(userId);
} catch (_) {}
}