@kaynooo/cli-env 0.14.0 → 0.15.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-env",
3
3
  "type": "module",
4
- "version": "0.14.0",
5
- "description": "ev lists and edits .env files with secrets masked, redact masks secrets in any piped output.",
4
+ "version": "0.15.2",
5
+ "description": "ev lists and edits .env files with secrets masked, and ev redact masks secrets in any piped output.",
6
6
  "author": "Kaynooo <contact@kaynooo.fr>",
7
7
  "license": "MIT",
8
- "homepage": "https://git.kaynooo.fr/kaynooo/cli/src/branch/main/packages/env#readme",
8
+ "homepage": "https://github.com/Kayno0o/cli/tree/main/packages/env#readme",
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git+https://git.kaynooo.fr/kaynooo/cli.git",
11
+ "url": "git+https://github.com/Kayno0o/cli.git",
12
12
  "directory": "packages/env"
13
13
  },
14
- "bugs": "https://git.kaynooo.fr/kaynooo/cli/issues",
14
+ "bugs": "https://github.com/Kayno0o/cli/issues",
15
15
  "keywords": ["env", "dotenv", "secrets", "redact", "mask"],
16
16
  "exports": {
17
17
  "./dotenv": "./src/dotenv.ts",
@@ -21,8 +21,7 @@
21
21
  "./redactor": "./src/redactor.ts"
22
22
  },
23
23
  "bin": {
24
- "ev": "./dist/ev.js",
25
- "redact": "./dist/redact.js"
24
+ "ev": "./dist/ev.js"
26
25
  },
27
26
  "files": ["dist", "src"],
28
27
  "engines": {
@@ -32,11 +31,15 @@
32
31
  "registry": "https://git.kaynooo.fr/api/packages/kaynooo/npm/"
33
32
  },
34
33
  "scripts": {
35
- "build": "$npm_execpath build src/ev.ts src/redact.ts --target bun --outdir dist",
36
- "ev": "$npm_execpath src/ev.ts",
37
- "redact": "$npm_execpath src/redact.ts"
34
+ "build": "$npm_execpath build src/ev.ts --target bun --outdir dist",
35
+ "ev": "$npm_execpath src/ev.ts"
36
+ },
37
+ "dependencies": {
38
+ "@kaynooo/cli-kit": "0.46.0"
38
39
  },
39
40
  "devDependencies": {
40
- "@kaynooo/cli-kit": "0.40.0"
41
+ "@decimalturn/toml-patch": "3.3.1",
42
+ "jsonc-parser": "3.3.1",
43
+ "yaml": "2.9.1"
41
44
  }
42
45
  }
@@ -9,11 +9,10 @@ import { shellQuote } from '@kaynooo/cli-kit/shell'
9
9
  import { audit as findExposed } from '../audit'
10
10
  import { fixCompose, shutOthersOut } from '../fix'
11
11
 
12
- const usage = `ev audit [dir...] [--fix]
12
+ const usage = `ev audit [dir...] [--fix]: lists the .env files any account can read or git tracks and the compose files writing a secret out, never printing a value
13
13
 
14
- Lists the .env files any account on the machine can read or git tracks, and the compose files any account can read that write a secret out (a secret-named value or a URL password) instead of reading it from a .env, never printing a value.
15
- Looks under each folder (the current one by default) down to 5 levels, skipping node_modules, vendor and .git, and leaves .example, .sample, .template and .dist files out. A folder it cannot read is named on stderr.
16
- With --fix, moves each secret a listed compose file writes out into the .env beside it as \${KEY}, puts it back when \`docker compose config\` resolves differently after, and shuts other accounts out of both files and of every listed .env; a key the .env already sets to another value stays put.`
14
+ It looks under each folder (the current one by default) down to 5 levels, skipping \`node_modules\`, \`vendor\` and \`.git\`, and leaves \`.example\`, \`.sample\`, \`.template\` and \`.dist\` files out.
15
+ With --fix, each secret a listed compose file writes out moves into the .env beside it as \`\${KEY}\` and goes back when \`docker compose config\` resolves differently after; a key the .env already sets to another value stays put.`
17
16
 
