Skip to content

Commit

Permalink
feat: output can be stdout so you can pipe it to other commands (#19)
Browse files Browse the repository at this point in the history
feat: output now can be sent to stdout so you can pipe it to commands
  • Loading branch information
imreACTmd authored Feb 1, 2022
1 parent a7c6441 commit 11e46ce
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ npx pg-anonymizer postgres://user:secret@localhost:1234/mydb -o dump.sql

☝️ This command requires `pg_dump`. It may already be installed as soon as PostgreSQL is installed.

Output can also be stdout ('-') so you can pipe the output to zip, gz, or to psql:

```bash
npx pg-anonymizer postgres://user:secret@localhost:1234/mydb -o - | psql DATABASE_URL
```

### Specify list of columns to anonymize

Use `--list` option with a comma separated list of column name:
Expand Down
22 changes: 14 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class PgAnonymizer extends Command {
char: "m",
description:
"Obsolete, not needed any more: max memory used to get output from pg_dump in MB",
default: "0",
}),
};

Expand All @@ -71,7 +70,7 @@ class PgAnonymizer extends Command {
? require(path.join(process.cwd(), flags.extension))
: null;

console.log("Launching pg_dump");
console.error("Launching pg_dump");
const pg = spawn("pg_dump", [args.database]);
pg.on("exit", function (code) {
if (code != 0) {
Expand Down Expand Up @@ -114,9 +113,16 @@ class PgAnonymizer extends Command {
let indices: Number[] = [];
let cols: string[] = [];

console.log("Command pg_dump started, running anonymization.");
console.log("Output file: " + flags.output);
let out = fs.createWriteStream(flags.output);
console.error("Command pg_dump started, running anonymization.");

let out: any;
if (flags.output === "-") {
out = process.stdout;
console.error("Output to stdout");
} else {
out = fs.createWriteStream(flags.output);
console.error("Output file: " + flags.output);
}

const inputLineResults = readline.createInterface({
input: pg.stdout,
Expand All @@ -126,7 +132,7 @@ class PgAnonymizer extends Command {
for await (let line of inputLineResults) {
if (line.match(/^COPY .* FROM stdin;$/)) {
table = line.replace(/^COPY (.*?) .*$/, "$1");
console.log("Anonymizing table " + table);
console.error("Anonymizing table " + table);

cols = line
.replace(/^COPY (?:.*?) \((.*)\).*$/, "$1")
Expand All @@ -143,11 +149,11 @@ class PgAnonymizer extends Command {
}, []);

if (indices.length)
console.log(
console.error(
"Columns to anonymize: " +
cols.filter((v, k) => indices.includes(k)).join(", ")
);
else console.log("No columns to anonymize");
else console.error("No columns to anonymize");
} else if (table && line.trim() && (line !== "\\.")) {
line = line
.split("\t")
Expand Down

0 comments on commit 11e46ce

Please sign in to comment.