denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
97.77k stars 5.38k forks source link

Proposal: add `Deno.getUserFullName()` #13506

Open ghost opened 2 years ago

ghost commented 2 years ago

This function allows you to view the GECOS or "Full/Display Name" of the account.

I'm not sure if it should be Deno.getUserFullName, Deno.getUserGecos or Deno.getUserDisplayName

It might be a long name so suggestions appreciated. I actually already did this in this snippet, but this should probably be better as a Deno built-in function:

const getFullName = async (username: string, fullNameDefaultUsername = false, passwdPath: PathLike = '/etc/passwd'): Promise<string | void> => {
  if (Deno.build.os === 'windows') {
    const fullNameProcess = await Deno.run({
      cmd: ['wmic', 'useraccount', 'where', `name='${username}'`, 'get', 'fullname'],
      stdout: 'piped'
    });

    if ((!fullName) && fullNameDefaultUsername) { fullName = username; }

    return username
  } else if (Deno.build.os === 'linux' || Deno.build.os === 'darwin') {
    const decoder = new TextDecoder();
    const file = await Deno.readFile(passwdPath);
    const data = decoder.decode(file);

    const passwdLines = data.split('\n');
    passwdLines.shift();
    for (const line of passwdLines) {
      const parts = line.split(':');
      if (parts[0] === username) {
        return parts[4]
      }
    }
  }
};
stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

ghost commented 2 years ago

I hate this bot. (why am I not able to reopen an issue??? I'm literally the one who made it?????)