18
17
  const EXITS: ExitRow[] = [[0, 'it finds none (with --fix, when none is left)'], [1, 'it finds one'], [2, 'a usage error']]
19
18
 
@@ -1,16 +1,17 @@
1
1
  import type { ExitRow } from '@kaynooo/cli-kit'
2
2
  import process from 'node:process'
3
3
  import { fail, jsonOption, parse, printJson, role } from '@kaynooo/cli-kit'
4
- import { entries } from '../dotenv'
5
- import { readEnv } from '../files'
4
+ import { loadKnown, readEntries } from '../files'
5
+ import { display } from './read'
6
6
 
7
7
  const diffUsage = `ev diff <a> <b>
8
8
 
9
- Names the keys set in one file and not the other, never their values.
9
+ Names the keys set in one file and not the other, and those both set to different values, each value masked as ev list does.
10
10
 
11
- --json prints {a, b, onlyA, onlyB}.`
11
+ --keys compares the keys only.
12
+ --json prints {a, b, onlyA, onlyB, differ}, differ naming the keys only.`
12
13
 
13
- const DIFF_EXITS: ExitRow[] = [[0, 'both set the same keys'], [1, 'they differ'], [2, 'a usage error']]
14
+ const DIFF_EXITS: ExitRow[] = [[0, 'both set the same keys to the same values'], [1, 'they differ'], [2, 'a usage error']]
14
15
 
15
16
  const missingUsage = `ev missing [file]
16
17
 
