Skip to content

Commit

Permalink
feat: new option to read replacements from a file (#18)
Browse files Browse the repository at this point in the history
feat: new option configFile to read replacements from a file
  • Loading branch information
imreACTmd authored Jan 7, 2022
1 parent cf2d6e8 commit a7c6441
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`):
Expand Down
42 changes: 32 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit a7c6441

Please sign in to comment.