@kaynooo/cli-env 0.13.0 → 0.13.1
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 +1 -0
- package/dist/ev.js +65 -29
- package/dist/redact.js +43 -14
- package/package.json +2 -2
- package/src/commands/audit.ts +3 -3
- package/src/commands/edit.ts +18 -10
- package/src/files.ts +2 -2
package/README.md
CHANGED
|
@@ -16,6 +16,7 @@ bun install -g @kaynooo/cli-env
|
|
|
16
16
|
| `ev show <rev>:<file>` | an env file as a git revision holds it, values masked |
|
|
17
17
|
| `ev set KEY [file]` | sets a key in `.env` (or the file), the value read from stdin or `-F <file>` so it never appears in a command line, `--empty` for an empty value |
|
|
18
18
|
| `ev unset KEY [file]` | removes a key |
|
|
19
|
+
| `--quiet` (`-q`) | on set, unset, cp and mv, prints nothing on success |
|
|
19
20
|
| `ev cp KEY --from <file> --to <file>` | copies a key's value to another file without printing it, replacing the key there or appending it, `--as NEWKEY` to rename |
|
|
20
21
|
| `ev mv KEY --from <file> --to <file>` | moves a key's line as written, `--force` to replace one the target has |
|
|
21
22
|
| `ev diff <a> <b>` | names the keys set in one file and not the other, exit 1 when they differ; `--json` prints `{a, b, onlyA, onlyB}` |
|
package/dist/ev.js
CHANGED
|
@@ -9,6 +9,7 @@ import { parseArgs } from "util";
|
|
|
9
9
|
|
|
10
10
|
// ../kit/src/format.ts
|
|
11
11
|
import process7 from "process";
|
|
12
|
+
|
|
12
13
|
// ../../node_modules/.bun/@kaynooo+utils@3.0.0+4c7ddab9986847c5/node_modules/@kaynooo/utils/dist/chunk-rxztfxgv.js
|
|
13
14
|
var N = new Map;
|
|
14
15
|
var Nt = /\s+\S*$/;
|
|
@@ -26,6 +27,7 @@ function ko(t, e, r = "...") {
|
|
|
26
27
|
}
|
|
27
28
|
var be = new Map(["\u20AC", "", "\u201A", "\u0192", "\u201E", "\u2026", "\u2020", "\u2021", "\u02C6", "\u2030", "\u0160", "\u2039", "\u0152", "", "\u017D", "", "", "\u2018", "\u2019", "\u201C", "\u201D", "\u2022", "\u2013", "\u2014", "\u02DC", "\u2122", "\u0161", "\u203A", "\u0153", "", "\u017E", "\u0178"].map((t, e) => [t, 128 + e]).filter(([t]) => t));
|
|
28
29
|
var z = new Map;
|
|
30
|
+
|
|
29
31
|
// ../kit/src/mode.ts
|
|
30
32
|
import process2 from "process";
|
|
31
33
|
var MODES = new Set(["terminal", "ci", "agent", "pipe"]);
|
|
@@ -71,7 +73,7 @@ function C(r, e = globalThis.process?.env ?? {}) {
|
|
|
71
73
|
return false;
|
|
72
74
|
return r?.isTTY === true;
|
|
73
75
|
}
|
|
74
|
-
function
|
|
76
|
+
function f(r, e = []) {
|
|
75
77
|
let l = "", n = "";
|
|
76
78
|
for (let t = 0, i = e.length;t < i; t++)
|
|
77
79
|
l += `\x1B[${e[t][0]}m`, n = `\x1B[${e[t][1]}m${n}`;
|
|
@@ -81,7 +83,7 @@ function f2(r, e = []) {
|
|
|
81
83
|
if (Array.isArray(a)) {
|
|
82
84
|
let s = g.get(i);
|
|
83
85
|
if (s === undefined)
|
|
84
|
-
s =
|
|
86
|
+
s = f(r, [...e, a]), g.set(i, s);
|
|
85
87
|
return s;
|
|
86
88
|
}
|
|
87
89
|
if (typeof a === "function")
|
|
@@ -90,20 +92,20 @@ function f2(r, e = []) {
|
|
|
90
92
|
if (u === undefined) {
|
|
91
93
|
if (g.size >= 64)
|
|
92
94
|
g.clear();
|
|
93
|
-
u =
|
|
95
|
+
u = f(r, [...e, a(...s)]), g.set(y, u);
|
|
94
96
|
}
|
|
95
97
|
return u;
|
|
96
98
|
};
|
|
97
99
|
throw Error(`Unknown style: ${String(i)}`);
|
|
98
100
|
} });
|
|
99
101
|
}
|
|
100
|
-
function
|
|
101
|
-
return
|
|
102
|
+
function Fe(r = {}) {
|
|
103
|
+
return f(r);
|
|
102
104
|
}
|
|
103
|
-
var
|
|
105
|
+
var o = Fe();
|
|
104
106
|
|
|
105
107
|
// ../kit/src/theme.ts
|
|
106
|
-
import { existsSync as
|
|
108
|
+
import { existsSync as existsSync3, readFileSync as readFileSync2, statSync } from "fs";
|
|
107
109
|
import { homedir as homedir2 } from "os";
|
|
108
110
|
import { join as join3, resolve as resolve2 } from "path";
|
|
109
111
|
import process5 from "process";
|
|
@@ -124,6 +126,7 @@ function findUp(names, from = process3.cwd(), stop = () => false) {
|
|
|
124
126
|
}
|
|
125
127
|
|
|
126
128
|
// ../kit/src/user-config.ts
|
|
129
|
+
import { existsSync as existsSync2, readFileSync } from "fs";
|
|
127
130
|
import { homedir } from "os";
|
|
128
131
|
import { join as join2 } from "path";
|
|
129
132
|
import process4 from "process";
|
|
@@ -134,6 +137,24 @@ function configBase(where = {}) {
|
|
|
134
137
|
function userConfigPath(bin, where = {}) {
|
|
135
138
|
return join2(configBase(where), "kaynooo-cli", `${bin}.json`);
|
|
136
139
|
}
|
|
140
|
+
function userConfigReadPath(bin, legacy = [], where = {}) {
|
|
141
|
+
const current = userConfigPath(bin, where);
|
|
142
|
+
if (existsSync2(current))
|
|
143
|
+
return current;
|
|
144
|
+
const base = configBase(where);
|
|
145
|
+
return legacy.map((parts) => join2(base, ...parts)).find((path) => existsSync2(path)) ?? current;
|
|
146
|
+
}
|
|
147
|
+
function readUserConfig(bin, where = {}, legacy = []) {
|
|
148
|
+
const path = userConfigReadPath(bin, legacy, where);
|
|
149
|
+
if (!existsSync2(path))
|
|
150
|
+
return {};
|
|
151
|
+
try {
|
|
152
|
+
const parsed = Bun.JSONC.parse(readFileSync(path, "utf8"));
|
|
153
|
+
return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed : {};
|
|
154
|
+
} catch {
|
|
155
|
+
return {};
|
|
156
|
+
}
|
|
157
|
+
}
|
|
137
158
|
|
|
138
159
|
// ../kit/src/theme.ts
|
|
139
160
|
var ENDS = new Map;
|
|
@@ -200,7 +221,7 @@ function readThemeFile(path, warn) {
|
|
|
200
221
|
warn(`${path} is not a regular file under ${MAX_FILE_BYTES / 1024} KiB, theme ignored`);
|
|
201
222
|
return;
|
|
202
223
|
}
|
|
203
|
-
const parsed = JSON.parse(
|
|
224
|
+
const parsed = JSON.parse(readFileSync2(path, "utf8"));
|
|
204
225
|
if (isRecord(parsed))
|
|
205
226
|
return parsed;
|
|
206
227
|
warn(`${path} is not a JSON object, theme ignored`);
|
|
@@ -266,7 +287,7 @@ function applyFile(theme, file, where, warn) {
|
|
|
266
287
|
}
|
|
267
288
|
}
|
|
268
289
|
function endsWalk(home) {
|
|
269
|
-
return (dir) => dir === resolve2(home) ||
|
|
290
|
+
return (dir) => dir === resolve2(home) || existsSync3(join3(dir, ".git"));
|
|
270
291
|
}
|
|
271
292
|
var warned = new Set;
|
|
272
293
|
function warnOnce(message) {
|
|
@@ -322,7 +343,7 @@ function takesControlCodes(stream, env = process6.env) {
|
|
|
322
343
|
return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb" && outputMode(stream, env) === "terminal";
|
|
323
344
|
}
|
|
324
345
|
function paint(stream, env = process6.env) {
|
|
325
|
-
return
|
|
346
|
+
return Fe({ stream: { isTTY: takesControlCodes(stream, env) }, env });
|
|
326
347
|
}
|
|
327
348
|
function marked(mark, text, stream, detail, env = process6.env) {
|
|
328
349
|
const colors = paint(stream, env);
|
|
@@ -336,6 +357,9 @@ function marked(mark, text, stream, detail, env = process6.env) {
|
|
|
336
357
|
function ok(text, detail) {
|
|
337
358
|
console.log(marked("ok", text, process6.stdout, detail));
|
|
338
359
|
}
|
|
360
|
+
function warn(text, detail) {
|
|
361
|
+
console.error(marked("warn", text, process6.stderr, detail));
|
|
362
|
+
}
|
|
339
363
|
function role(name, stream, env = process6.env) {
|
|
340
364
|
const colors = paint(stream, env);
|
|
341
365
|
return takesControlCodes(stream, env) ? tinted(colors, activeTheme().theme.colors[name], env) : colors;
|
|
@@ -721,7 +745,7 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
|
|
|
721
745
|
var RESET_STYLE = `${ESC}[0m`;
|
|
722
746
|
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
723
747
|
// ../kit/src/cli.ts
|
|
724
|
-
import { readFileSync as
|
|
748
|
+
import { readFileSync as readFileSync3, realpathSync } from "fs";
|
|
725
749
|
import { dirname as dirname3, join as join5 } from "path";
|
|
726
750
|
import process12 from "process";
|
|
727
751
|
|
|
@@ -1022,7 +1046,7 @@ async function printCandidates(found) {
|
|
|
1022
1046
|
function packageVersion(entry = Bun.main) {
|
|
1023
1047
|
for (let dir = dirname3(realpathSync(entry));; dir = dirname3(dir)) {
|
|
1024
1048
|
try {
|
|
1025
|
-
const { version } = JSON.parse(
|
|
1049
|
+
const { version } = JSON.parse(readFileSync3(join5(dir, "package.json"), "utf8"));
|
|
1026
1050
|
if (typeof version === "string")
|
|
1027
1051
|
return version;
|
|
1028
1052
|
} catch {}
|
|
@@ -1080,7 +1104,7 @@ async function runMain({ name, version, run, redact, usage }, argv = process12.a
|
|
|
1080
1104
|
// ../kit/src/compose.ts
|
|
1081
1105
|
async function captureOutput(argv, { cwd, env } = {}) {
|
|
1082
1106
|
const run = Bun.spawn(argv, { ...cwd === undefined ? {} : { cwd }, env: { ...process.env, ...env }, stdin: "ignore", stdout: "pipe", stderr: "pipe" });
|
|
1083
|
-
const [stdout, stderr, code] = await Promise.all([run.stdout.text(), run.stderr.text(), run.exited]);
|
|
1107
|
+
const [stdout, stderr, code] = await Promise.all([new Response(run.stdout).text(), new Response(run.stderr).text(), run.exited]);
|
|
1084
1108
|
return { code, stdout, stderr };
|
|
1085
1109
|
}
|
|
1086
1110
|
async function compose(args, { flags = [], ...options } = {}) {
|
|
@@ -1088,6 +1112,11 @@ async function compose(args, { flags = [], ...options } = {}) {
|
|
|
1088
1112
|
}
|
|
1089
1113
|
// ../kit/src/folders.ts
|
|
1090
1114
|
var SKIPPED_FOLDERS = new Set(["node_modules", "vendor", "dist", "build", "target", "coverage", "venv", "__pycache__"]);
|
|
1115
|
+
function ignoredFolders(extra = [], where = {}) {
|
|
1116
|
+
const configured = readUserConfig("config", where).ignore;
|
|
1117
|
+
const names = Array.isArray(configured) ? configured.filter((name) => typeof name === "string" && name !== "") : [];
|
|
1118
|
+
return new Set([...SKIPPED_FOLDERS, ...extra, ...names]);
|
|
1119
|
+
}
|
|
1091
1120
|
// ../kit/src/secrets.ts
|
|
1092
1121
|
var SECRET_PART = /token|secret|passw|credential|private|dsn|apikey|accesskey|key$/i;
|
|
1093
1122
|
var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
|
|
@@ -1451,7 +1480,7 @@ function maskValue(key, value, known) {
|
|
|
1451
1480
|
var ENV_FILE = /^(?:\.env(?:\..+)?|.+\.env|\.envrc)$/;
|
|
1452
1481
|
var TEMPLATE = /\.(?:example|sample|template|dist|defaults)$/;
|
|
1453
1482
|
var KNOWN_DEPTH = 3;
|
|
1454
|
-
var SKIPPED =
|
|
1483
|
+
var SKIPPED = ignoredFolders([".git", ".kay"]);
|
|
1455
1484
|
var LOCK_WAIT_MS = 5000;
|
|
1456
1485
|
var LOCK_STALE_MS = 30000;
|
|
1457
1486
|
var REAP_STALE_MS = 5000;
|
|
@@ -1957,10 +1986,10 @@ async function audit2(args) {
|
|
|
1957
1986
|
const { exposed, unread } = await audit(roots);
|
|
1958
1987
|
const json = parsed.values.json === true;
|
|
1959
1988
|
if (!json && unread.length > MAX_UNREAD_LISTED) {
|
|
1960
|
-
|
|
1989
|
+
warn(`could not read ${unread.length} folders, so they were not audited (--json lists them)`);
|
|
1961
1990
|
} else if (!json) {
|
|
1962
1991
|
for (const dir of unread)
|
|
1963
|
-
|
|
1992
|
+
warn(`could not read ${dir}, so it was not audited`);
|
|
1964
1993
|
}
|
|
1965
1994
|
if (parsed.values.fix === true && (json || exposed.length > 0)) {
|
|
1966
1995
|
const { fixed, left } = await fixAll(exposed, json);
|
|
@@ -2060,22 +2089,23 @@ async function missing(args) {
|
|
|
2060
2089
|
var DEFAULT_FILE = ".env";
|
|
2061
2090
|
var TRAILING_NEWLINE = /\r?\n$/;
|
|
2062
2091
|
var KEY_NAME = /^[A-Z_][\w.-]*$/i;
|
|
2063
|
-
var
|
|
2092
|
+
var QUIET = { quiet: { type: "boolean", short: "q", description: "print nothing on success" } };
|
|
2093
|
+
var setUsage = `ev set KEY [file] [--quiet]
|
|
2064
2094
|
|
|
2065
2095
|
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.
|
|
2066
2096
|
|
|
2067
2097
|
Exits 0 when it wrote the key, 1 when it could not, 2 on a usage error.`;
|
|
2068
|
-
var unsetUsage = `ev unset KEY [file]
|
|
2098
|
+
var unsetUsage = `ev unset KEY [file] [--quiet]
|
|
2069
2099
|
|
|
2070
2100
|
Removes every line setting KEY from the file (.env by default), leaving comments and other lines as they are.
|
|
2071
2101
|
|
|
2072
2102
|
Exits 0 when it removed the key, 1 when the file does not set it, 2 on a usage error.`;
|
|
2073
|
-
var mvUsage = `ev mv KEY --from <file> --to <file>
|
|
2103
|
+
var mvUsage = `ev mv KEY --from <file> --to <file> [--quiet]
|
|
2074
2104
|
|
|
2075
2105
|
Moves KEY's line from one file to another, as written. Refuses to replace a key the target already sets unless --force.
|
|
2076
2106
|
|
|
2077
2107
|
Exits 0 when it moved the key, 1 when the source lacks it or the target already sets it, 2 on a usage error.`;
|
|
2078
|
-
var cpUsage = `ev cp KEY --from <file> --to <file> [--as NEWKEY]
|
|
2108
|
+
var cpUsage = `ev cp KEY --from <file> --to <file> [--as NEWKEY] [--quiet]
|
|
2079
2109
|
|
|
2080
2110
|
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.
|
|
2081
2111
|
|
|
@@ -2096,7 +2126,8 @@ async function readOrEmpty(path) {
|
|
|
2096
2126
|
async function set(args) {
|
|
2097
2127
|
const parsed = parse(args, setUsage, {
|
|
2098
2128
|
file: { type: "string", short: "F", description: "read the value from this file instead of stdin" },
|
|
2099
|
-
empty: { type: "boolean", description: "set an empty value, reading nothing" }
|
|
2129
|
+
empty: { type: "boolean", description: "set an empty value, reading nothing" },
|
|
2130
|
+
...QUIET
|
|
2100
2131
|
});
|
|
2101
2132
|
const [key, target = DEFAULT_FILE] = parsed.positionals;
|
|
2102
2133
|
if (key === undefined)
|
|
@@ -2113,12 +2144,13 @@ ${setUsage}`);
|
|
|
2113
2144
|
await writeEnv(target, current);
|
|
2114
2145
|
return current;
|
|
2115
2146
|
});
|
|
2116
|
-
|
|
2147
|
+
if (parsed.values.quiet !== true)
|
|
2148
|
+
console.log(`${target}: ${key}=${maskValue(key, value, await loadKnown(process.cwd(), [file])).replaceAll(`
|
|
2117
2149
|
`, "\\n")}`);
|
|
2118
2150
|
return 0;
|
|
2119
2151
|
}
|
|
2120
2152
|
async function unset(args) {
|
|
2121
|
-
const parsed = parse(args, unsetUsage,
|
|
2153
|
+
const parsed = parse(args, unsetUsage, QUIET);
|
|
2122
2154
|
const [key, target = DEFAULT_FILE] = parsed.positionals;
|
|
2123
2155
|
if (key === undefined)
|
|
2124
2156
|
fail(`missing KEY
|
|
@@ -2130,7 +2162,8 @@ ${unsetUsage}`);
|
|
|
2130
2162
|
fail(`${key} is not set in ${target}`, 1);
|
|
2131
2163
|
await writeEnv(target, file);
|
|
2132
2164
|
});
|
|
2133
|
-
|
|
2165
|
+
if (parsed.values.quiet !== true)
|
|
2166
|
+
console.log(`${target}: removed ${key}`);
|
|
2134
2167
|
return 0;
|
|
2135
2168
|
}
|
|
2136
2169
|
async function transfer(key, from, to, newKey, replace) {
|
|
@@ -2156,7 +2189,8 @@ async function mv(args) {
|
|
|
2156
2189
|
const parsed = parse(args, mvUsage, {
|
|
2157
2190
|
from: { type: "string", description: "the file to take KEY from" },
|
|
2158
2191
|
to: { type: "string", description: "the file to put KEY in, created when missing" },
|
|
2159
|
-
force: { type: "boolean", description: "replace KEY when the target already sets it" }
|
|
2192
|
+
force: { type: "boolean", description: "replace KEY when the target already sets it" },
|
|
2193
|
+
...QUIET
|
|
2160
2194
|
});
|
|
2161
2195
|
const [key] = parsed.positionals;
|
|
2162
2196
|
const { from, to, force } = parsed.values;
|
|
@@ -2169,14 +2203,16 @@ ${mvUsage}`);
|
|
|
2169
2203
|
removeKey(source, key);
|
|
2170
2204
|
await writeEnv(from, source);
|
|
2171
2205
|
});
|
|
2172
|
-
|
|
2206
|
+
if (parsed.values.quiet !== true)
|
|
2207
|
+
console.log(`${key}: ${from} -> ${to}`);
|
|
2173
2208
|
return 0;
|
|
2174
2209
|
}
|
|
2175
2210
|
async function cp(args) {
|
|
2176
2211
|
const parsed = parse(args, cpUsage, {
|
|
2177
2212
|
from: { type: "string", description: "the file to take KEY from" },
|
|
2178
2213
|
to: { type: "string", description: "the file to put the copy in, created when missing" },
|
|
2179
|
-
as: { type: "string", description: "store the copy under this key instead" }
|
|
2214
|
+
as: { type: "string", description: "store the copy under this key instead" },
|
|
2215
|
+
...QUIET
|
|
2180
2216
|
});
|
|
2181
2217
|
const [key] = parsed.positionals;
|
|
2182
2218
|
const { from, to, as } = parsed.values;
|
|
@@ -2185,8 +2221,8 @@ async function cp(args) {
|
|
|
2185
2221
|
|
|
2186
2222
|
${cpUsage}`);
|
|
2187
2223
|
const { source, target, value, name } = await withLocks([from, to], async () => transfer(key, from, to, as, true));
|
|
2188
|
-
|
|
2189
|
-
|
|
2224
|
+
if (parsed.values.quiet !== true)
|
|
2225
|
+
console.log(`${to}: ${name}=${maskValue(name, value, await loadKnown(process.cwd(), [source, target])).replaceAll(`
|
|
2190
2226
|
`, "\\n")}`);
|
|
2191
2227
|
return 0;
|
|
2192
2228
|
}
|
package/dist/redact.js
CHANGED
|
@@ -9,6 +9,7 @@ import { parseArgs } from "util";
|
|
|
9
9
|
|
|
10
10
|
// ../kit/src/format.ts
|
|
11
11
|
import process7 from "process";
|
|
12
|
+
|
|
12
13
|
// ../../node_modules/.bun/@kaynooo+utils@3.0.0+4c7ddab9986847c5/node_modules/@kaynooo/utils/dist/chunk-rxztfxgv.js
|
|
13
14
|
var N = new Map;
|
|
14
15
|
var Nt = /\s+\S*$/;
|
|
@@ -26,6 +27,7 @@ function ko(t, e, r = "...") {
|
|
|
26
27
|
}
|
|
27
28
|
var be = new Map(["\u20AC", "", "\u201A", "\u0192", "\u201E", "\u2026", "\u2020", "\u2021", "\u02C6", "\u2030", "\u0160", "\u2039", "\u0152", "", "\u017D", "", "", "\u2018", "\u2019", "\u201C", "\u201D", "\u2022", "\u2013", "\u2014", "\u02DC", "\u2122", "\u0161", "\u203A", "\u0153", "", "\u017E", "\u0178"].map((t, e) => [t, 128 + e]).filter(([t]) => t));
|
|
28
29
|
var z = new Map;
|
|
30
|
+
|
|
29
31
|
// ../kit/src/mode.ts
|
|
30
32
|
import process2 from "process";
|
|
31
33
|
var MODES = new Set(["terminal", "ci", "agent", "pipe"]);
|
|
@@ -71,7 +73,7 @@ function C(r, e = globalThis.process?.env ?? {}) {
|
|
|
71
73
|
return false;
|
|
72
74
|
return r?.isTTY === true;
|
|
73
75
|
}
|
|
74
|
-
function
|
|
76
|
+
function f(r, e = []) {
|
|
75
77
|
let l = "", n = "";
|
|
76
78
|
for (let t = 0, i = e.length;t < i; t++)
|
|
77
79
|
l += `\x1B[${e[t][0]}m`, n = `\x1B[${e[t][1]}m${n}`;
|
|
@@ -81,7 +83,7 @@ function f2(r, e = []) {
|
|
|
81
83
|
if (Array.isArray(a)) {
|
|
82
84
|
let s = g.get(i);
|
|
83
85
|
if (s === undefined)
|
|
84
|
-
s =
|
|
86
|
+
s = f(r, [...e, a]), g.set(i, s);
|
|
85
87
|
return s;
|
|
86
88
|
}
|
|
87
89
|
if (typeof a === "function")
|
|
@@ -90,20 +92,20 @@ function f2(r, e = []) {
|
|
|
90
92
|
if (u === undefined) {
|
|
91
93
|
if (g.size >= 64)
|
|
92
94
|
g.clear();
|
|
93
|
-
u =
|
|
95
|
+
u = f(r, [...e, a(...s)]), g.set(y, u);
|
|
94
96
|
}
|
|
95
97
|
return u;
|
|
96
98
|
};
|
|
97
99
|
throw Error(`Unknown style: ${String(i)}`);
|
|
98
100
|
} });
|
|
99
101
|
}
|
|
100
|
-
function
|
|
101
|
-
return
|
|
102
|
+
function Fe(r = {}) {
|
|
103
|
+
return f(r);
|
|
102
104
|
}
|
|
103
|
-
var
|
|
105
|
+
var o = Fe();
|
|
104
106
|
|
|
105
107
|
// ../kit/src/theme.ts
|
|
106
|
-
import { existsSync as
|
|
108
|
+
import { existsSync as existsSync3, readFileSync as readFileSync2, statSync } from "fs";
|
|
107
109
|
import { homedir as homedir2 } from "os";
|
|
108
110
|
import { join as join3, resolve as resolve2 } from "path";
|
|
109
111
|
import process5 from "process";
|
|
@@ -124,6 +126,7 @@ function findUp(names, from = process3.cwd(), stop = () => false) {
|
|
|
124
126
|
}
|
|
125
127
|
|
|
126
128
|
// ../kit/src/user-config.ts
|
|
129
|
+
import { existsSync as existsSync2, readFileSync } from "fs";
|
|
127
130
|
import { homedir } from "os";
|
|
128
131
|
import { join as join2 } from "path";
|
|
129
132
|
import process4 from "process";
|
|
@@ -134,6 +137,24 @@ function configBase(where = {}) {
|
|
|
134
137
|
function userConfigPath(bin, where = {}) {
|
|
135
138
|
return join2(configBase(where), "kaynooo-cli", `${bin}.json`);
|
|
136
139
|
}
|
|
140
|
+
function userConfigReadPath(bin, legacy = [], where = {}) {
|
|
141
|
+
const current = userConfigPath(bin, where);
|
|
142
|
+
if (existsSync2(current))
|
|
143
|
+
return current;
|
|
144
|
+
const base = configBase(where);
|
|
145
|
+
return legacy.map((parts) => join2(base, ...parts)).find((path) => existsSync2(path)) ?? current;
|
|
146
|
+
}
|
|
147
|
+
function readUserConfig(bin, where = {}, legacy = []) {
|
|
148
|
+
const path = userConfigReadPath(bin, legacy, where);
|
|
149
|
+
if (!existsSync2(path))
|
|
150
|
+
return {};
|
|
151
|
+
try {
|
|
152
|
+
const parsed = Bun.JSONC.parse(readFileSync(path, "utf8"));
|
|
153
|
+
return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed : {};
|
|
154
|
+
} catch {
|
|
155
|
+
return {};
|
|
156
|
+
}
|
|
157
|
+
}
|
|
137
158
|
|
|
138
159
|
// ../kit/src/theme.ts
|
|
139
160
|
var ENDS = new Map;
|
|
@@ -200,7 +221,7 @@ function readThemeFile(path, warn) {
|
|
|
200
221
|
warn(`${path} is not a regular file under ${MAX_FILE_BYTES / 1024} KiB, theme ignored`);
|
|
201
222
|
return;
|
|
202
223
|
}
|
|
203
|
-
const parsed = JSON.parse(
|
|
224
|
+
const parsed = JSON.parse(readFileSync2(path, "utf8"));
|
|
204
225
|
if (isRecord(parsed))
|
|
205
226
|
return parsed;
|
|
206
227
|
warn(`${path} is not a JSON object, theme ignored`);
|
|
@@ -266,7 +287,7 @@ function applyFile(theme, file, where, warn) {
|
|
|
266
287
|
}
|
|
267
288
|
}
|
|
268
289
|
function endsWalk(home) {
|
|
269
|
-
return (dir) => dir === resolve2(home) ||
|
|
290
|
+
return (dir) => dir === resolve2(home) || existsSync3(join3(dir, ".git"));
|
|
270
291
|
}
|
|
271
292
|
var warned = new Set;
|
|
272
293
|
function warnOnce(message) {
|
|
@@ -322,7 +343,7 @@ function takesControlCodes(stream, env = process6.env) {
|
|
|
322
343
|
return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb" && outputMode(stream, env) === "terminal";
|
|
323
344
|
}
|
|
324
345
|
function paint(stream, env = process6.env) {
|
|
325
|
-
return
|
|
346
|
+
return Fe({ stream: { isTTY: takesControlCodes(stream, env) }, env });
|
|
326
347
|
}
|
|
327
348
|
function marked(mark, text, stream, detail, env = process6.env) {
|
|
328
349
|
const colors = paint(stream, env);
|
|
@@ -336,6 +357,9 @@ function marked(mark, text, stream, detail, env = process6.env) {
|
|
|
336
357
|
function ok(text, detail) {
|
|
337
358
|
console.log(marked("ok", text, process6.stdout, detail));
|
|
338
359
|
}
|
|
360
|
+
function warn(text, detail) {
|
|
361
|
+
console.error(marked("warn", text, process6.stderr, detail));
|
|
362
|
+
}
|
|
339
363
|
function role(name, stream, env = process6.env) {
|
|
340
364
|
const colors = paint(stream, env);
|
|
341
365
|
return takesControlCodes(stream, env) ? tinted(colors, activeTheme().theme.colors[name], env) : colors;
|
|
@@ -721,7 +745,7 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
|
|
|
721
745
|
var RESET_STYLE = `${ESC}[0m`;
|
|
722
746
|
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
723
747
|
// ../kit/src/cli.ts
|
|
724
|
-
import { readFileSync as
|
|
748
|
+
import { readFileSync as readFileSync3, realpathSync } from "fs";
|
|
725
749
|
import { dirname as dirname3, join as join5 } from "path";
|
|
726
750
|
import process12 from "process";
|
|
727
751
|
|
|
@@ -1022,7 +1046,7 @@ async function printCandidates(found) {
|
|
|
1022
1046
|
function packageVersion(entry = Bun.main) {
|
|
1023
1047
|
for (let dir = dirname3(realpathSync(entry));; dir = dirname3(dir)) {
|
|
1024
1048
|
try {
|
|
1025
|
-
const { version } = JSON.parse(
|
|
1049
|
+
const { version } = JSON.parse(readFileSync3(join5(dir, "package.json"), "utf8"));
|
|
1026
1050
|
if (typeof version === "string")
|
|
1027
1051
|
return version;
|
|
1028
1052
|
} catch {}
|
|
@@ -1080,7 +1104,7 @@ async function runMain({ name, version, run, redact, usage }, argv = process12.a
|
|
|
1080
1104
|
// ../kit/src/compose.ts
|
|
1081
1105
|
async function captureOutput(argv, { cwd, env } = {}) {
|
|
1082
1106
|
const run = Bun.spawn(argv, { ...cwd === undefined ? {} : { cwd }, env: { ...process.env, ...env }, stdin: "ignore", stdout: "pipe", stderr: "pipe" });
|
|
1083
|
-
const [stdout, stderr, code] = await Promise.all([run.stdout.text(), run.stderr.text(), run.exited]);
|
|
1107
|
+
const [stdout, stderr, code] = await Promise.all([new Response(run.stdout).text(), new Response(run.stderr).text(), run.exited]);
|
|
1084
1108
|
return { code, stdout, stderr };
|
|
1085
1109
|
}
|
|
1086
1110
|
async function compose(args, { flags = [], ...options } = {}) {
|
|
@@ -1088,6 +1112,11 @@ async function compose(args, { flags = [], ...options } = {}) {
|
|
|
1088
1112
|
}
|
|
1089
1113
|
// ../kit/src/folders.ts
|
|
1090
1114
|
var SKIPPED_FOLDERS = new Set(["node_modules", "vendor", "dist", "build", "target", "coverage", "venv", "__pycache__"]);
|
|
1115
|
+
function ignoredFolders(extra = [], where = {}) {
|
|
1116
|
+
const configured = readUserConfig("config", where).ignore;
|
|
1117
|
+
const names = Array.isArray(configured) ? configured.filter((name) => typeof name === "string" && name !== "") : [];
|
|
1118
|
+
return new Set([...SKIPPED_FOLDERS, ...extra, ...names]);
|
|
1119
|
+
}
|
|
1091
1120
|
// ../kit/src/secrets.ts
|
|
1092
1121
|
var SECRET_PART = /token|secret|passw|credential|private|dsn|apikey|accesskey|key$/i;
|
|
1093
1122
|
var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
|
|
@@ -1397,7 +1426,7 @@ function maskValue(key, value, known) {
|
|
|
1397
1426
|
var ENV_FILE = /^(?:\.env(?:\..+)?|.+\.env|\.envrc)$/;
|
|
1398
1427
|
var TEMPLATE = /\.(?:example|sample|template|dist|defaults)$/;
|
|
1399
1428
|
var KNOWN_DEPTH = 3;
|
|
1400
|
-
var SKIPPED =
|
|
1429
|
+
var SKIPPED = ignoredFolders([".git", ".kay"]);
|
|
1401
1430
|
var LOCK_WAIT_MS = 5000;
|
|
1402
1431
|
var LOCK_STALE_MS = 30000;
|
|
1403
1432
|
var REAP_STALE_MS = 5000;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-env",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.13.
|
|
4
|
+
"version": "0.13.1",
|
|
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",
|
|
@@ -37,6 +37,6 @@
|
|
|
37
37
|
"redact": "$npm_execpath src/redact.ts"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@kaynooo/cli-kit": "0.
|
|
40
|
+
"@kaynooo/cli-kit": "0.37.0"
|
|
41
41
|
}
|
|
42
42
|
}
|
package/src/commands/audit.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { ComposeFix } from '../fix'
|
|
|
3
3
|
import { stat } from 'node:fs/promises'
|
|
4
4
|
import { relative, resolve } from 'node:path'
|
|
5
5
|
import process from 'node:process'
|
|
6
|
-
import { fail, jsonOption, parse, printJson, role } from '@kaynooo/cli-kit'
|
|
6
|
+
import { fail, jsonOption, parse, printJson, role, warn } from '@kaynooo/cli-kit'
|
|
7
7
|
import { shellQuote } from '@kaynooo/cli-kit/shell'
|
|
8
8
|
import { audit as findExposed } from '../audit'
|
|
9
9
|
import { fixCompose, shutOthersOut } from '../fix'
|
|
@@ -90,11 +90,11 @@ export async function audit(args: string[]): Promise<number> {
|
|
|
90
90
|
|
|
91
91
|
// Container data folders owned by another account often run to dozens, so past a few they are only counted.
|
|
92
92
|
if (!json && unread.length > MAX_UNREAD_LISTED) {
|
|
93
|
-
|
|
93
|
+
warn(`could not read ${unread.length} folders, so they were not audited (--json lists them)`)
|
|
94
94
|
}
|
|
95
95
|
else if (!json) {
|
|
96
96
|
for (const dir of unread)
|
|
97
|
-
|
|
97
|
+
warn(`could not read ${dir}, so it was not audited`)
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
if (parsed.values.fix === true && (json || exposed.length > 0)) {
|
package/src/commands/edit.ts
CHANGED
|
@@ -7,25 +7,27 @@ const DEFAULT_FILE = '.env'
|
|
|
7
7
|
const TRAILING_NEWLINE = /\r?\n$/
|
|
8
8
|
const KEY_NAME = /^[A-Z_][\w.-]*$/i
|
|
9
9
|
|
|
10
|
-
const
|
|
10
|
+
const QUIET = { quiet: { type: 'boolean', short: 'q', description: 'print nothing on success' } } as const
|
|
11
|
+
|
|
12
|
+
const setUsage = `ev set KEY [file] [--quiet]
|
|
11
13
|
|
|
12
14
|
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
15
|
|
|
14
16
|
Exits 0 when it wrote the key, 1 when it could not, 2 on a usage error.`
|
|
15
17
|
|
|
16
|
-
const unsetUsage = `ev unset KEY [file]
|
|
18
|
+
const unsetUsage = `ev unset KEY [file] [--quiet]
|
|
17
19
|
|
|
18
20
|
Removes every line setting KEY from the file (.env by default), leaving comments and other lines as they are.
|
|
19
21
|
|
|
20
22
|
Exits 0 when it removed the key, 1 when the file does not set it, 2 on a usage error.`
|
|
21
23
|
|
|
22
|
-
const mvUsage = `ev mv KEY --from <file> --to <file>
|
|
24
|
+
const mvUsage = `ev mv KEY --from <file> --to <file> [--quiet]
|
|
23
25
|
|
|
24
26
|
Moves KEY's line from one file to another, as written. Refuses to replace a key the target already sets unless --force.
|
|
25
27
|
|
|
26
28
|
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
29
|
|
|
28
|
-
const cpUsage = `ev cp KEY --from <file> --to <file> [--as NEWKEY]
|
|
30
|
+
const cpUsage = `ev cp KEY --from <file> --to <file> [--as NEWKEY] [--quiet]
|
|
29
31
|
|
|
30
32
|
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
33
|
|
|
@@ -54,6 +56,7 @@ export async function set(args: string[]): Promise<number> {
|
|
|
54
56
|
const parsed = parse(args, setUsage, {
|
|
55
57
|
file: { type: 'string', short: 'F', description: 'read the value from this file instead of stdin' },
|
|
56
58
|
empty: { type: 'boolean', description: 'set an empty value, reading nothing' },
|
|
59
|
+
...QUIET,
|
|
57
60
|
})
|
|
58
61
|
|
|
59
62
|
const [key, target = DEFAULT_FILE] = parsed.positionals
|
|
@@ -77,13 +80,14 @@ export async function set(args: string[]): Promise<number> {
|
|
|
77
80
|
return current
|
|
78
81
|
})
|
|
79
82
|
|
|
80
|
-
|
|
83
|
+
if (parsed.values.quiet !== true)
|
|
84
|
+
console.log(`${target}: ${key}=${maskValue(key, value, await loadKnown(process.cwd(), [file])).replaceAll('\n', '\\n')}`)
|
|
81
85
|
|
|
82
86
|
return 0
|
|
83
87
|
}
|
|
84
88
|
|
|
85
89
|
export async function unset(args: string[]): Promise<number> {
|
|
86
|
-
const parsed = parse(args, unsetUsage,
|
|
90
|
+
const parsed = parse(args, unsetUsage, QUIET)
|
|
87
91
|
|
|
88
92
|
const [key, target = DEFAULT_FILE] = parsed.positionals
|
|
89
93
|
|
|
@@ -99,7 +103,8 @@ export async function unset(args: string[]): Promise<number> {
|
|
|
99
103
|
await writeEnv(target, file)
|
|
100
104
|
})
|
|
101
105
|
|
|
102
|
-
|
|
106
|
+
if (parsed.values.quiet !== true)
|
|
107
|
+
console.log(`${target}: removed ${key}`)
|
|
103
108
|
|
|
104
109
|
return 0
|
|
105
110
|
}
|
|
@@ -139,6 +144,7 @@ export async function mv(args: string[]): Promise<number> {
|
|
|
139
144
|
from: { type: 'string', description: 'the file to take KEY from' },
|
|
140
145
|
to: { type: 'string', description: 'the file to put KEY in, created when missing' },
|
|
141
146
|
force: { type: 'boolean', description: 'replace KEY when the target already sets it' },
|
|
147
|
+
...QUIET,
|
|
142
148
|
})
|
|
143
149
|
|
|
144
150
|
const [key] = parsed.positionals
|
|
@@ -154,7 +160,8 @@ export async function mv(args: string[]): Promise<number> {
|
|
|
154
160
|
await writeEnv(from, source)
|
|
155
161
|
})
|
|
156
162
|
|
|
157
|
-
|
|
163
|
+
if (parsed.values.quiet !== true)
|
|
164
|
+
console.log(`${key}: ${from} -> ${to}`)
|
|
158
165
|
|
|
159
166
|
return 0
|
|
160
167
|
}
|
|
@@ -164,6 +171,7 @@ export async function cp(args: string[]): Promise<number> {
|
|
|
164
171
|
from: { type: 'string', description: 'the file to take KEY from' },
|
|
165
172
|
to: { type: 'string', description: 'the file to put the copy in, created when missing' },
|
|
166
173
|
as: { type: 'string', description: 'store the copy under this key instead' },
|
|
174
|
+
...QUIET,
|
|
167
175
|
})
|
|
168
176
|
|
|
169
177
|
const [key] = parsed.positionals
|
|
@@ -173,9 +181,9 @@ export async function cp(args: string[]): Promise<number> {
|
|
|
173
181
|
fail(`needs KEY, --from and --to\n\n${cpUsage}`)
|
|
174
182
|
|
|
175
183
|
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
184
|
|
|
178
|
-
|
|
185
|
+
if (parsed.values.quiet !== true)
|
|
186
|
+
console.log(`${to}: ${name}=${maskValue(name, value, await loadKnown(process.cwd(), [source, target])).replaceAll('\n', '\\n')}`)
|
|
179
187
|
|
|
180
188
|
return 0
|
|
181
189
|
}
|
package/src/files.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { Dotenv } from './dotenv'
|
|
|
3
3
|
import type { KnownSecrets } from './mask'
|
|
4
4
|
import { chmod, chown, readdir, realpath, rename, rm, stat, writeFile } from 'node:fs/promises'
|
|
5
5
|
import { basename, dirname, join } from 'node:path'
|
|
6
|
-
import { fail,
|
|
6
|
+
import { fail, ignoredFolders, isSecretName } from '@kaynooo/cli-kit'
|
|
7
7
|
import { entries, parseDotenv, serializeDotenv } from './dotenv'
|
|
8
8
|
import { knownSecrets, urlPasswords } from './mask'
|
|
9
9
|
|
|
@@ -14,7 +14,7 @@ export const TEMPLATE = /\.(?:example|sample|template|dist|defaults)$/
|
|
|
14
14
|
// Folders searched for secrets below the current one, and the ones never entered.
|
|
15
15
|
const KNOWN_DEPTH = 3
|
|
16
16
|
|
|
17
|
-
export const SKIPPED =
|
|
17
|
+
export const SKIPPED = ignoredFolders(['.git', '.kay'])
|
|
18
18
|
const LOCK_WAIT_MS = 5000
|
|
19
19
|
const LOCK_STALE_MS = 30_000
|
|
20
20
|
// A reap takes a few milliseconds, so a guard older than this belongs to a reaper that died.
|