@runlocal-ai/cli 0.3.0 → 0.3.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 +7 -1
- package/dist/rlx.mjs +280 -35
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,6 +29,10 @@ the code, and approve the sign-in. Then `rlx` offers to store an API key in a
|
|
|
29
29
|
file in your home folder, so that it works next time. `rlx login --no-key`
|
|
30
30
|
keeps the session only.
|
|
31
31
|
|
|
32
|
+
If `RUNLOCAL_API_KEY` is set, `rlx login` checks that key and uses it, with
|
|
33
|
+
no browser; it does not store the key. With no terminal and no key, `rlx login`
|
|
34
|
+
prints the address and the code, and waits at most 10 minutes.
|
|
35
|
+
|
|
32
36
|
After sign-in, run `rlx help` to see your commands.
|
|
33
37
|
|
|
34
38
|
`rlx logout` removes the stored key from this computer. It does not revoke the
|
|
@@ -41,9 +45,11 @@ key: you can revoke it in the workspace.
|
|
|
41
45
|
| `--json` | Prints the result as JSON, for a program. |
|
|
42
46
|
| `--yes`, `-y` | Answers yes to each confirmation. |
|
|
43
47
|
| `--api <origin>` | Uses this API origin. |
|
|
44
|
-
| `--quiet`, `-q` | Prints the result only. |
|
|
48
|
+
| `--quiet`, `-q` | Prints the result only, and errors. |
|
|
45
49
|
| `--debug` | Prints each request. Credentials are never printed. |
|
|
46
50
|
|
|
51
|
+
`rlx --version` prints the version. It goes before any command.
|
|
52
|
+
|
|
47
53
|
Exit codes: 0 success, 1 user error, 2 not signed in, 3 server error,
|
|
48
54
|
4 cancelled.
|
|
49
55
|
|
package/dist/rlx.mjs
CHANGED
|
@@ -7416,7 +7416,122 @@ var known = /* @__PURE__ */ new Set();
|
|
|
7416
7416
|
function registerSecret(value) {
|
|
7417
7417
|
if (typeof value === "string" && value.length >= 8) known.add(value);
|
|
7418
7418
|
}
|
|
7419
|
-
var
|
|
7419
|
+
var STRONG_WORDS = /* @__PURE__ */ new Set(["password", "passwd", "passphrase", "secret", "secrets", "authorization", "apikey", "cookie"]);
|
|
7420
|
+
var STRONG_PAIRS = /* @__PURE__ */ new Set([
|
|
7421
|
+
"api key",
|
|
7422
|
+
"private key",
|
|
7423
|
+
"access key",
|
|
7424
|
+
"signing key",
|
|
7425
|
+
"secret key",
|
|
7426
|
+
"access token",
|
|
7427
|
+
"refresh token",
|
|
7428
|
+
"id token",
|
|
7429
|
+
"session token",
|
|
7430
|
+
"bearer token",
|
|
7431
|
+
"auth token",
|
|
7432
|
+
"device code",
|
|
7433
|
+
"client secret"
|
|
7434
|
+
]);
|
|
7435
|
+
var WEAK_WORDS = /* @__PURE__ */ new Set(["token", "key", "credential", "credentials", "auth", "signature", "sig"]);
|
|
7436
|
+
var STRONG_PARTS = ["password", "passwd", "passphrase", "secret", "apikey", "privatekey", "accesstoken", "refreshtoken", "devicecode"];
|
|
7437
|
+
var WEAK_PARTS = ["token", "credential"];
|
|
7438
|
+
var DESCRIBING_WORDS = /* @__PURE__ */ new Set([
|
|
7439
|
+
"type",
|
|
7440
|
+
"types",
|
|
7441
|
+
"kind",
|
|
7442
|
+
"kinds",
|
|
7443
|
+
"hint",
|
|
7444
|
+
"prefix",
|
|
7445
|
+
"suffix",
|
|
7446
|
+
"last4",
|
|
7447
|
+
"name",
|
|
7448
|
+
"names",
|
|
7449
|
+
"label",
|
|
7450
|
+
"count",
|
|
7451
|
+
"length",
|
|
7452
|
+
"url",
|
|
7453
|
+
"uri",
|
|
7454
|
+
"endpoint",
|
|
7455
|
+
"env",
|
|
7456
|
+
"var",
|
|
7457
|
+
"file",
|
|
7458
|
+
"path",
|
|
7459
|
+
"mode",
|
|
7460
|
+
"days",
|
|
7461
|
+
"format",
|
|
7462
|
+
"scheme",
|
|
7463
|
+
"method",
|
|
7464
|
+
"status",
|
|
7465
|
+
"state",
|
|
7466
|
+
"scope",
|
|
7467
|
+
"scopes",
|
|
7468
|
+
"expires",
|
|
7469
|
+
"expiry",
|
|
7470
|
+
"ttl",
|
|
7471
|
+
"at",
|
|
7472
|
+
"set",
|
|
7473
|
+
"present",
|
|
7474
|
+
"configured",
|
|
7475
|
+
"required",
|
|
7476
|
+
"enabled",
|
|
7477
|
+
"version",
|
|
7478
|
+
"header",
|
|
7479
|
+
"field",
|
|
7480
|
+
"source",
|
|
7481
|
+
"policy",
|
|
7482
|
+
"rotation",
|
|
7483
|
+
"created",
|
|
7484
|
+
"updated",
|
|
7485
|
+
"used",
|
|
7486
|
+
"algorithm",
|
|
7487
|
+
"alg"
|
|
7488
|
+
]);
|
|
7489
|
+
function wordsOf(name) {
|
|
7490
|
+
return name.replace(/([a-z0-9])([A-Z])/g, "$1 $2").toLowerCase().split(/[^a-z0-9]+/).filter(Boolean);
|
|
7491
|
+
}
|
|
7492
|
+
function secretName(name) {
|
|
7493
|
+
const words = wordsOf(name);
|
|
7494
|
+
if (words.length === 0) return void 0;
|
|
7495
|
+
if (DESCRIBING_WORDS.has(words[words.length - 1]) && words.length > 1) return void 0;
|
|
7496
|
+
if (words.some((word) => STRONG_WORDS.has(word))) return "strong";
|
|
7497
|
+
for (let i = 1; i < words.length; i++) if (STRONG_PAIRS.has(`${words[i - 1]} ${words[i]}`)) return "strong";
|
|
7498
|
+
if (words.some((word) => WEAK_WORDS.has(word))) return "weak";
|
|
7499
|
+
const joined = words.join("");
|
|
7500
|
+
if (STRONG_PARTS.some((part) => joined.includes(part))) return "strong";
|
|
7501
|
+
if (WEAK_PARTS.some((part) => joined.includes(part))) return "weak";
|
|
7502
|
+
return void 0;
|
|
7503
|
+
}
|
|
7504
|
+
function plainWord(value) {
|
|
7505
|
+
if (/^\d{1,10}$/.test(value)) return true;
|
|
7506
|
+
if (value.length > 24 || !/^[A-Za-z][A-Za-z0-9_.:/-]*$/.test(value)) return false;
|
|
7507
|
+
return (value.match(/[0-9]/g)?.length ?? 0) <= 3;
|
|
7508
|
+
}
|
|
7509
|
+
function entropy(text) {
|
|
7510
|
+
const counts = /* @__PURE__ */ new Map();
|
|
7511
|
+
for (const char of text) counts.set(char, (counts.get(char) ?? 0) + 1);
|
|
7512
|
+
let bits = 0;
|
|
7513
|
+
for (const count of counts.values()) {
|
|
7514
|
+
const p = count / text.length;
|
|
7515
|
+
bits -= p * Math.log2(p);
|
|
7516
|
+
}
|
|
7517
|
+
return bits;
|
|
7518
|
+
}
|
|
7519
|
+
function looksRandom(text) {
|
|
7520
|
+
const body = text.replace(/=+$/, "");
|
|
7521
|
+
if (body.length < 40 || !/[a-z]/.test(body) || !/[A-Z]/.test(body) || !/[0-9]/.test(body)) return false;
|
|
7522
|
+
if (entropy(body) < 4.3) return false;
|
|
7523
|
+
const classes = [...body].flatMap((char) => /[a-z]/.test(char) ? [0] : /[A-Z]/.test(char) ? [1] : /[0-9]/.test(char) ? [2] : []);
|
|
7524
|
+
let changes = 0;
|
|
7525
|
+
for (let i = 1; i < classes.length; i++) if (classes[i] !== classes[i - 1]) changes++;
|
|
7526
|
+
return changes / (classes.length - 1) >= 0.45;
|
|
7527
|
+
}
|
|
7528
|
+
function secretField(name, value) {
|
|
7529
|
+
if (value.length === 0) return false;
|
|
7530
|
+
const kind = secretName(name);
|
|
7531
|
+
if (kind === "strong") return true;
|
|
7532
|
+
if (kind === "weak") return !plainWord(value);
|
|
7533
|
+
return false;
|
|
7534
|
+
}
|
|
7420
7535
|
var RULES = [
|
|
7421
7536
|
// Authorization: Bearer <value>, Basic <value>
|
|
7422
7537
|
[/(?<![A-Za-z0-9])(Bearer|Basic)(\s+)[A-Za-z0-9._~+/=-]{6,}/gi, (_m, scheme, space) => `${scheme}${space}${MASK}`],
|
|
@@ -7427,15 +7542,17 @@ var RULES = [
|
|
|
7427
7542
|
// key stands apart from letters and digits; an underscore next to it, as in
|
|
7428
7543
|
// NAME_tok_..., does not hide it.
|
|
7429
7544
|
[/(?<![A-Za-z0-9])([a-z]{2,8}_)([A-Za-z0-9]{8})[A-Za-z0-9]{32,}(?![A-Za-z0-9])/g, (_m, prefix, start) => `${prefix}${start}\u2026`],
|
|
7545
|
+
// A long random string of base64 or base64url, standing apart.
|
|
7546
|
+
[/(?<![A-Za-z0-9+/_-])[A-Za-z0-9+/_-]{40,}={0,2}(?![A-Za-z0-9+/=_-])/g, (match) => looksRandom(match) ? MASK : match],
|
|
7430
7547
|
// "name": "value" in JSON, where the name says that the value is secret.
|
|
7431
7548
|
[
|
|
7432
|
-
|
|
7433
|
-
(
|
|
7549
|
+
/("([A-Za-z0-9_.-]{1,64})"\s*:\s*")((?:[^"\\]|\\.)*)(")/g,
|
|
7550
|
+
(match, before, name, value, after) => secretField(name, value) ? `${before}${MASK}${after}` : match
|
|
7434
7551
|
],
|
|
7435
7552
|
// name=value in a query, a form, or the environment.
|
|
7436
7553
|
[
|
|
7437
|
-
|
|
7438
|
-
(
|
|
7554
|
+
/\b([A-Za-z0-9_.-]{1,64})=([^&\s"']+)/g,
|
|
7555
|
+
(match, name, value) => value !== MASK && secretField(name, value) ? `${name}=${MASK}` : match
|
|
7439
7556
|
]
|
|
7440
7557
|
];
|
|
7441
7558
|
function mask(text) {
|
|
@@ -7521,7 +7638,7 @@ var Api = class {
|
|
|
7521
7638
|
registerSecret(token);
|
|
7522
7639
|
const headers = {
|
|
7523
7640
|
Accept: "application/json",
|
|
7524
|
-
"User-Agent": `rlx/${"0.3.
|
|
7641
|
+
"User-Agent": `rlx/${"0.3.1"}`
|
|
7525
7642
|
};
|
|
7526
7643
|
const named = (name) => Object.keys(headers).find((key) => key.toLowerCase() === name.toLowerCase());
|
|
7527
7644
|
for (const [name, value] of Object.entries(req.headers ?? {})) {
|
|
@@ -7583,6 +7700,9 @@ var Api = class {
|
|
|
7583
7700
|
};
|
|
7584
7701
|
|
|
7585
7702
|
// src/args.ts
|
|
7703
|
+
function versionHint(token) {
|
|
7704
|
+
return token === "--version" || token === "-V" ? " rlx --version prints the version of rlx: give it first, with no command." : "";
|
|
7705
|
+
}
|
|
7586
7706
|
function parseGlobals(argv) {
|
|
7587
7707
|
const flags = {
|
|
7588
7708
|
json: false,
|
|
@@ -7595,6 +7715,7 @@ function parseGlobals(argv) {
|
|
|
7595
7715
|
};
|
|
7596
7716
|
let noKey = false;
|
|
7597
7717
|
const rest = [];
|
|
7718
|
+
let afterCommand = false;
|
|
7598
7719
|
for (let i = 0; i < argv.length; i++) {
|
|
7599
7720
|
const arg = argv[i];
|
|
7600
7721
|
if (arg === "--") {
|
|
@@ -7622,6 +7743,7 @@ function parseGlobals(argv) {
|
|
|
7622
7743
|
continue;
|
|
7623
7744
|
case "--version":
|
|
7624
7745
|
case "-V":
|
|
7746
|
+
if (afterCommand) break;
|
|
7625
7747
|
flags.version = true;
|
|
7626
7748
|
continue;
|
|
7627
7749
|
case "--no-key":
|
|
@@ -7639,6 +7761,7 @@ function parseGlobals(argv) {
|
|
|
7639
7761
|
flags.api = arg.slice("--api=".length);
|
|
7640
7762
|
continue;
|
|
7641
7763
|
}
|
|
7764
|
+
if (!arg.startsWith("-")) afterCommand = true;
|
|
7642
7765
|
rest.push(arg);
|
|
7643
7766
|
}
|
|
7644
7767
|
return { flags, rest, noKey };
|
|
@@ -7735,7 +7858,7 @@ function parseCommandArgs(command, argv, options = {}) {
|
|
|
7735
7858
|
inline = token.length > 2 ? token.slice(2) : void 0;
|
|
7736
7859
|
arg2 = byShort.get(name);
|
|
7737
7860
|
}
|
|
7738
|
-
if (!arg2) throw userError(`Unknown option ${token}
|
|
7861
|
+
if (!arg2) throw userError(`Unknown option ${token}.${versionHint(token)} ${helpHint}`);
|
|
7739
7862
|
if (arg2.kind === "flag") {
|
|
7740
7863
|
set(arg2, inline === void 0 ? true : coerce(arg2, inline));
|
|
7741
7864
|
continue;
|
|
@@ -7757,6 +7880,17 @@ function parseCommandArgs(command, argv, options = {}) {
|
|
|
7757
7880
|
}
|
|
7758
7881
|
}
|
|
7759
7882
|
if (options.lenient) return values;
|
|
7883
|
+
for (const arg of positionals) {
|
|
7884
|
+
const value = options.fill?.[arg.name];
|
|
7885
|
+
if (values[arg.name] !== void 0 || value === void 0 || value === null) continue;
|
|
7886
|
+
if (Array.isArray(value) && value.length === 0) continue;
|
|
7887
|
+
const items = Array.isArray(value) ? value : [value];
|
|
7888
|
+
if (Array.isArray(value) && !arg.repeat || items.some((item) => !["string", "number", "boolean"].includes(typeof item))) {
|
|
7889
|
+
throw userError(`The input file gives ${label(arg)} a value that is not ${arg.repeat ? "a value or a list of values" : "one value"}. ${helpHint}`);
|
|
7890
|
+
}
|
|
7891
|
+
const coerced = items.map((item) => coerce(arg, String(item)));
|
|
7892
|
+
values[arg.name] = arg.repeat ? coerced : coerced[0];
|
|
7893
|
+
}
|
|
7760
7894
|
for (const arg of specs) {
|
|
7761
7895
|
if (values[arg.name] === void 0 && arg.default !== void 0) values[arg.name] = arg.default;
|
|
7762
7896
|
if (values[arg.name] === void 0 && arg.kind === "flag") values[arg.name] = false;
|
|
@@ -8584,20 +8718,33 @@ async function requireSignIn(io, out, flags) {
|
|
|
8584
8718
|
if (!signed) throw notSignedIn();
|
|
8585
8719
|
return signed;
|
|
8586
8720
|
}
|
|
8587
|
-
async function
|
|
8588
|
-
if (signed.source !== "file") return;
|
|
8721
|
+
async function refusedCredential(io, out, signed, error) {
|
|
8589
8722
|
const api = new Api(io, out, signed.origin, async () => signed.token);
|
|
8590
8723
|
try {
|
|
8591
8724
|
await api.request({ method: "GET", path: AUTH_STATUS_PATH });
|
|
8592
|
-
return;
|
|
8593
|
-
} catch (
|
|
8594
|
-
if (
|
|
8725
|
+
return error;
|
|
8726
|
+
} catch (statusError) {
|
|
8727
|
+
if (statusError.status !== 401) return error;
|
|
8595
8728
|
}
|
|
8729
|
+
return await keyRefused(io, signed, error);
|
|
8730
|
+
}
|
|
8731
|
+
async function keyRefused(io, signed, error) {
|
|
8732
|
+
const { keyEnv } = signed.config.credentials;
|
|
8733
|
+
const place = signed.config.workspaceUrl ? ` in the workspace at ${signed.config.workspaceUrl}` : " in the workspace";
|
|
8734
|
+
const options = { exitCode: EXIT.notSignedIn, code: error?.code ?? "AUTH.REQUIRED", status: 401, callId: error?.callId };
|
|
8735
|
+
if (signed.source === "env") {
|
|
8736
|
+
return new CliError(`The API refused the key from ${keyEnv}: it is invalid or revoked.`, {
|
|
8737
|
+
...options,
|
|
8738
|
+
hint: `Set ${keyEnv} to a valid API key (you can create one${place}), or unset it and run: rlx login`
|
|
8739
|
+
});
|
|
8740
|
+
}
|
|
8741
|
+
const file = keyFilePath(io, signed.config.credentials);
|
|
8596
8742
|
await removeKeyEntry(io, signed.config.credentials, signed.origin);
|
|
8597
8743
|
await dropManifestCaches(io, signed.origin);
|
|
8598
|
-
|
|
8599
|
-
|
|
8600
|
-
|
|
8744
|
+
return new CliError(`The API refused the stored key in ${file}: it is invalid or revoked, so rlx removed it.`, {
|
|
8745
|
+
...options,
|
|
8746
|
+
hint: "Run: rlx login"
|
|
8747
|
+
});
|
|
8601
8748
|
}
|
|
8602
8749
|
|
|
8603
8750
|
// src/commands/help.ts
|
|
@@ -8611,7 +8758,7 @@ var GLOBAL_OPTIONS2 = [
|
|
|
8611
8758
|
["--json", "Print the result as JSON, for a program."],
|
|
8612
8759
|
["--yes, -y", "Answer yes to each confirmation."],
|
|
8613
8760
|
["--api <origin>", "Use this API origin."],
|
|
8614
|
-
["--quiet, -q", "Print the result only."],
|
|
8761
|
+
["--quiet, -q", "Print the result only, and errors."],
|
|
8615
8762
|
["--debug", "Print each request. Credentials are never printed."]
|
|
8616
8763
|
];
|
|
8617
8764
|
function columns(rows, indent = " ") {
|
|
@@ -8700,7 +8847,9 @@ function builtInHelp(word) {
|
|
|
8700
8847
|
"",
|
|
8701
8848
|
"Signs in with a code that you approve in a browser.",
|
|
8702
8849
|
"Then rlx offers to store an API key in the key file of your home folder.",
|
|
8703
|
-
"--no-key keeps the session only. --yes stores the key without a question."
|
|
8850
|
+
"--no-key keeps the session only. --yes stores the key without a question.",
|
|
8851
|
+
"When the API key variable of the origin is set, rlx checks that key instead,",
|
|
8852
|
+
"and does not store it. With no terminal, rlx waits at most 10 minutes."
|
|
8704
8853
|
].join("\n");
|
|
8705
8854
|
case "logout":
|
|
8706
8855
|
return [
|
|
@@ -8839,12 +8988,19 @@ var Session = class {
|
|
|
8839
8988
|
this.accept(body);
|
|
8840
8989
|
}
|
|
8841
8990
|
};
|
|
8842
|
-
async function waitForApproval(io, config, device, debug) {
|
|
8991
|
+
async function waitForApproval(io, config, device, debug, options = {}) {
|
|
8843
8992
|
let interval = device.interval;
|
|
8844
|
-
const
|
|
8993
|
+
const seconds = Math.min(device.expiresIn, options.maxSeconds ?? device.expiresIn);
|
|
8994
|
+
const deadline = io.now() + seconds * 1e3;
|
|
8845
8995
|
for (; ; ) {
|
|
8996
|
+
if (io.now() + interval * 1e3 > deadline) {
|
|
8997
|
+
const waited = Math.max(1, Math.round(seconds / 60));
|
|
8998
|
+
throw new CliError(`No one approved the sign-in within ${waited} minute${waited === 1 ? "" : "s"}, so rlx stopped waiting. Run rlx login again.`, {
|
|
8999
|
+
code: "SIGN_IN.TIMEOUT",
|
|
9000
|
+
hint: options.timeoutHint
|
|
9001
|
+
});
|
|
9002
|
+
}
|
|
8846
9003
|
await io.sleep(interval * 1e3);
|
|
8847
|
-
if (io.now() > deadline) throw new CliError("The sign-in code expired. Run rlx login again.");
|
|
8848
9004
|
const { status, body } = await postForm(
|
|
8849
9005
|
io,
|
|
8850
9006
|
config.tokenEndpoint,
|
|
@@ -8875,16 +9031,22 @@ async function login(io, out, flags, noKey) {
|
|
|
8875
9031
|
const debug = (line) => out.debug(line);
|
|
8876
9032
|
const origin = await resolveOrigin(io, flags.api);
|
|
8877
9033
|
const config = await loadClientConfig(io, origin, { refresh: true, debug });
|
|
9034
|
+
const fromEnv = io.env[config.credentials.keyEnv]?.trim();
|
|
9035
|
+
if (fromEnv) return await loginWithEnvKey(io, out, flags, origin, config, fromEnv);
|
|
8878
9036
|
const device = await startDeviceAuthorization(io, config, debug);
|
|
8879
|
-
|
|
9037
|
+
const wait = approvalWait(io, device.expiresIn);
|
|
9038
|
+
out.error(
|
|
8880
9039
|
[
|
|
8881
9040
|
"To sign in, open this address in a browser:",
|
|
8882
9041
|
` ${device.verificationUriComplete ?? device.verificationUri}`,
|
|
8883
9042
|
`and check that it shows this code: ${device.userCode}`,
|
|
8884
|
-
|
|
9043
|
+
`Waiting for you to approve the sign-in (at most ${minutes(wait)})...`
|
|
8885
9044
|
].join("\n")
|
|
8886
9045
|
);
|
|
8887
|
-
const session = await waitForApproval(io, config, device, debug
|
|
9046
|
+
const session = await waitForApproval(io, config, device, debug, {
|
|
9047
|
+
maxSeconds: wait,
|
|
9048
|
+
timeoutHint: io.interactive ? void 0 : `With no terminal, set ${config.credentials.keyEnv} to an API key in place of rlx login.`
|
|
9049
|
+
});
|
|
8888
9050
|
const api = new Api(io, out, origin, () => session.token());
|
|
8889
9051
|
const status = (await api.request({ method: "GET", path: AUTH_STATUS_PATH })).body;
|
|
8890
9052
|
const email = typeof status?.email === "string" ? status.email : void 0;
|
|
@@ -8914,6 +9076,36 @@ async function login(io, out, flags, noKey) {
|
|
|
8914
9076
|
if (flags.json) out.result(JSON.stringify({ signed_in: true, origin, email: email ?? null, key_stored: stored }));
|
|
8915
9077
|
return EXIT.ok;
|
|
8916
9078
|
}
|
|
9079
|
+
var MAX_WAIT_SECONDS = 30 * 60;
|
|
9080
|
+
var MAX_WAIT_SECONDS_NO_TTY = 10 * 60;
|
|
9081
|
+
function approvalWait(io, expiresIn) {
|
|
9082
|
+
return Math.max(1, Math.min(expiresIn, io.interactive ? MAX_WAIT_SECONDS : MAX_WAIT_SECONDS_NO_TTY));
|
|
9083
|
+
}
|
|
9084
|
+
function minutes(seconds) {
|
|
9085
|
+
const n = Math.max(1, Math.round(seconds / 60));
|
|
9086
|
+
return `${n} minute${n === 1 ? "" : "s"}`;
|
|
9087
|
+
}
|
|
9088
|
+
async function loginWithEnvKey(io, out, flags, origin, config, key) {
|
|
9089
|
+
registerSecret(key);
|
|
9090
|
+
const { keyEnv, keyFile } = config.credentials;
|
|
9091
|
+
const api = new Api(io, out, origin, async () => key);
|
|
9092
|
+
let status;
|
|
9093
|
+
try {
|
|
9094
|
+
status = (await api.request({ method: "GET", path: AUTH_STATUS_PATH })).body;
|
|
9095
|
+
} catch (error) {
|
|
9096
|
+
if (error instanceof CliError && error.status === 401) throw await keyRefused(io, { origin, config, token: key, source: "env" }, error);
|
|
9097
|
+
throw error;
|
|
9098
|
+
}
|
|
9099
|
+
const email = typeof status?.email === "string" ? status.email : void 0;
|
|
9100
|
+
const settings = await readSettings(io);
|
|
9101
|
+
await writeSettings(io, { ...settings, origin });
|
|
9102
|
+
out.info(`Signed in with the key from ${keyEnv}, to ${origin}${email ? ` as ${email}` : ""}.`);
|
|
9103
|
+
out.info(`rlx did not store the key in ${keyFile}: it works while ${keyEnv} is set. To store a key of its own, unset ${keyEnv} and run rlx login.`);
|
|
9104
|
+
if (flags.json) {
|
|
9105
|
+
out.result(JSON.stringify({ signed_in: true, origin, email: email ?? null, key_stored: false, key_source: "env" }));
|
|
9106
|
+
}
|
|
9107
|
+
return EXIT.ok;
|
|
9108
|
+
}
|
|
8917
9109
|
async function storeKey(io, out, api, origin, convention, where, workspaceUrl) {
|
|
8918
9110
|
const { manifest } = await loadManifest(api, io, "", { cache: false });
|
|
8919
9111
|
const path = manifest.auth?.api_keys;
|
|
@@ -9537,6 +9729,45 @@ async function runCommand(ctx, command, args) {
|
|
|
9537
9729
|
return data;
|
|
9538
9730
|
}
|
|
9539
9731
|
|
|
9732
|
+
// src/engine/input.ts
|
|
9733
|
+
var import_yaml2 = __toESM(require_dist(), 1);
|
|
9734
|
+
import { readFile as readFile3, stat as stat5 } from "node:fs/promises";
|
|
9735
|
+
import { extname as extname2 } from "node:path";
|
|
9736
|
+
var INPUT_OPTION = "input";
|
|
9737
|
+
var MAX_INPUT_BYTES = 256 * 1024;
|
|
9738
|
+
var INPUT_EXTENSIONS = /* @__PURE__ */ new Set([".yaml", ".yml", ".json"]);
|
|
9739
|
+
function inputOption(command) {
|
|
9740
|
+
return (command.args ?? []).find((arg) => arg.name === INPUT_OPTION && arg.kind === "option" && arg.type === "path");
|
|
9741
|
+
}
|
|
9742
|
+
function keysOf(name) {
|
|
9743
|
+
const snake = name.replace(/-/g, "_");
|
|
9744
|
+
return snake === name ? [name] : [snake, name];
|
|
9745
|
+
}
|
|
9746
|
+
async function positionalsFromInput(ctx, command, given) {
|
|
9747
|
+
const option = inputOption(command);
|
|
9748
|
+
const file = option ? given[option.name] : void 0;
|
|
9749
|
+
const missing = (command.args ?? []).filter((arg) => arg.kind === "positional" && given[arg.name] === void 0);
|
|
9750
|
+
if (typeof file !== "string" || !file || missing.length === 0) return {};
|
|
9751
|
+
const path = localFile(ctx, file);
|
|
9752
|
+
if (!INPUT_EXTENSIONS.has(extname2(path).toLowerCase())) return {};
|
|
9753
|
+
let doc;
|
|
9754
|
+
try {
|
|
9755
|
+
const info = await stat5(path);
|
|
9756
|
+
if (!info.isFile() || info.size > MAX_INPUT_BYTES) return {};
|
|
9757
|
+
const text = await readFile3(path, "utf8");
|
|
9758
|
+
doc = extname2(path).toLowerCase() === ".json" ? JSON.parse(text) : (0, import_yaml2.parse)(text, { maxAliasCount: 50 });
|
|
9759
|
+
} catch {
|
|
9760
|
+
return {};
|
|
9761
|
+
}
|
|
9762
|
+
if (typeof doc !== "object" || doc === null || Array.isArray(doc)) return {};
|
|
9763
|
+
const values = {};
|
|
9764
|
+
for (const arg of missing) {
|
|
9765
|
+
const key = keysOf(arg.name).find((name) => Object.hasOwn(doc, name));
|
|
9766
|
+
if (key !== void 0) values[arg.name] = doc[key];
|
|
9767
|
+
}
|
|
9768
|
+
return values;
|
|
9769
|
+
}
|
|
9770
|
+
|
|
9540
9771
|
// src/util/io.ts
|
|
9541
9772
|
import { createInterface } from "node:readline/promises";
|
|
9542
9773
|
import { homedir } from "node:os";
|
|
@@ -9579,8 +9810,17 @@ var Out = class {
|
|
|
9579
9810
|
this.io.writeError(mask(`${text}
|
|
9580
9811
|
`));
|
|
9581
9812
|
}
|
|
9582
|
-
/** A warning, on standard error. --quiet
|
|
9813
|
+
/** A warning, on standard error. --quiet hides it, as it hides each line that is not the result. */
|
|
9583
9814
|
warn(text) {
|
|
9815
|
+
if (this.flags.quiet) return;
|
|
9816
|
+
this.io.writeError(mask(`${text}
|
|
9817
|
+
`));
|
|
9818
|
+
}
|
|
9819
|
+
/**
|
|
9820
|
+
* An error, or a line that the person must see to go on (such as the
|
|
9821
|
+
* address of the sign-in), on standard error. --quiet does not hide it.
|
|
9822
|
+
*/
|
|
9823
|
+
error(text) {
|
|
9584
9824
|
this.io.writeError(mask(`${text}
|
|
9585
9825
|
`));
|
|
9586
9826
|
}
|
|
@@ -9621,10 +9861,11 @@ async function help(io, out, flags, words) {
|
|
|
9621
9861
|
try {
|
|
9622
9862
|
manifest = (await loadManifest(signedApi(io, out, signed), io, signed.token)).manifest;
|
|
9623
9863
|
} catch (error) {
|
|
9624
|
-
if (error instanceof CliError
|
|
9625
|
-
|
|
9626
|
-
|
|
9627
|
-
throw
|
|
9864
|
+
if (!(error instanceof CliError) || error.status !== 401) throw error;
|
|
9865
|
+
const refused = await refusedCredential(io, out, signed, error);
|
|
9866
|
+
if (refused !== error) {
|
|
9867
|
+
if (words.length) throw refused;
|
|
9868
|
+
out.warn([refused.message, refused.hint].filter(Boolean).join(" "));
|
|
9628
9869
|
}
|
|
9629
9870
|
}
|
|
9630
9871
|
}
|
|
@@ -9666,12 +9907,14 @@ async function runManifestCommand(io, out, flags, rest) {
|
|
|
9666
9907
|
protectedPaths: [configDir(io), join10(keyFilePath(io, signed.config.credentials), "..")]
|
|
9667
9908
|
};
|
|
9668
9909
|
if (flags.help) return await commandHelpWithExplain(ctx, found.command, rest.slice(found.used));
|
|
9669
|
-
const
|
|
9910
|
+
const argv = rest.slice(found.used);
|
|
9911
|
+
const fill = await positionalsFromInput(ctx, found.command, parseCommandArgs(found.command, argv, { lenient: true }));
|
|
9912
|
+
const args = parseCommandArgs(found.command, argv, { fill });
|
|
9670
9913
|
const data = await runCommand(ctx, found.command, args);
|
|
9671
9914
|
await printOutput(ctx, found.command.output, data, args);
|
|
9672
9915
|
return EXIT.ok;
|
|
9673
9916
|
} catch (error) {
|
|
9674
|
-
if (error instanceof CliError && error.status === 401) await
|
|
9917
|
+
if (error instanceof CliError && error.status === 401) throw await refusedCredential(io, out, signed, error);
|
|
9675
9918
|
throw error;
|
|
9676
9919
|
}
|
|
9677
9920
|
}
|
|
@@ -9721,9 +9964,9 @@ function report(out, flags, error) {
|
|
|
9721
9964
|
}
|
|
9722
9965
|
}));
|
|
9723
9966
|
} else {
|
|
9724
|
-
out.
|
|
9725
|
-
if (cli?.callId) out.
|
|
9726
|
-
if (cli?.hint) out.
|
|
9967
|
+
out.error(`Error${cli?.code ? ` ${cli.code}` : ""}: ${message}`);
|
|
9968
|
+
if (cli?.callId) out.error(`Call id: ${cli.callId}`);
|
|
9969
|
+
if (cli?.hint) out.error(cli.hint);
|
|
9727
9970
|
}
|
|
9728
9971
|
return exitCode;
|
|
9729
9972
|
}
|
|
@@ -9735,7 +9978,7 @@ async function main(argv, io = processIo()) {
|
|
|
9735
9978
|
flags = parsed.flags;
|
|
9736
9979
|
out = new Out(io, flags);
|
|
9737
9980
|
if (flags.version) {
|
|
9738
|
-
out.result("0.3.
|
|
9981
|
+
out.result("0.3.1");
|
|
9739
9982
|
return EXIT.ok;
|
|
9740
9983
|
}
|
|
9741
9984
|
const words = leadingWords(parsed.rest);
|
|
@@ -9745,12 +9988,14 @@ async function main(argv, io = processIo()) {
|
|
|
9745
9988
|
return EXIT.ok;
|
|
9746
9989
|
}
|
|
9747
9990
|
if (first === "login" || first === "logout") {
|
|
9748
|
-
if (parsed.rest.length > 1) throw userError(`Unknown argument for ${first}: ${parsed.rest[1]}`);
|
|
9991
|
+
if (parsed.rest.length > 1) throw userError(`Unknown argument for ${first}: ${parsed.rest[1]}.${versionHint(parsed.rest[1])}`);
|
|
9749
9992
|
return first === "login" ? await login(io, out, flags, parsed.noKey) : await logout(io, out, flags);
|
|
9750
9993
|
}
|
|
9751
9994
|
if (parsed.noKey) throw userError("--no-key is an option of rlx login.");
|
|
9752
9995
|
if (first === void 0 || first === "help") {
|
|
9753
9996
|
if (first === void 0 && parsed.rest.length > 0) throw userError(`Unknown option: ${parsed.rest[0]}`);
|
|
9997
|
+
const option = parsed.rest.find((arg) => arg.startsWith("-"));
|
|
9998
|
+
if (option !== void 0) throw userError(`Unknown option for help: ${option}.${versionHint(option)}`);
|
|
9754
9999
|
return await help(io, out, flags, first === "help" ? words.slice(1) : []);
|
|
9755
10000
|
}
|
|
9756
10001
|
return await runManifestCommand(io, out, flags, parsed.rest);
|