Skip to content

Commit

Permalink
add debug messages for environment variables and show exports in .env…
Browse files Browse the repository at this point in the history
….example so users can more easily run datagen from a different directory from the .env file (#102)
  • Loading branch information
chuck-alt-delete authored Jun 15, 2023
1 parent eeeb497 commit f5de274
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 29 deletions.
20 changes: 10 additions & 10 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# Kafka
KAFKA_BROKERS=
export KAFKA_BROKERS=

# Kafka SASL Authentication
SASL_USERNAME=
SASL_PASSWORD=
SASL_MECHANISM=
export SASL_USERNAME=
export SASL_PASSWORD=
export SASL_MECHANISM=

# Kafka SSL Authentication
SSL_CA_LOCATION=
SSL_CERT_LOCATION=
SSL_KEY_LOCATION=
export SSL_CA_LOCATION=
export SSL_CERT_LOCATION=
export SSL_KEY_LOCATION=

# Schema Registry if producing Avro
SCHEMA_REGISTRY_URL=
SCHEMA_REGISTRY_USERNAME=
SCHEMA_REGISTRY_PASSWORD=
export SCHEMA_REGISTRY_URL=
export SCHEMA_REGISTRY_USERNAME=
export SCHEMA_REGISTRY_PASSWORD=

# Postgres
export POSTGRES_HOST=
Expand Down
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,33 @@ Create a file called `.env` with the following environment variables

```bash
# Kafka Brokers
KAFKA_BROKERS=
export KAFKA_BROKERS=

# For Kafka SASL Authentication:
SASL_USERNAME=
SASL_PASSWORD=
SASL_MECHANISM=
export SASL_USERNAME=
export SASL_PASSWORD=
export SASL_MECHANISM=

# For Kafka SSL Authentication:
SSL_CA_LOCATION=
SSL_CERT_LOCATION=
SSL_KEY_LOCATION=
export SSL_CA_LOCATION=
export SSL_CERT_LOCATION=
export SSL_KEY_LOCATION=

# Connect to Schema Registry if using '--format avro'
SCHEMA_REGISTRY_URL=
SCHEMA_REGISTRY_USERNAME=
SCHEMA_REGISTRY_PASSWORD=
export SCHEMA_REGISTRY_URL=
export SCHEMA_REGISTRY_USERNAME=
export SCHEMA_REGISTRY_PASSWORD=

# Postgres
POSTGRES_HOST=
POSTGRES_PORT=
POSTGRES_DB=
POSTGRES_USER=
POSTGRES_PASSWORD=
export POSTGRES_HOST=
export POSTGRES_PORT=
export POSTGRES_DB=
export POSTGRES_USER=
export POSTGRES_PASSWORD=
```

The `datagen` program will read the environment variables from `.env` in the current working directory.
If you are running `datagen` from a different directory, you can first `source /path/to/your/.env` before running the command.


## Usage
Expand Down
2 changes: 1 addition & 1 deletion src/formats/avroFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class AvroFormat implements OutputFormat {
private registry: SchemaRegistry;

static async create(): Promise<AvroFormat> {
const url = Env.required("SCHEMA_REGISTRY_URL");
const url = Env.optional("SCHEMA_REGISTRY_URL", "http://localhost:8081");
const username = Env.optional("SCHEMA_REGISTRY_USERNAME", null);
const password = Env.optional("SCHEMA_REGISTRY_PASSWORD", null);

Expand Down
2 changes: 1 addition & 1 deletion src/kafka/cleanKafka.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import alert from 'cli-alerts';
import { Env } from '../utils/env.js';

async function deleteSchemaSubjects(topics: any): Promise<void> {
const schemaRegistryUrl = Env.required("SCHEMA_REGISTRY_URL");
const schemaRegistryUrl = Env.optional("SCHEMA_REGISTRY_URL", "http://localhost:8081");

for await (const topic of topics) {
const url = `${schemaRegistryUrl}/subjects/${topic}-value?permanent=false`;
Expand Down
22 changes: 20 additions & 2 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dotenv from 'dotenv';
import alert from 'cli-alerts';

export class Env {

Expand All @@ -11,7 +12,10 @@ export class Env {
if (!value) {
alert({
type: `error`,
name: `Missing required environment variable ${name}`
name:
`Missing required environment variable ${name}\n
Provide environment variable inline or export it to your environment.
`
});
process.exit(1);
}
Expand All @@ -20,6 +24,20 @@ export class Env {
}

static optional(name: string, orElse: string): string {
return process.env[name] ?? orElse;
const value = process.env[name];
if (!value && global.debug) {
alert({
type: `warning`,
name: `Environment variables`,
msg:
`\nEnvironment variable ${name} not found.
Using default alternative.
If you meant to include this environment variable, export it or
run this command from a directory with a .env file that includes it.
See https://github.com/MaterializeInc/datagen/ for more detail.
`
})
}
return value ?? orElse;
}
}

0 comments on commit f5de274

Please sign in to comment.