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

Environment Variable fixes #102

Merged
merged 2 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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=

bobbyiliev marked this conversation as resolved.
Show resolved Hide resolved
# 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;
}
}