define-alpine-the-sequel/helpers/passwd.ts

38 lines
994 B
TypeScript

import { readFile } from "node:fs/promises";
export interface PasswdEntry {
name: string;
password: string;
uid: number;
gid: number;
gecos: string;
directory: string;
shell: string;
}
export function parsePasswd(content: string): PasswdEntry[] {
const lines = content.split("\n");
return lines
.filter((line) => line.length > 0)
.map((line, index) => {
const values = line.split(":");
if (values.length !== 7)
throw new Error(
`La línea ${index + 1} no tiene la cantidad correcta de partes`
);
const entry: PasswdEntry = {
name: values[0],
password: values[1],
uid: parseInt(values[2]),
gid: parseInt(values[3]),
gecos: values[4],
directory: values[5],
shell: values[6],
};
return entry;
});
}
export async function readPasswd(path: string): Promise<PasswdEntry[]> {
const content = await readFile(path, "utf-8");
return parsePasswd(content);
}