From 9ecbd3b664171fb8354142802a603f842cd9220b Mon Sep 17 00:00:00 2001 From: Mike Garde Date: Thu, 4 Jul 2024 22:05:50 -0400 Subject: [PATCH 1/2] Allowing no-json Allowing return of all values (not sure why but you can) --- Taskfile.yaml | 2 ++ src/app.ts | 16 +++++++++------- src/components/qualifyingRules.ts | 1 + src/services/handlers/getValue.ts | 4 ++++ 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Taskfile.yaml b/Taskfile.yaml index a98eff4..e6e036e 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -7,6 +7,8 @@ tasks: - task --list --sort alphanumeric tests: + aliases: + - test desc: Run tests deps: - build diff --git a/src/app.ts b/src/app.ts index ce24608..cca496a 100644 --- a/src/app.ts +++ b/src/app.ts @@ -27,6 +27,7 @@ async function app() { .argument('[key...]', 'Environment variable key') .option('-f, --file ', 'Specify the .env file (default: .env)') .option('-j, --json', 'Output as JSON') + .option('--no-json', 'Output as plain text') .option('-m, --multiline', 'Allow multiline values') .option('-s, --set ', 'Update the environment variable in the .env file') .option('-q, --quote', 'Quote the value when --set regardless of need') @@ -48,12 +49,6 @@ async function app() { const keys: string[] = program.args; const set: string = cliOptions.set; - // Multiple keys or no keys assume --json - if (keys.length > 1 || !keys.length) { - log.debug('Key count (0 or >1) defaulting to JSON'); - cliOptions.json = true; - } - // Determine if we are setting a value, and if so, what's the value let setValue: string = ''; if (stdin && set) { @@ -76,7 +71,8 @@ async function app() { let options: Options = { fullEnvPath: fullEnvPath, envObject: new EnvObject(), - json: (cliOptions.json !== undefined), + json: (process.argv.includes('--json')), // commander.js sets json instead of no-json + noJson: (process.argv.includes('--no-json')), // commander.js fails to parse this multiline: (cliOptions.multiline !== undefined), quote: (cliOptions.quote !== undefined), action: { @@ -88,6 +84,12 @@ async function app() { targetKeys: keys, setValue: escapeAndQuote(setValue, (cliOptions.quote !== undefined)), }; + + // Multiple keys or no keys assume --json + if ((keys.length > 1 || !keys.length) && !options['noJson']) { + log.debug('Key count (0 or >1) defaulting to JSON'); + options.json = true; + } log.debug('Options:', options); qualifyingRules(options); diff --git a/src/components/qualifyingRules.ts b/src/components/qualifyingRules.ts index 915f41a..7d58b1b 100644 --- a/src/components/qualifyingRules.ts +++ b/src/components/qualifyingRules.ts @@ -6,6 +6,7 @@ export interface Options { fullEnvPath: string; envObject: EnvObject; json: boolean; + noJson: boolean; multiline: boolean; quote: boolean; action: { diff --git a/src/services/handlers/getValue.ts b/src/services/handlers/getValue.ts index 9e3cac3..9a0d8b3 100644 --- a/src/services/handlers/getValue.ts +++ b/src/services/handlers/getValue.ts @@ -5,6 +5,10 @@ import {Options} from "../../components/qualifyingRules.js"; export default function getValue(options: Options) { let result: string = ''; + if (options.targetKeys.length === 0) { + options.targetKeys = Object.keys(options.envObject); + } + for (const key of options.targetKeys) { log.debug(`Getting "${key}"`); From 20ecb142983a598b1b1a7f440a8ff9593275d579 Mon Sep 17 00:00:00 2001 From: Mike Garde Date: Thu, 4 Jul 2024 22:56:55 -0400 Subject: [PATCH 2/2] Wildcard search --- README.md | 13 ++++++++++++- src/app.ts | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 39ecd87..9002f8a 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,21 @@ Get a value from a .env.example file: dotenv --file .env.example ``` +### JSON + +By default multiple keys are returned as a JSON object. To return a single key as a JSON object, use the `--json` flag. +To not return a JSON object, use the `--no-json` flag. + Return a .env file as JSON: ```shell -dotenv --json +dotenv +``` + +Wildcard search: + +```shell +dotenv "DB_*" ``` ### Multiline Values diff --git a/src/app.ts b/src/app.ts index cca496a..f949921 100644 --- a/src/app.ts +++ b/src/app.ts @@ -96,6 +96,25 @@ async function app() { options.envObject = parseEnvFile(envFilePath); + if (keys.length === 1 && !options['noJson']) { + log.debug('Single key, and not --no-json, checking for wildcard'); + + if (options.targetKeys[0].includes('*')) { + log.debug('Wildcard found') + const target: string = options.targetKeys[0].replace('*', '.*'); + const regex: RegExp = new RegExp('^' + target + '$'); + let i: number = 0; + for (const key in options.envObject) { + if (key.match(regex)) { + log.debug(`Adding "${key}" to targetKeys`); + options.targetKeys[i] = key; + i++; + } + } + options.json = true; + } + } + if (options.json && options.returnAllKeys) { log.debug('Outputting entire .env file as JSON'); log.info(options.envObject.toJsonString(options.multiline));