Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new option to read replacements from a file #18

Merged
merged 9 commits into from
Jan 7, 2022
Merged
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