@@ -23,33 +24,42 @@ const MISSING_EXITS: ExitRow[] = [[0, 'none is missing'], [1, 'some are'], [2, '
23
24
  const jsonFlag = { json: jsonOption() } as const
24
25
 
25
26
  async function keys(path: string): Promise<Set<string>> {
26
- return new Set(entries(await readEnv(path)).keys())
27
+ return new Set((await readEntries(path)).keys())
27
28
  }
28
29
 
30
+ // Running ev reads both files already, and `ev redact` tells whether a string is one of their secrets, so naming a value as changed gives nothing away.
29
31
  export async function diff(args: string[]): Promise<number> {
30
- const parsed = parse(args, diffUsage, jsonFlag, { exits: DIFF_EXITS })
32
+ const parsed = parse(args, diffUsage, { keys: { type: 'boolean', description: 'compare the keys only' }, ...jsonFlag }, { exits: DIFF_EXITS })
31
33
 
32
34
  const [a, b] = parsed.positionals
33
35
 
34
36
  if (a === undefined || b === undefined)
35
37
  fail(`needs two files\n\n${diffUsage}`)
36
38
 
37
- const left = await keys(a)
38
- const right = await keys(b)
39
- const onlyA = [...left].filter(key => !right.has(key))
40
- const onlyB = [...right].filter(key => !left.has(key))
39
+ const left = await readEntries(a)
40
+ const right = await readEntries(b)
41
+ const onlyA = [...left.keys()].filter(key => !right.has(key))
42
+ const onlyB = [...right.keys()].filter(key => !left.has(key))
43
+ const differ = parsed.values.keys === true ? [] : [...left].filter(([key, value]) => right.has(key) && right.get(key) !== value).map(([key]) => key)
44
+ const same = onlyA.length + onlyB.length + differ.length === 0
41
45
 
42
46
  if (parsed.values.json === true) {
43
- printJson({ a, b, onlyA, onlyB })
47
+ printJson({ a, b, onlyA, onlyB, differ })
44
48
 
45
- return onlyA.length + onlyB.length === 0 ? 0 : 1
49
+ return same ? 0 : 1
46
50
  }
47
51
 
48
52
  const path = role('path', process.stdout)
49
53
  const value = role('value', process.stdout)
50
- const lines = [...onlyA.map(key => `only in ${path(a)}: ${value(key)}`), ...onlyB.map(key => `only in ${path(b)}: ${value(key)}`)]
51
-
52
- if (lines.length === 0)
54
+ const known = differ.length ? await loadKnown(process.cwd(), [left, right]) : undefined
55
+ const shown = (key: string, raw: string) => raw === '' ? 'empty' : display(key, raw, known)
56
+ const lines = [
57
+ ...onlyA.map(key => `only in ${path(a)}: ${value(key)}`),
58
+ ...onlyB.map(key => `only in ${path(b)}: ${value(key)}`),
59
+ ...differ.map(key => `differs: ${value(key)} (${shown(key, left.get(key)!)} in ${path(a)}, ${shown(key, right.get(key)!)} in ${path(b)})`),
60
+ ]
61
+
62
+ if (same)
53
63
  return 0
54
64
 
55
65
  console.log(lines.join('\n'))
@@ -1,58 +1,81 @@
1
1
  import type { ExitRow } from '@kaynooo/cli-kit'
2
2
  import { badUsage, fail, isInteractive, parse, readInput } from '@kaynooo/cli-kit'
3
3
  import { input } from '@kaynooo/cli-kit/prompt'
4
- import { entries, lastLine, removeKey, setLine } from '../dotenv'
5
- import { loadKnown, readEnv, withLocks, writeEnv } from '../files'
4
+ import { entries, formatAssignment, lastLine, removeKey, setLine, unquotable } from '../dotenv'
5
+ import { assertDotenvName, loadKnown, readEnv, withLocks, writeAtomic, writeEnv } from '../files'
6
6
  import { maskValue } from '../mask'
7
+ import { parseStructured, structuredFormat } from '../structured'
8
+ import { setStructured, unsetStructured } from '../structured-write'
7
9
 
8
10
  const DEFAULT_FILE = '.env'
9
11
  const TRAILING_NEWLINE = /\r?\n$/
10
12
  const KEY_NAME = /^[A-Z_][\w.-]*$/i
13
+ // eslint-disable-next-line no-control-regex
14
+ const CONTROL = /[\u0000-\u001F]/
11
15
 
12
16
  const QUIET = { quiet: { type: 'boolean', short: 'q', description: 'print nothing on success' } } as const
13
17
 
14
18
  const setUsage = `ev set KEY [file] [--quiet]
15
19
 
16
- Sets KEY in the file (.env by default, created when missing), replacing its line in place or appending one. The value is read from stdin, or from -F <file>, so it never appears in a command line; on a terminal with nothing piped it is asked for, masked as it is typed. --empty sets it to an empty value.`
20
+ Sets KEY in the file (.env by default, created when missing), replacing its line in place or appending one. The value is read from stdin, or from -F <file>, so it never appears in a command line; on a terminal with nothing piped it is asked for, masked as it is typed. --empty sets it to an empty value. The value is written in the quoting Bun's --env-file and compose's env_file both read back unchanged, and refused when none does, as for one holding both a dollar sign and a backslash.
21
+
22
+ In a JSON, YAML or TOML file that exists, KEY is a dotted path (database.password, servers.0.host), and only its value changes: comments, order, indentation and the value's quoting stay, a number or boolean stays one when the value reads as one, and an edit that would change anything else is refused.`
17
23
 
18
24
  const SET_EXITS: ExitRow[] = [[0, 'it wrote the key'], [1, 'it could not'], [2, 'a usage error'], [130, 'the prompt was cancelled']]
19
25
 
20
26
  const unsetUsage = `ev unset KEY [file] [--quiet]
21
27
 
22
- Removes every line setting KEY from the file (.env by default), leaving comments and other lines as they are.`
28
+ Removes every line setting KEY from the file (.env by default), leaving comments and other lines as they are. In a JSON, YAML or TOML file KEY is a dotted path to a value, removed with its line.`
23
29
 
24
30
  const UNSET_EXITS: ExitRow[] = [[0, 'it removed the key'], [1, 'the file does not set it'], [2, 'a usage error']]
25
31
 
26
32
  const mvUsage = `ev mv KEY --from <file> --to <file> [--quiet]
27
33
 
28
- Moves KEY's line from one file to another, as written. Refuses to replace a key the target already sets unless --force.`
34
+ Moves KEY's line from one file to another, as written. Refuses to replace a key the target already sets unless --force, and with --force a value no quoting keeps for both Bun and compose, as ev set does.`
29
35
 
30
- const MV_EXITS: ExitRow[] = [[0, 'it moved the key'], [1, 'the source lacks it or the target already sets it'], [2, 'a usage error']]
36
+ const MV_EXITS: ExitRow[] = [[0, 'it moved the key'], [1, 'the source lacks it, the target already sets it, or its value cannot be rewritten'], [2, 'a usage error']]
31
37
 
32
38
  const cpUsage = `ev cp KEY --from <file> --to <file> [--as NEWKEY] [--quiet]
33
39
 
34
- Copies KEY's value from one file to another without printing it, replacing the key's line in the target in place or appending one, and prints the masked result as ev set does. --as stores the copy under another key.`
40
+ Copies KEY's value from one file to another without printing it, replacing the key's line in the target in place or appending one, and prints the masked result as ev set does. --as stores the copy under another key. A value it has to rewrite is refused when no quoting keeps it for both Bun and compose, as ev set does.`
35
41
 
36
- const CP_EXITS: ExitRow[] = [[0, 'it copied the key'], [1, 'the source lacks it'], [2, 'a usage error']]
42
+ const CP_EXITS: ExitRow[] = [[0, 'it copied the key'], [1, 'the source lacks it or its value cannot be rewritten'], [2, 'a usage error']]
37
43
 
38
44
  async function readValue(key: string, from: string | undefined): Promise<string> {
39
45
  const value = from === undefined && isInteractive()
40
46
  ? await input(`value of ${key}`, { mask: true }) ?? fail('cancelled, nothing was written', 130)
41
47
  : await readInput(from ?? '-', { noStdin: 'pipe the value on stdin or give -F <file>, it is never taken from the command line' })
42
48
 
43
- if (value === '')
49
+ const stripped = value.replace(TRAILING_NEWLINE, '')
50
+
51
+ if (stripped === '')
44
52
  fail('the value is empty, nothing was written: --empty sets an empty value')
45
53
 
46
- return value.replace(TRAILING_NEWLINE, '')
54
+ return stripped
47
55
  }
48
56
 
49
- // A key that is not a plain name could carry a line break or an `=` and write more than one line.
50
- function checkKey(key: string): void {
57
+ // A key that is not a plain name could carry a line break or an `=` and write more than one line. A JSON, YAML or TOML file quotes its keys, so a dotted path there only needs each part named.
58
+ function checkKey(key: string, target: string): void {
59
+ if (structuredFormat(target) !== undefined) {
60
+ if (key.split('.').includes('') || CONTROL.test(key))
61
+ badUsage(`${JSON.stringify(key)} is not a path of keys: names joined by dots, none empty`)
62
+
63
+ return
64
+ }
65
+
51
66
  if (!KEY_NAME.test(key))
52
67
  badUsage(`${JSON.stringify(key)} is not a valid key: letters, digits, _, . and - only, not starting with a digit`)
53
68
  }
54
69
 
70
+ async function readStructured(path: string): Promise<string> {
71
+ const file = Bun.file(path)
72
+
73
+ return await file.exists() ? file.text() : fail(`${path} does not exist: ev writes into a JSON, YAML or TOML file that is there`, 1)
74
+ }
75
+
55
76
  async function readOrEmpty(path: string) {
77
+ assertDotenvName(path)
78
+
56
79
  return await Bun.file(path).exists() ? readEnv(path) : { lines: [] }
57
80
  }
58
81
 
@@ -71,10 +94,28 @@ export async function set(args: string[]): Promise<number> {
71
94
  if (parsed.values.empty === true && parsed.values.file !== undefined)
72
95
  fail('--empty and -F exclude each other')
73
96
 
74
- checkKey(key)
97
+ checkKey(key, target)
75
98
 
76
99
  const value = parsed.values.empty === true ? '' : await readValue(key, parsed.values.file)
77
100
 
101
+ if (structuredFormat(target) !== undefined) {
102
+ const text = await withLocks([target], async () => {
103
+ const edited = setStructured(target, await readStructured(target), key, value)
104
+
105
+ await writeAtomic(target, edited)
106
+
107
+ return edited
108
+ })
109
+
110
+ if (parsed.values.quiet !== true)
111
+ console.log(`${target}: ${key}=${maskValue(key, value, await loadKnown(process.cwd(), [parseStructured(target, text)])).replaceAll('\n', '\\n')}`)
112
+
113
+ return 0
114
+ }
115
+
116
+ if (formatAssignment(key, value) === undefined)
117
+ fail(`${unquotable(value)}, nothing was written`, 1)
118
+
78
119
  const file = await withLocks([target], async () => {
79
120
  const current = await readOrEmpty(target)
80
121
 
@@ -85,7 +126,7 @@ export async function set(args: string[]): Promise<number> {
85
126
  })
86
127
 
87
128
  if (parsed.values.quiet !== true)
88
- console.log(`${target}: ${key}=${maskValue(key, value, await loadKnown(process.cwd(), [file])).replaceAll('\n', '\\n')}`)
129
+ console.log(`${target}: ${key}=${maskValue(key, value, await loadKnown(process.cwd(), [entries(file)])).replaceAll('\n', '\\n')}`)
89
130
 
90
131
  return 0
91
132
  }
@@ -98,7 +139,16 @@ export async function unset(args: string[]): Promise<number> {
98
139
  if (key === undefined)
99
140
  fail(`missing KEY\n\n${unsetUsage}`)
100
141
 
142
+ if (structuredFormat(target) !== undefined)
143
+ checkKey(key, target)
144
+
101
145
  await withLocks([target], async () => {
146
+ if (structuredFormat(target) !== undefined) {
147
+ await writeAtomic(target, unsetStructured(target, await readStructured(target), key) ?? fail(`${key} is not set in ${target}`, 1))
148
+
149
+ return
150
+ }
151
+
102
152
  const file = await readEnv(target)
103
153
 
104
154
  if (removeKey(file, key) === 0)
@@ -115,10 +165,10 @@ export async function unset(args: string[]): Promise<number> {
115
165
 
116
166
  // The write path of mv and cp: puts KEY's line from `from` into `to`, as `newKey` when given, and returns both files for the caller to finish.
117
167
  async function transfer(key: string, from: string, to: string, newKey: string | undefined, replace: boolean) {
118
- checkKey(key)
168
+ checkKey(key, to)
119
169
 
120
170
  if (newKey !== undefined)
121
- checkKey(newKey)
171
+ checkKey(newKey, to)
122
172
 
123
173
  if (from === to && newKey === undefined)
124
174
  fail('--from and --to are the same file')
@@ -136,8 +186,12 @@ async function transfer(key: string, from: string, to: string, newKey: string |
136
186
  fail(`${to} already sets ${name}, --force replaces it`, 1)
137
187
 
138
188
  const value = line.value ?? ''
189
+ const raw = newKey === undefined && !entries(target).has(name) ? line.raw : undefined
190
+
191
+ if (raw === undefined && formatAssignment(name, value) === undefined)
192
+ fail(`${key} in ${from}: ${unquotable(value)}, nothing was written`, 1)
139
193
 
140
- setLine(target, name, value, newKey === undefined && !entries(target).has(name) ? line.raw : undefined)
194
+ setLine(target, name, value, raw)
141
195
  await writeEnv(to, target)
142
196
 
143
197
  return { source, target, value, name }
@@ -187,7 +241,7 @@ export async function cp(args: string[]): Promise<number> {
187
241
  const { source, target, value, name } = await withLocks([from, to], async () => transfer(key, from, to, as, true))
188
242
 
189
243
  if (parsed.values.quiet !== true)
190
- console.log(`${to}: ${name}=${maskValue(name, value, await loadKnown(process.cwd(), [source, target])).replaceAll('\n', '\\n')}`)
244
+ console.log(`${to}: ${name}=${maskValue(name, value, await loadKnown(process.cwd(), [entries(source), entries(target)])).replaceAll('\n', '\\n')}`)
191
245
 
192
246
  return 0
193
247
  }
@@ -8,10 +8,8 @@ import { checkApp } from '../pins'
8
8
  const usage = `ev pins [dir] [--except NAME,...]
9
9
 
10
10
  Checks that an app's tests pin the env that reaches outside.
11
- Reads the app's env schema (the file calling defineEnv, src/env.ts) and lists its optional URL, key, token, secret, password and host variables, then the ones no \`.env.test\` entry, test-level overrideEnv or local bunfig preload dropping what .env sets pins, and each test file that calls testSite or proxyKeyTests without pinning them all.
12
- A file whose pages reach none of them says so with a \`// ev pins: none reached, <why>\` comment.
13
- The dir is an app, or a monorepo holding apps one level down (the current folder by default).
14
- Reads variable names only, never a value.`
11
+ Lists the optional URL, key, token, secret, password and host variables of the app's \`defineEnv\` schema that no \`.env.test\` entry, \`overrideEnv\` or bunfig preload pins, and each test file calling \`testSite\` or \`proxyKeyTests\` without pinning them all; a file reaching none says so with a \`// ev pins: none reached, <why>\` comment.
12
+ The dir is an app, or a monorepo holding apps one level down (the current folder by default). It reads variable names only, never a value.`
15
13
 
16
14
  const EXITS: ExitRow[] = [[0, 'everything is pinned'], [1, 'something is not'], [2, 'a usage error']]
17
15
 
@@ -1,15 +1,15 @@
1
1
  import type { ExitRow } from '@kaynooo/cli-kit'
2
- import type { Dotenv } from '../dotenv'
3
2
  import type { KnownSecrets } from '../mask'
4
3
  import { fail, jsonOption, parse, printJson, role, table } from '@kaynooo/cli-kit'
5
4
  import { $ } from 'bun'
6
5
  import { entries, parseDotenv } from '../dotenv'
7
- import { envFiles, loadKnown, readEnv } from '../files'
6
+ import { assertDotenvText, assertReadableName, envFiles, loadKnown, readEntries } from '../files'
8
7
  import { isSecretKey, maskValue } from '../mask'
8
+ import { parseStructured, structuredFormat } from '../structured'
9
9
 
10
10
  const listUsage = `ev list [file]
11
11
 
12
- Prints every key of the folder's .env* files, or of one file, with secret values masked: a few leading characters and the length under a key named like TOKEN, SECRET, PASSWORD, PASS, PWD, KEY, PRIVATE, DSN, AUTH, SALT or COOKIE, any value equal to a secret of the folder's .env* files, and the password and tokens of any URL.
12
+ Prints every key of the folder's .env* files, or of one file, with secret values masked to a few leading characters and the length. A JSON, YAML or TOML file names its keys as dotted paths (\`database.password\`, \`servers.0.host\`).
13
13
 
14
14
  --json prints [{file, key, value, secret}], value masked and secret true when a value was.`
15
15
 
@@ -17,13 +17,13 @@ const LIST_EXITS: ExitRow[] = [[0, 'it printed them'], [1, 'there is no .env* fi
17
17
 
18
18
  const getUsage = `ev get KEY [file]
19
19
 
20
- Prints the masked value of one key, from every .env* file that sets it or from one file.
20
+ Prints the masked value of one key, from every .env* file that sets it or from one file: \`ev get database.password config.yaml\`.
21
21
 
22
22
  --json prints [{file, key, value, secret}] for each file that sets it.`
23
23
 
24
24
  const GET_EXITS: ExitRow[] = [[0, 'the key is set'], [1, 'it is not set anywhere'], [2, 'a usage error']]
25
25
 
26
- function display(key: string, value: string, known: KnownSecrets): string {
26
+ export function display(key: string, value: string, known?: KnownSecrets): string {
27
27
  return maskValue(key, value, known).replaceAll('\n', '\\n').replaceAll('\r', '\\r')
28
28
  }
29
29
 
@@ -60,11 +60,11 @@ function printRows(rows: Row[], json: boolean, plain: () => void): void {
60
60
  }
61
61
 
62
62
  // Reads each file once and learns the secrets of the folder and of these files, so a value is masked whatever key holds it.
63
- async function readAll(names: string[]): Promise<{ files: [name: string, file: Dotenv][], known: KnownSecrets }> {
64
- const files: [string, Dotenv][] = []
63
+ async function readAll(names: string[], scanned: boolean): Promise<{ files: [name: string, keys: Map<string, string>][], known: KnownSecrets }> {
64
+ const files: [string, Map<string, string>][] = []
65
65
 
66
66
  for (const name of names)
67
- files.push([name, await readEnv(name)])
67
+ files.push([name, await readEntries(name, { scanned })])
68
68
 
69
69
  return { files, known: await loadKnown(process.cwd(), files.map(([, file]) => file)) }
70
70
  }
@@ -80,10 +80,10 @@ export async function list(args: string[]): Promise<number> {
80
80
 
81
81
  const rows: Row[] = []
82
82
 
83
- const { files, known } = await readAll(names)
83
+ const { files, known } = await readAll(names, file === undefined)
84
84
 
85
- for (const [name, file] of files) {
86
- for (const [key, value] of entries(file))
85
+ for (const [name, keys] of files) {
86
+ for (const [key, value] of keys)
87
87
  rows.push(row(name, key, value, known))
88
88
  }
89
89
 
@@ -106,10 +106,10 @@ export async function get(args: string[]): Promise<number> {
106
106
  const names = file === undefined ? await envFiles() : [file]
107
107
  const rows: Row[] = []
108
108
 
109
- const { files, known } = await readAll(names)
109
+ const { files, known } = await readAll(names, file === undefined)
110
110
 
111
- for (const [name, file] of files) {
112
- const value = entries(file).get(key)
111
+ for (const [name, keys] of files) {
112
+ const value = keys.get(key)
113
113
 
114
114
  if (value !== undefined)
115
115
  rows.push(row(name, key, value, known))
@@ -161,10 +161,10 @@ export async function grep(args: string[]): Promise<number> {
161
161
  const names = given.length === 0 ? await envFiles() : given
162
162
  const rows: Row[] = []
163
163
 
164
- const { files, known } = await readAll(names)
164
+ const { files, known } = await readAll(names, given.length === 0)
165
165
 
166
- for (const [name, file] of files) {
167
- for (const [key, value] of entries(file)) {
166
+ for (const [name, keys] of files) {
167
+ for (const [key, value] of keys) {
168
168
  if (pattern.test(key))
169
169
  rows.push(row(name, key, value, known))
170
170
  }
@@ -178,6 +178,14 @@ export async function grep(args: string[]): Promise<number> {
178
178
  return 0
179
179
  }
180
180
 
181
+ function dotenvKeys(name: string, text: string, redact: string): Map<string, string> {
182
+ const file = parseDotenv(text)
183
+
184
+ assertDotenvText(name, file, redact)
185
+
186
+ return entries(file)
187
+ }
188
+
181
189
  export async function show(args: string[]): Promise<number> {
182
190
  const parsed = parse(args, showUsage, jsonFlag, { exits: SHOW_EXITS })
183
191
 
@@ -190,14 +198,18 @@ export async function show(args: string[]): Promise<number> {
190
198
  if (revision.startsWith('-'))
191
199
  fail(`${revision} is not a revision: it reads as a git option`)
192
200
 
201
+ const redact = `git show ${revision}:${path} | ev redact`
202
+
203
+ assertReadableName(path, redact)
204
+
193
205
  const shown = await $`git show --end-of-options ${`${revision}:${path}`}`.quiet().nothrow()
194
206
 
195
207
  if (shown.exitCode !== 0)
196
208
  fail(`git show ${revision}:${path} failed: ${shown.stderr.toString().trim()}`, 1)
197
209
 
198
- const file = parseDotenv(shown.stdout.toString())
199
- const known = await loadKnown(process.cwd(), [file])
200
- const rows = [...entries(file)].map(([key, value]) => row(target ?? '', key, value, known))
210
+ const keys = structuredFormat(path) === undefined ? dotenvKeys(target ?? '', shown.stdout.toString(), redact) : parseStructured(target ?? '', shown.stdout.toString())
211
+ const known = await loadKnown(process.cwd(), [keys])
212
+ const rows = [...keys].map(([key, value]) => row(target ?? '', key, value, known))
201
213
 
202
214
  printRows(rows, parsed.values.json === true, () => {
203
215
  if (rows.length > 0)
@@ -4,10 +4,10 @@ import { parse, readInput } from '@kaynooo/cli-kit'
4
4
  import { loadKnown } from '../files'
5
5
  import { check, Redactor } from '../redactor'
6
6
 
7
- const usage = `redact [--check]
7
+ const usage = `ev redact [--check]
8
8
 
9
- Copies stdin to stdout line by line with secrets masked: known token formats (GitHub, GitLab, Slack, AWS, npm, Stripe, Google and SendGrid keys, sk-, JWTs, Authorization headers, password= and secret= pairs), private key blocks, the password and the tokens of any URL and the values of secret-looking keys in this folder's .env* files.
10
- --check prints nothing of the input and lists the file and line of each secret found, exiting 1 when there is any: \`git diff --cached | redact --check\`.`
9
+ Copies stdin to stdout line by line with secrets masked: known token formats, private key blocks, the password and tokens of any URL and the values of secret-looking keys in this folder's .env* files.
10
+ --check prints nothing of the input and lists the file and line of each secret found, exiting 1 when there is any: \`git diff --cached | ev redact --check\`.`
11
11
 
12
12
  const EXITS: ExitRow[] = [[0, 'it copied the input or found no secret'], [1, '--check finds one'], [2, 'a usage error']]
13
13
 
@@ -17,7 +17,7 @@ const options = {
17
17
  } as const
18
18
 
19
19
  export async function redact(args: string[]): Promise<number> {
20
- const parsed = parse(args, usage, options, { exits: EXITS, replaces: 'printing a command\'s secrets into a log or a transcript', examples: [['docker compose config | redact', 'the resolved compose file, secrets masked'], ['git diff --cached | redact --check', 'refuse a commit carrying a secret']] })
20
+ const parsed = parse(args, usage, options, { exits: EXITS, replaces: 'printing a command\'s secrets into a log or a transcript', examples: [['docker compose config | ev redact', 'the resolved compose file, secrets masked'], ['git diff --cached | ev redact --check', 'refuse a commit carrying a secret']] })
21
21
 
22
22
  const known = parsed.values['no-env'] === true ? undefined : await loadKnown()
23
23