diff --git a/README.md b/README.md index e2350a8..be02f81 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,13 @@ You can also specify the table for a column using the dot notation: public.user.email,public.product.description,email,name ``` +Alternatively use `--configFile` option to specify a file with a list of column names and optional replacements, one per line: + +```bash +npx pg-anonymizer postgres://localhost/mydb \ + --configFile /path/to/file +``` + #### Customize replacements You can also choose which faker function you want to use to replace data (default is `faker.random.word`): diff --git a/src/index.ts b/src/index.ts index d08c16e..b112380 100644 --- a/src/index.ts +++ b/src/index.ts @@ -33,9 +33,11 @@ class PgAnonymizer extends Command { help: flags.help({ char: "h" }), list: flags.string({ char: "l", - description: "list of columns to anonymize", - default: - "email,name,description,address,city,country,phone,comment,birthdate", + description: "[default: email,name,description,address,city,country,phone,comment,birthdate] list of columns to anonymize", + }), + configFile: flags.string({ + char: "c", + description: "config file with list of columns to anonymize", }), extension: flags.string({ char: "e", @@ -81,12 +83,32 @@ class PgAnonymizer extends Command { }); pg.stdout.setEncoding("utf8"); - const list = flags.list.split(",").map((l: string) => { - return { - col: l.replace(/:(?:.*)$/, "").toLowerCase(), - replacement: l.includes(":") ? l.replace(/^(?:.*):/, "") : null, - }; - }); + if (!(flags.list || flags.configFile)) { + flags.list = "email,name,description,address,city,country,phone,comment,birthdate"; + } + + let list: { col: string; replacement: string | null; }[]; + if (flags.configFile) { + list = fs.readFileSync(flags.configFile, "utf8") + .split(/\r?\n/) + .map((l: string) => l.trim()) + .map((l: string) => { + if (l === "") return null; + if (l.startsWith("#")) return null; + return { + col: l.replace(/:(?:.*)$/, "").toLowerCase(), + replacement: l.includes(":") ? l.replace(/^(?:.*):/, "") : null + }; + }) + .filter(Boolean); + } else if (flags.list) { + list = flags.list.split(",").map((l: string) => { + return { + col: l.replace(/:(?:.*)$/, "").toLowerCase(), + replacement: l.includes(":") ? l.replace(/^(?:.*):/, "") : null, + }; + }); + } let table: string | null = null; let indices: Number[] = []; @@ -126,7 +148,7 @@ class PgAnonymizer extends Command { cols.filter((v, k) => indices.includes(k)).join(", ") ); else console.log("No columns to anonymize"); - } else if (table && line.trim()) { + } else if (table && line.trim() && (line !== "\\.")) { line = line .split("\t") .map((v, k) => {