diff --git a/.env.example b/.env.example index 5a6939d..ef3a301 100644 --- a/.env.example +++ b/.env.example @@ -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= diff --git a/README.md b/README.md index 482f240..81b1a8a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/formats/avroFormat.ts b/src/formats/avroFormat.ts index a734c6b..0a1a608 100644 --- a/src/formats/avroFormat.ts +++ b/src/formats/avroFormat.ts @@ -10,7 +10,7 @@ export class AvroFormat implements OutputFormat { private registry: SchemaRegistry; static async create(): Promise { - 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); diff --git a/src/kafka/cleanKafka.ts b/src/kafka/cleanKafka.ts index f1f90d5..02c7d80 100644 --- a/src/kafka/cleanKafka.ts +++ b/src/kafka/cleanKafka.ts @@ -4,7 +4,7 @@ import alert from 'cli-alerts'; import { Env } from '../utils/env.js'; async function deleteSchemaSubjects(topics: any): Promise { - 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`; diff --git a/src/utils/env.ts b/src/utils/env.ts index 9fd274b..01b4c35 100644 --- a/src/utils/env.ts +++ b/src/utils/env.ts @@ -1,4 +1,5 @@ import dotenv from 'dotenv'; +import alert from 'cli-alerts'; export class Env { @@ -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); } @@ -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; } } \ No newline at end of file