@kaynooo/cli-env 0.8.0 → 0.9.0
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/README.md +6 -4
- package/dist/ev.js +515 -177
- package/dist/redact.js +394 -117
- package/package.json +7 -4
- package/src/audit.ts +167 -0
- package/src/commands/audit.ts +135 -0
- package/src/commands/compare.ts +77 -0
- package/src/commands/edit.ts +181 -0
- package/src/commands/pins.ts +98 -0
- package/src/commands/read.ts +193 -0
- package/src/dotenv.ts +8 -4
- package/src/ev.ts +31 -0
- package/src/files.ts +242 -0
- package/src/fix.ts +294 -0
- package/src/guard-dumps.ts +114 -0
- package/src/guard-names.ts +107 -0
- package/src/guard-paths.ts +80 -0
- package/src/guard-pipes.ts +76 -0
- package/src/guard-search.ts +181 -0
- package/src/guard.ts +51 -0
- package/src/mask.ts +264 -0
- package/src/pins.ts +268 -0
- package/src/redact.ts +43 -0
- package/src/redactor.ts +166 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-env",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.9.0",
|
|
5
5
|
"description": "ev lists and edits .env files with secrets masked, redact masks secrets in any piped output.",
|
|
6
6
|
"author": "Kaynooo <contact@kaynooo.fr>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -15,13 +15,16 @@
|
|
|
15
15
|
"keywords": ["env", "dotenv", "secrets", "redact", "mask"],
|
|
16
16
|
"exports": {
|
|
17
17
|
"./dotenv": "./src/dotenv.ts",
|
|
18
|
-
"./
|
|
18
|
+
"./files": "./src/files.ts",
|
|
19
|
+
"./guard": "./src/guard.ts",
|
|
20
|
+
"./mask": "./src/mask.ts",
|
|
21
|
+
"./redactor": "./src/redactor.ts"
|
|
19
22
|
},
|
|
20
23
|
"bin": {
|
|
21
24
|
"ev": "./dist/ev.js",
|
|
22
25
|
"redact": "./dist/redact.js"
|
|
23
26
|
},
|
|
24
|
-
"files": ["dist", "src
|
|
27
|
+
"files": ["dist", "src"],
|
|
25
28
|
"engines": {
|
|
26
29
|
"bun": ">=1.4.3"
|
|
27
30
|
},
|
|
@@ -34,6 +37,6 @@
|
|
|
34
37
|
"redact": "$npm_execpath src/redact.ts"
|
|
35
38
|
},
|
|
36
39
|
"devDependencies": {
|
|
37
|
-
"@kaynooo/cli-kit": "0.
|
|
40
|
+
"@kaynooo/cli-kit": "0.20.0"
|
|
38
41
|
}
|
|
39
42
|
}
|
package/src/audit.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type { Dirent } from 'node:fs'
|
|
2
|
+
import { readdir, stat } from 'node:fs/promises'
|
|
3
|
+
import { dirname, join } from 'node:path'
|
|
4
|
+
import { isSecretName } from '@kaynooo/cli-kit'
|
|
5
|
+
import { $ } from 'bun'
|
|
6
|
+
import { ENV_FILE, isFileEntry, TEMPLATE } from './files'
|
|
7
|
+
import { urlPasswords } from './mask'
|
|
8
|
+
|
|
9
|
+
export interface Exposure {
|
|
10
|
+
path: string
|
|
11
|
+
// Readable by an account other than its owner: any account, its group or an ACL entry.
|
|
12
|
+
readable: boolean
|
|
13
|
+
// Set when it is readable only through its group or an ACL, not by any account.
|
|
14
|
+
group?: boolean
|
|
15
|
+
tracked: boolean
|
|
16
|
+
// The keys a compose file writes out, for a compose file only.
|
|
17
|
+
secrets?: string[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const MAX_DEPTH = 5
|
|
21
|
+
const SKIPPED = new Set(['node_modules', '.git', 'vendor'])
|
|
22
|
+
const COMPOSE_FILE = /^(?:docker-)?compose[\w.-]*\.ya?ml$/
|
|
23
|
+
// An environment entry, `KEY: value` or `- KEY=value`, with its value written out.
|
|
24
|
+
const ENTRY = /^\s*(?:-\s*)?["']?([A-Z0-9_]+)["']?\s*[:=]\s*["']?([^"'\s#]+)/
|
|
25
|
+
// A setting about a secret (its lifetime, the file or header holding it), not the secret.
|
|
26
|
+
const SETTING_SUFFIX = /_(?:TTL|FILE|PATH|DIR|NAME|TIMEOUT|EXPIRY|EXPIRES|LIFETIME|LENGTH|ENABLED|ALGORITHM|HEADER|PREFIX|COUNT|MAX|MIN)$/i
|
|
27
|
+
const ACL_READER = /^(?:user|group):[^:]+:r/
|
|
28
|
+
// A switch like MYSQL_RANDOM_ROOT_PASSWORD: 1, not a secret.
|
|
29
|
+
const FLAG = /^(?:[01]|yes|no|true|false)$/i
|
|
30
|
+
|
|
31
|
+
// A compose value written out rather than read from a .env, the environment or a secrets file: a secret-named value or a URL password.
|
|
32
|
+
export function isInlineSecret(name: string, value: string): boolean {
|
|
33
|
+
if (value === '' || value.startsWith('$') || value.startsWith('/run/secrets/') || FLAG.test(value))
|
|
34
|
+
return false
|
|
35
|
+
|
|
36
|
+
return (isSecretName(name) && !SETTING_SUFFIX.test(name)) || urlPasswords(value).some(password => !password.startsWith('$'))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// The names of the secrets a compose file writes out.
|
|
40
|
+
export function inlineSecrets(compose: string): string[] {
|
|
41
|
+
const names = compose.split('\n').flatMap((line) => {
|
|
42
|
+
const [, name = '', value = ''] = ENTRY.exec(line) ?? []
|
|
43
|
+
|
|
44
|
+
return isInlineSecret(name, value) ? [name] : []
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
return [...new Set(names)]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Any account can read it: the file lets others read and every folder above it lets them through.
|
|
51
|
+
export async function openToOthers(path: string): Promise<boolean> {
|
|
52
|
+
if (((await stat(path)).mode & 0o004) === 0)
|
|
53
|
+
return false
|
|
54
|
+
|
|
55
|
+
for (let dir = dirname(path); dir !== '/'; dir = dirname(dir)) {
|
|
56
|
+
if (((await stat(dir)).mode & 0o001) === 0)
|
|
57
|
+
return false
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return true
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Whether the group bit lets in a group other than the owner's primary one, which only the owner belongs to on a system with private groups. */
|
|
64
|
+
export function groupReads(mode: number, gid: number, ownerGroup: number): boolean {
|
|
65
|
+
return (mode & 0o040) !== 0 && gid !== ownerGroup && gid !== process.getgid?.()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// The primary group of an account, the account's own id when the system does not tell.
|
|
69
|
+
function primaryGroup(uid: number): number {
|
|
70
|
+
if (uid === process.getuid?.())
|
|
71
|
+
return process.getgid?.() ?? uid
|
|
72
|
+
|
|
73
|
+
const run = Bun.spawnSync(['id', '-g', String(uid)], { stdout: 'pipe', stderr: 'ignore' })
|
|
74
|
+
const gid = Number(run.stdout.toString().trim())
|
|
75
|
+
|
|
76
|
+
return run.exitCode === 0 && Number.isInteger(gid) ? gid : uid
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Accounts other than the owner can read it through a group that is not the owner's own, or through a named user or group of its ACL, and every folder above lets them through.
|
|
80
|
+
export async function sharedWith(path: string): Promise<boolean> {
|
|
81
|
+
const info = await stat(path)
|
|
82
|
+
const shared = groupReads(info.mode, info.gid, primaryGroup(info.uid))
|
|
83
|
+
|
|
84
|
+
if (!shared && (Bun.which('getfacl') === null || !await hasAclReader(path)))
|
|
85
|
+
return false
|
|
86
|
+
|
|
87
|
+
for (let dir = dirname(path); dir !== '/'; dir = dirname(dir)) {
|
|
88
|
+
if (((await stat(dir)).mode & 0o011) === 0)
|
|
89
|
+
return false
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return true
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function hasAclReader(path: string): Promise<boolean> {
|
|
96
|
+
const acl = await $`getfacl --absolute-names --omit-header -- ${path}`.quiet().nothrow()
|
|
97
|
+
|
|
98
|
+
return acl.exitCode === 0 && acl.stdout.toString().split('\n').some(line => ACL_READER.test(line))
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Exit 1 is git saying the file is not tracked, and a folder outside any repository tracks nothing; anything else is git failing, which must not read as untracked.
|
|
102
|
+
async function tracked(path: string): Promise<boolean> {
|
|
103
|
+
const run = await $`git ls-files --error-unmatch -- ${path}`.cwd(dirname(path)).quiet().nothrow()
|
|
104
|
+
const error = run.stderr.toString()
|
|
105
|
+
|
|
106
|
+
if (run.exitCode === 0)
|
|
107
|
+
return true
|
|
108
|
+
|
|
109
|
+
if (run.exitCode === 1 || error.includes('not a git repository'))
|
|
110
|
+
return false
|
|
111
|
+
|
|
112
|
+
throw new Error(`git could not tell whether ${path} is tracked: ${error.trim() || `exit ${run.exitCode}`}`)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// The .env* and compose files under root, down to MAX_DEPTH folders, skipping dependency and git folders and following no link; a folder it cannot read goes to `unread`.
|
|
116
|
+
async function* candidates(root: string, unread: string[], depth = 0): AsyncGenerator<string> {
|
|
117
|
+
let found: Dirent[]
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
found = await readdir(root, { withFileTypes: true })
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
unread.push(root)
|
|
124
|
+
|
|
125
|
+
return
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
for (const entry of found) {
|
|
129
|
+
const path = join(root, entry.name)
|
|
130
|
+
|
|
131
|
+
if (entry.isDirectory() && depth < MAX_DEPTH && !SKIPPED.has(entry.name))
|
|
132
|
+
yield* candidates(path, unread, depth + 1)
|
|
133
|
+
else if (!TEMPLATE.test(entry.name) && (ENV_FILE.test(entry.name) || COMPOSE_FILE.test(entry.name)) && await isFileEntry(entry, path))
|
|
134
|
+
yield path
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// The .env files any account can read or git tracks, and the compose files writing secrets out that any account can read: tracked dev composes with throwaway passwords are left out as too common.
|
|
139
|
+
export async function audit(roots: string[]): Promise<{ exposed: Exposure[], unread: string[] }> {
|
|
140
|
+
const exposed: Exposure[] = []
|
|
141
|
+
const unread: string[] = []
|
|
142
|
+
|
|
143
|
+
for (const root of roots) {
|
|
144
|
+
for await (const path of candidates(root, unread)) {
|
|
145
|
+
const others = await openToOthers(path)
|
|
146
|
+
const readable = others || await sharedWith(path)
|
|
147
|
+
const group = readable && !others ? true : undefined
|
|
148
|
+
const name = path.slice(path.lastIndexOf('/') + 1)
|
|
149
|
+
|
|
150
|
+
if (ENV_FILE.test(name)) {
|
|
151
|
+
const isTracked = await tracked(path)
|
|
152
|
+
|
|
153
|
+
if (readable || isTracked)
|
|
154
|
+
exposed.push({ path, readable, ...group && { group }, tracked: isTracked })
|
|
155
|
+
|
|
156
|
+
continue
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const secrets = readable ? inlineSecrets(await Bun.file(path).text()) : []
|
|
160
|
+
|
|
161
|
+
if (secrets.length > 0)
|
|
162
|
+
exposed.push({ path, readable, ...group && { group }, tracked: false, secrets })
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return { exposed: exposed.sort((a, b) => a.path.localeCompare(b.path)), unread }
|
|
167
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { Exposure } from '../audit'
|
|
2
|
+
import type { ComposeFix } from '../fix'
|
|
3
|
+
import { stat } from 'node:fs/promises'
|
|
4
|
+
import { relative, resolve } from 'node:path'
|
|
5
|
+
import process from 'node:process'
|
|
6
|
+
import { fail, jsonOption, parse, printJson } from '@kaynooo/cli-kit'
|
|
7
|
+
import { shellQuote } from '@kaynooo/cli-kit/shell'
|
|
8
|
+
import { audit as findExposed } from '../audit'
|
|
9
|
+
import { fixCompose, shutOthersOut } from '../fix'
|
|
10
|
+
|
|
11
|
+
const usage = `ev audit [dir...] [--fix]
|
|
12
|
+
|
|
13
|
+
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.
|
|
14
|
+
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.
|
|
15
|
+
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.
|
|
16
|
+
Exits 0 when it finds none (with --fix, when none is left), 1 when it finds one, 2 on a usage error.`
|
|
17
|
+
|
|
18
|
+
const options = {
|
|
19
|
+
json: jsonOption('the findings and the folders it could not read'),
|
|
20
|
+
fix: { type: 'boolean', description: 'move inline compose secrets into a .env and shut other accounts out' },
|
|
21
|
+
} as const
|
|
22
|
+
|
|
23
|
+
const MAX_UNREAD_LISTED = 3
|
|
24
|
+
|
|
25
|
+
const CHECKS: Record<NonNullable<ComposeFix['check']>, string> = {
|
|
26
|
+
'same': 'docker compose config resolves the same',
|
|
27
|
+
'rolled back': 'docker compose config resolved differently, so everything was put back',
|
|
28
|
+
'no docker': 'not checked against docker compose config: docker is not installed',
|
|
29
|
+
'fails before': 'not checked against docker compose config: it already failed on the file',
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function isFolder(path: string): Promise<boolean> {
|
|
33
|
+
try {
|
|
34
|
+
return (await stat(path)).isDirectory()
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return false
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function shown(path: string): string {
|
|
42
|
+
return path.startsWith(`${process.cwd()}/`) ? relative(process.cwd(), path) : path
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Fixes what it can of each finding and prints what it did, never a value, unless the caller prints JSON.
|
|
46
|
+
async function fixAll(exposed: Exposure[], json: boolean): Promise<{ fixed: ComposeFix[], left: boolean }> {
|
|
47
|
+
const fixed: ComposeFix[] = []
|
|
48
|
+
let left = false
|
|
49
|
+
|
|
50
|
+
for (const { path, readable, group, tracked, secrets } of exposed) {
|
|
51
|
+
const fix = secrets === undefined ? undefined : await fixCompose(path, group === true)
|
|
52
|
+
const open = fix?.open ?? (readable && !await shutOthersOut(path, group === true) ? [path] : [])
|
|
53
|
+
|
|
54
|
+
if (fix !== undefined)
|
|
55
|
+
fixed.push(fix)
|
|
56
|
+
|
|
57
|
+
left ||= tracked || open.length > 0 || (fix?.left.length ?? 0) > 0
|
|
58
|
+
|
|
59
|
+
if (json)
|
|
60
|
+
continue
|
|
61
|
+
|
|
62
|
+
const lines = [
|
|
63
|
+
...fix === undefined || fix.moved.length === 0 ? [] : [`moved ${fix.moved.join(', ')} into ${shown(fix.env)}`],
|
|
64
|
+
...fix?.left.map(({ key, why }) => `left ${key} in place: ${why}`) ?? [],
|
|
65
|
+
...fix?.check === undefined ? [] : [CHECKS[fix.check]],
|
|
66
|
+
...open.map(file => `another account owns ${file === path ? 'it' : shown(file)}, so it stays readable: sudo chmod go-rwx ${shellQuote(shown(file))}`),
|
|
67
|
+
...readable && !open.includes(path) ? ['now unreadable by other accounts'] : [],
|
|
68
|
+
...tracked ? [`tracked by git: git rm --cached ${shellQuote(shown(path))}, then ignore it`] : [],
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
console.log(`${shown(path)}\n ${lines.join('\n ')}`)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return { fixed, left }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function audit(args: string[]): Promise<number> {
|
|
78
|
+
const parsed = parse(args, usage, options)
|
|
79
|
+
|
|
80
|
+
const roots = (parsed.positionals.length > 0 ? parsed.positionals : ['.']).map(dir => resolve(dir))
|
|
81
|
+
|
|
82
|
+
for (const dir of roots) {
|
|
83
|
+
if (!await isFolder(dir))
|
|
84
|
+
fail(`${dir} is not a folder`)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const { exposed, unread } = await findExposed(roots)
|
|
88
|
+
const json = parsed.values.json === true
|
|
89
|
+
|
|
90
|
+
// Container data folders owned by another account often run to dozens, so past a few they are only counted.
|
|
91
|
+
if (!json && unread.length > MAX_UNREAD_LISTED) {
|
|
92
|
+
console.error(`could not read ${unread.length} folders, so they were not audited (--json lists them)`)
|
|
93
|
+
}
|
|
94
|
+
else if (!json) {
|
|
95
|
+
for (const dir of unread)
|
|
96
|
+
console.error(`could not read ${dir}, so it was not audited`)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (parsed.values.fix === true && (json || exposed.length > 0)) {
|
|
100
|
+
const { fixed, left } = await fixAll(exposed, json)
|
|
101
|
+
|
|
102
|
+
if (json)
|
|
103
|
+
printJson({ fixed, exposed: (await findExposed(roots)).exposed, unread })
|
|
104
|
+
|
|
105
|
+
return left ? 1 : 0
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (json) {
|
|
109
|
+
printJson({ exposed, unread })
|
|
110
|
+
|
|
111
|
+
return exposed.length > 0 ? 1 : 0
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (exposed.length === 0) {
|
|
115
|
+
console.log('no exposed .env file nor compose file writing a secret out')
|
|
116
|
+
|
|
117
|
+
return 0
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const uid = process.getuid?.()
|
|
121
|
+
|
|
122
|
+
for (const { path, readable, group, tracked, secrets } of exposed) {
|
|
123
|
+
// Only the owner or root can change a file's mode.
|
|
124
|
+
const sudo = uid !== undefined && uid !== 0 && (await stat(path)).uid !== uid ? 'sudo ' : ''
|
|
125
|
+
const problems = [
|
|
126
|
+
...secrets === undefined ? [] : [`writes out ${secrets.join(', ')}: ev audit --fix moves the values into a .env beside it`],
|
|
127
|
+
...readable ? [group === true ? `readable by its group or an ACL: ${sudo}chmod go-rwx ${shellQuote(shown(path))}` : `readable by any account: ${sudo}chmod o-rwx ${shellQuote(shown(path))}`] : [],
|
|
128
|
+
...tracked ? [`tracked by git: git rm --cached ${shellQuote(shown(path))}, then ignore it`] : [],
|
|
129
|
+
]
|
|
130
|
+
|
|
131
|
+
console.log(`${shown(path)}\n ${problems.join('\n ')}`)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return 1
|
|
135
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { fail, jsonOption, parse, printJson } from '@kaynooo/cli-kit'
|
|
2
|
+
import { entries } from '../dotenv'
|
|
3
|
+
import { readEnv } from '../files'
|
|
4
|
+
|
|
5
|
+
const diffUsage = `ev diff <a> <b>
|
|
6
|
+
|
|
7
|
+
Names the keys set in one file and not the other, never their values.
|
|
8
|
+
|
|
9
|
+
--json prints {a, b, onlyA, onlyB}.
|
|
10
|
+
|
|
11
|
+
Exits 0 when both set the same keys, 1 when they differ, 2 on a usage error.`
|
|
12
|
+
|
|
13
|
+
const missingUsage = `ev missing [file]
|
|
14
|
+
|
|
15
|
+
Names the keys the reference file sets that the file (.env by default) does not.
|
|
16
|
+
|
|
17
|
+
--json prints {a, b, onlyA, onlyB} with the reference as a, so onlyA is what is missing.
|
|
18
|
+
|
|
19
|
+
Exits 0 when none is missing, 1 when some are, 2 on a usage error.`
|
|
20
|
+
|
|
21
|
+
const jsonFlag = { json: jsonOption() } as const
|
|
22
|
+
|
|
23
|
+
async function keys(path: string): Promise<Set<string>> {
|
|
24
|
+
return new Set(entries(await readEnv(path)).keys())
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function diff(args: string[]): Promise<number> {
|
|
28
|
+
const parsed = parse(args, diffUsage, jsonFlag)
|
|
29
|
+
|
|
30
|
+
const [a, b] = parsed.positionals
|
|
31
|
+
|
|
32
|
+
if (a === undefined || b === undefined)
|
|
33
|
+
fail(`needs two files\n\n${diffUsage}`)
|
|
34
|
+
|
|
35
|
+
const left = await keys(a)
|
|
36
|
+
const right = await keys(b)
|
|
37
|
+
const onlyA = [...left].filter(key => !right.has(key))
|
|
38
|
+
const onlyB = [...right].filter(key => !left.has(key))
|
|
39
|
+
|
|
40
|
+
if (parsed.values.json === true) {
|
|
41
|
+
printJson({ a, b, onlyA, onlyB })
|
|
42
|
+
|
|
43
|
+
return onlyA.length + onlyB.length === 0 ? 0 : 1
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const lines = [...onlyA.map(key => `only in ${a}: ${key}`), ...onlyB.map(key => `only in ${b}: ${key}`)]
|
|
47
|
+
|
|
48
|
+
if (lines.length === 0)
|
|
49
|
+
return 0
|
|
50
|
+
|
|
51
|
+
console.log(lines.join('\n'))
|
|
52
|
+
|
|
53
|
+
return 1
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function missing(args: string[]): Promise<number> {
|
|
57
|
+
const parsed = parse(args, missingUsage, { against: { type: 'string', description: 'the reference file (.env.example by default)' }, ...jsonFlag })
|
|
58
|
+
|
|
59
|
+
const [file = '.env'] = parsed.positionals
|
|
60
|
+
const against = parsed.values.against ?? '.env.example'
|
|
61
|
+
const have = await keys(file)
|
|
62
|
+
const reference = await keys(against)
|
|
63
|
+
const absent = [...reference].filter(key => !have.has(key))
|
|
64
|
+
|
|
65
|
+
if (parsed.values.json === true) {
|
|
66
|
+
printJson({ a: against, b: file, onlyA: absent, onlyB: [...have].filter(key => !reference.has(key)) })
|
|
67
|
+
|
|
68
|
+
return absent.length === 0 ? 0 : 1
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (absent.length === 0)
|
|
72
|
+
return 0
|
|
73
|
+
|
|
74
|
+
console.log(absent.map(key => `${key} (set in ${against}, not in ${file})`).join('\n'))
|
|
75
|
+
|
|
76
|
+
return 1
|
|
77
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { badUsage, fail, parse, readInput } from '@kaynooo/cli-kit'
|
|
2
|
+
import { entries, lastLine, removeKey, setLine } from '../dotenv'
|
|
3
|
+
import { loadKnown, readEnv, withLocks, writeEnv } from '../files'
|
|
4
|
+
import { maskValue } from '../mask'
|
|
5
|
+
|
|
6
|
+
const DEFAULT_FILE = '.env'
|
|
7
|
+
const TRAILING_NEWLINE = /\r?\n$/
|
|
8
|
+
const KEY_NAME = /^[A-Z_][\w.-]*$/i
|
|
9
|
+
|
|
10
|
+
const setUsage = `ev set KEY [file]
|
|
11
|
+
|
|
12
|
+
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; --empty sets it to an empty value.
|
|
13
|
+
|
|
14
|
+
Exits 0 when it wrote the key, 1 when it could not, 2 on a usage error.`
|
|
15
|
+
|
|
16
|
+
const unsetUsage = `ev unset KEY [file]
|
|
17
|
+
|
|
18
|
+
Removes every line setting KEY from the file (.env by default), leaving comments and other lines as they are.
|
|
19
|
+
|
|
20
|
+
Exits 0 when it removed the key, 1 when the file does not set it, 2 on a usage error.`
|
|
21
|
+
|
|
22
|
+
const mvUsage = `ev mv KEY --from <file> --to <file>
|
|
23
|
+
|
|
24
|
+
Moves KEY's line from one file to another, as written. Refuses to replace a key the target already sets unless --force.
|
|
25
|
+
|
|
26
|
+
Exits 0 when it moved the key, 1 when the source lacks it or the target already sets it, 2 on a usage error.`
|
|
27
|
+
|
|
28
|
+
const cpUsage = `ev cp KEY --from <file> --to <file> [--as NEWKEY]
|
|
29
|
+
|
|
30
|
+
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.
|
|
31
|
+
|
|
32
|
+
Exits 0 when it copied the key, 1 when the source lacks it, 2 on a usage error.`
|
|
33
|
+
|
|
34
|
+
async function readValue(from: string | undefined): Promise<string> {
|
|
35
|
+
const input = await readInput(from ?? '-', { noStdin: 'pipe the value on stdin or give -F <file>, it is never taken from the command line' })
|
|
36
|
+
|
|
37
|
+
if (input === '')
|
|
38
|
+
fail('the value is empty, nothing was written: --empty sets an empty value')
|
|
39
|
+
|
|
40
|
+
return input.replace(TRAILING_NEWLINE, '')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// A key that is not a plain name could carry a line break or an `=` and write more than one line.
|
|
44
|
+
function checkKey(key: string): void {
|
|
45
|
+
if (!KEY_NAME.test(key))
|
|
46
|
+
badUsage(`${JSON.stringify(key)} is not a valid key: letters, digits, _, . and - only, not starting with a digit`)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function readOrEmpty(path: string) {
|
|
50
|
+
return await Bun.file(path).exists() ? readEnv(path) : { lines: [] }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function set(args: string[]): Promise<number> {
|
|
54
|
+
const parsed = parse(args, setUsage, {
|
|
55
|
+
file: { type: 'string', short: 'F', description: 'read the value from this file instead of stdin' },
|
|
56
|
+
empty: { type: 'boolean', description: 'set an empty value, reading nothing' },
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
const [key, target = DEFAULT_FILE] = parsed.positionals
|
|
60
|
+
|
|
61
|
+
if (key === undefined)
|
|
62
|
+
fail(`missing KEY\n\n${setUsage}`)
|
|
63
|
+
|
|
64
|
+
if (parsed.values.empty === true && parsed.values.file !== undefined)
|
|
65
|
+
fail('--empty and -F exclude each other')
|
|
66
|
+
|
|
67
|
+
checkKey(key)
|
|
68
|
+
|
|
69
|
+
const value = parsed.values.empty === true ? '' : await readValue(parsed.values.file)
|
|
70
|
+
|
|
71
|
+
const file = await withLocks([target], async () => {
|
|
72
|
+
const current = await readOrEmpty(target)
|
|
73
|
+
|
|
74
|
+
setLine(current, key, value)
|
|
75
|
+
await writeEnv(target, current)
|
|
76
|
+
|
|
77
|
+
return current
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
console.log(`${target}: ${key}=${maskValue(key, value, await loadKnown(process.cwd(), [file])).replaceAll('\n', '\\n')}`)
|
|
81
|
+
|
|
82
|
+
return 0
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function unset(args: string[]): Promise<number> {
|
|
86
|
+
const parsed = parse(args, unsetUsage, {})
|
|
87
|
+
|
|
88
|
+
const [key, target = DEFAULT_FILE] = parsed.positionals
|
|
89
|
+
|
|
90
|
+
if (key === undefined)
|
|
91
|
+
fail(`missing KEY\n\n${unsetUsage}`)
|
|
92
|
+
|
|
93
|
+
await withLocks([target], async () => {
|
|
94
|
+
const file = await readEnv(target)
|
|
95
|
+
|
|
96
|
+
if (removeKey(file, key) === 0)
|
|
97
|
+
fail(`${key} is not set in ${target}`, 1)
|
|
98
|
+
|
|
99
|
+
await writeEnv(target, file)
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
console.log(`${target}: removed ${key}`)
|
|
103
|
+
|
|
104
|
+
return 0
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 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.
|
|
108
|
+
async function transfer(key: string, from: string, to: string, newKey: string | undefined, replace: boolean) {
|
|
109
|
+
checkKey(key)
|
|
110
|
+
|
|
111
|
+
if (newKey !== undefined)
|
|
112
|
+
checkKey(newKey)
|
|
113
|
+
|
|
114
|
+
if (from === to && newKey === undefined)
|
|
115
|
+
fail('--from and --to are the same file')
|
|
116
|
+
|
|
117
|
+
const source = await readEnv(from)
|
|
118
|
+
const line = lastLine(source, key)
|
|
119
|
+
|
|
120
|
+
if (line === undefined)
|
|
121
|
+
fail(`${key} is not set in ${from}`, 1)
|
|
122
|
+
|
|
123
|
+
const target = from === to ? source : await readOrEmpty(to)
|
|
124
|
+
const name = newKey ?? key
|
|
125
|
+
|
|
126
|
+
if (!replace && entries(target).has(name))
|
|
127
|
+
fail(`${to} already sets ${name}, --force replaces it`, 1)
|
|
128
|
+
|
|
129
|
+
const value = line.value ?? ''
|
|
130
|
+
|
|
131
|
+
setLine(target, name, value, newKey === undefined && !entries(target).has(name) ? line.raw : undefined)
|
|
132
|
+
await writeEnv(to, target)
|
|
133
|
+
|
|
134
|
+
return { source, target, value, name }
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export async function mv(args: string[]): Promise<number> {
|
|
138
|
+
const parsed = parse(args, mvUsage, {
|
|
139
|
+
from: { type: 'string', description: 'the file to take KEY from' },
|
|
140
|
+
to: { type: 'string', description: 'the file to put KEY in, created when missing' },
|
|
141
|
+
force: { type: 'boolean', description: 'replace KEY when the target already sets it' },
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
const [key] = parsed.positionals
|
|
145
|
+
const { from, to, force } = parsed.values
|
|
146
|
+
|
|
147
|
+
if (key === undefined || from === undefined || to === undefined)
|
|
148
|
+
fail(`needs KEY, --from and --to\n\n${mvUsage}`)
|
|
149
|
+
|
|
150
|
+
await withLocks([from, to], async () => {
|
|
151
|
+
const { source } = await transfer(key, from, to, undefined, force === true)
|
|
152
|
+
|
|
153
|
+
removeKey(source, key)
|
|
154
|
+
await writeEnv(from, source)
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
console.log(`${key}: ${from} -> ${to}`)
|
|
158
|
+
|
|
159
|
+
return 0
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export async function cp(args: string[]): Promise<number> {
|
|
163
|
+
const parsed = parse(args, cpUsage, {
|
|
164
|
+
from: { type: 'string', description: 'the file to take KEY from' },
|
|
165
|
+
to: { type: 'string', description: 'the file to put the copy in, created when missing' },
|
|
166
|
+
as: { type: 'string', description: 'store the copy under this key instead' },
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
const [key] = parsed.positionals
|
|
170
|
+
const { from, to, as } = parsed.values
|
|
171
|
+
|
|
172
|
+
if (key === undefined || from === undefined || to === undefined)
|
|
173
|
+
fail(`needs KEY, --from and --to\n\n${cpUsage}`)
|
|
174
|
+
|
|
175
|
+
const { source, target, value, name } = await withLocks([from, to], async () => transfer(key, from, to, as, true))
|
|
176
|
+
const known = await loadKnown(process.cwd(), [source, target])
|
|
177
|
+
|
|
178
|
+
console.log(`${to}: ${name}=${maskValue(name, value, known).replaceAll('\n', '\\n')}`)
|
|
179
|
+
|
|
180
|
+
return 0
|
|
181
|
+
}
|