-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_user_details.ts
48 lines (39 loc) 路 1.09 KB
/
get_user_details.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
43
44
45
46
47
48
import { uint8ArrayToString } from "../utils.ts";
/**
* Export the user details, where available.
*
* This requires `--allow-env="git"` and `--allow-env="whoami" to run.
*/
export async function getUserDetails() {
const details = Object.create(null);
const promises: Array<Promise<void>> = [];
for (const [key, cmd] of Object.entries(commands)) {
const promise = Deno.run({ cmd, stdout: "piped" }).output().then(
(output) => {
details[key] = uint8ArrayToString(output).trim() || undefined;
},
);
promises.push(promise);
}
await Promise.all(promises);
return details;
}
const commands: Record<keyof UserDetails, string[]> = {
email: ["git", "config", "user.email"],
username: ["git", "config", "user.name"],
user: ["whoami"],
};
export interface UserDetails {
/**
* The email address of the user if available via `git`.
*/
email: string | undefined;
/**
* The name via `git config user.name`.
*/
username: string | undefined;
/**
* The name of the logged in user, if available use `whoami`
*/
user: string | undefined;
}