@platforma-sdk/pl-cli 0.13.6 → 0.14.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 +208 -0
- package/dist/address.d.ts +6 -0
- package/dist/base_command.d.ts +10 -3
- package/dist/cli.js +649 -230
- package/dist/cli.js.map +1 -1
- package/dist/cmd/login.d.ts +21 -0
- package/dist/cmd/logout.d.ts +7 -0
- package/dist/cmd/session/list.d.ts +6 -0
- package/dist/cmd/session/remove.d.ts +7 -0
- package/dist/cmd/session/select.d.ts +9 -0
- package/dist/cmd-opts.d.ts +8 -3
- package/dist/connection.d.ts +59 -3
- package/dist/index.js +1 -1
- package/dist/instance.d.ts +7 -0
- package/dist/jwt.d.ts +12 -0
- package/dist/lib.d.ts +2 -1
- package/dist/project_ops-CHhJu5k5.js +524 -0
- package/dist/project_ops-CHhJu5k5.js.map +1 -0
- package/dist/prompt.d.ts +5 -0
- package/dist/session_store.d.ts +112 -0
- package/dist/sso.d.ts +30 -0
- package/package.json +3 -3
- package/src/address.ts +15 -0
- package/src/base_command.ts +24 -6
- package/src/cli.ts +74 -3
- package/src/cmd/admin/delete-user.ts +7 -0
- package/src/cmd/login.ts +262 -0
- package/src/cmd/logout.ts +55 -0
- package/src/cmd/session/list.ts +71 -0
- package/src/cmd/session/remove.ts +29 -0
- package/src/cmd/session/select.ts +36 -0
- package/src/cmd-opts.ts +24 -15
- package/src/commands.test.ts +637 -0
- package/src/connection.ts +275 -15
- package/src/instance.ts +19 -0
- package/src/jwt.ts +36 -0
- package/src/lib.ts +12 -0
- package/src/prompt.ts +54 -0
- package/src/session_store.test.ts +237 -0
- package/src/session_store.ts +315 -0
- package/src/sso.test.ts +269 -0
- package/src/sso.ts +336 -0
- package/src/test_backend.ts +151 -0
- package/dist/output-D41Wn0wM.js +0 -229
- package/dist/output-D41Wn0wM.js.map +0 -1
package/dist/cli.js
CHANGED
|
@@ -1,50 +1,450 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Command as
|
|
3
|
-
import { asSignedResourceId as
|
|
4
|
-
import { randomUUID as
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
1
|
+
import { A as e, C as t, D as n, E as r, F as i, O as a, P as o, S as s, T as c, _ as l, a as u, b as d, c as f, d as p, f as m, g as h, h as g, i as ee, j as _, k as te, l as ne, m as v, n as re, o as ie, p as ae, r as y, s as oe, t as b, u as se, v as x, w as ce, x as S, y as C } from "./project_ops-CHhJu5k5.js";
|
|
2
|
+
import { Command as w, InvalidArgumentError as T, Option as E } from "commander";
|
|
3
|
+
import { UnauthenticatedPlClient as le, asSignedResourceId as ue, createSignedResourceId as de, field as D, isLocalResourceId as fe, isNotNullSignedResourceId as O, isPermissionDenied as pe, isUnauthenticated as me, plAddressToConfig as k, resourceIdFromString as he, resourceIdToString as A, resourceType as ge, resourceTypeToString as _e } from "@milaboratories/pl-client";
|
|
4
|
+
import { createHash as ve, randomBytes as ye, randomUUID as j } from "node:crypto";
|
|
5
|
+
import { createInterface as M } from "node:readline";
|
|
6
|
+
import { spawn as be } from "node:child_process";
|
|
7
|
+
import { createServer as xe } from "node:http";
|
|
8
|
+
import { InstanceStatsContextTypeName as Se, ProjectMetaKey as N, parseInstanceStats as Ce } from "@milaboratories/pl-middle-layer";
|
|
9
|
+
import { once as we } from "node:events";
|
|
10
|
+
// src/cmd-opts.ts
|
|
11
|
+
function P(e, ...t) {
|
|
12
|
+
for (let n of t) for (let t of n) e.addOption(t);
|
|
13
|
+
return e;
|
|
14
|
+
}
|
|
15
|
+
var F = (e = {}) => {
|
|
16
|
+
let t = e.required ? new E("-a, --address <url>", "Platforma server address").env("PL_ADDRESS") : new E("-a, --address <url>", "Platforma server address. Default: the address of the default stored session").env("PL_ADDRESS");
|
|
17
|
+
return [e.required ? t.makeOptionMandatory() : t];
|
|
18
|
+
}, I = (e = "text", t = ["text", "json"]) => [...F(), new E("-f, --format <format>", "Output format").choices(t).default(e)], L = () => [new E("-u, --user <user>", "Username for authentication. Without --password, selects the stored session of this login").env("PL_USER"), new E("-p, --password <password>", "Password for authentication").env("PL_PASSWORD")], R = () => [new E("--admin-user <user>", "Admin/controller username. Without --admin-password, selects the stored session of this login").env("PL_ADMIN_USER"), new E("--admin-password <password>", "Admin/controller password").env("PL_ADMIN_PASSWORD")], z = () => [
|
|
19
|
+
new E("--admin-user <user>", "Admin/controller username (enables admin mode)").env("PL_ADMIN_USER"),
|
|
20
|
+
new E("--admin-password <password>", "Admin/controller password").env("PL_ADMIN_PASSWORD"),
|
|
21
|
+
new E("--target-user <user>", "Operate on this user's data (requires admin credentials or an admin's stored session)").env("PL_TARGET_USER")
|
|
22
|
+
];
|
|
23
|
+
// src/prompt.ts
|
|
24
|
+
async function B(e, t = {}) {
|
|
25
|
+
let n = process.stdin, r = process.stderr;
|
|
26
|
+
if (!n.isTTY) throw Error(`Cannot ask for input: stdin is not a terminal. (${e.trim()})`);
|
|
27
|
+
if (!t.hidden) {
|
|
28
|
+
let t = M({
|
|
29
|
+
input: n,
|
|
30
|
+
output: r,
|
|
31
|
+
terminal: !0
|
|
32
|
+
});
|
|
33
|
+
try {
|
|
34
|
+
return await new Promise((n) => t.question(e, n));
|
|
35
|
+
} finally {
|
|
36
|
+
t.close();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
r.write(e), n.setRawMode(!0), n.resume(), n.setEncoding("utf8");
|
|
40
|
+
try {
|
|
41
|
+
return await new Promise((e, t) => {
|
|
42
|
+
let i = "", a = (o) => {
|
|
43
|
+
for (let s of o) {
|
|
44
|
+
if (s === "\r" || s === "\n" || s === "") {
|
|
45
|
+
n.removeListener("data", a), r.write("\n"), e(i);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (s === "") {
|
|
49
|
+
n.removeListener("data", a), r.write("\n"), t(/* @__PURE__ */ Error("Cancelled."));
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
s === "" || s === "\b" ? i = i.slice(0, -1) : i += s;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
n.on("data", a);
|
|
56
|
+
});
|
|
57
|
+
} finally {
|
|
58
|
+
n.setRawMode(!1), n.pause();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// src/sso.ts
|
|
62
|
+
async function Te(e) {
|
|
63
|
+
let { method: t, nonce: n } = e;
|
|
64
|
+
V(t.issuer, "SSO issuer");
|
|
65
|
+
let r = Oe(), i = ye(32).toString("base64url"), a = await ke(t.redirectPorts, i, e.signal);
|
|
66
|
+
try {
|
|
67
|
+
let o = `http://127.0.0.1:${a.port}/callback`, { authorizationEndpoint: s, tokenEndpoint: c } = await je(t.issuer, e.signal), l = new URL(s);
|
|
68
|
+
return l.searchParams.set("response_type", "code"), l.searchParams.set("client_id", t.clientId), l.searchParams.set("redirect_uri", o), l.searchParams.set("scope", t.scopes), l.searchParams.set("state", i), l.searchParams.set("nonce", n), l.searchParams.set("code_challenge", r.codeChallenge), l.searchParams.set("code_challenge_method", r.codeChallengeMethod), t.resource && l.searchParams.set("resource", t.resource), t.prompt && l.searchParams.set("prompt", t.prompt), t.accessType && l.searchParams.set("access_type", t.accessType), await e.openUrl(l.toString()), await Me({
|
|
69
|
+
tokenEndpoint: c,
|
|
70
|
+
code: await a.code,
|
|
71
|
+
redirectUri: o,
|
|
72
|
+
clientId: t.clientId,
|
|
73
|
+
codeVerifier: r.codeVerifier,
|
|
74
|
+
clientSecret: e.clientSecret,
|
|
75
|
+
resource: t.resource || void 0,
|
|
76
|
+
signal: e.signal
|
|
77
|
+
});
|
|
78
|
+
} finally {
|
|
79
|
+
a.close();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function V(e, t) {
|
|
83
|
+
let n;
|
|
84
|
+
try {
|
|
85
|
+
n = new URL(e);
|
|
86
|
+
} catch {
|
|
87
|
+
throw Error(`${t} is not a valid URL: ${H(e)}`);
|
|
88
|
+
}
|
|
89
|
+
if (n.protocol === "https:" || n.protocol === "http:" && Ee(n.hostname)) return n;
|
|
90
|
+
throw Error(`${t} must use https (http is accepted only for localhost): ${H(e)}`);
|
|
91
|
+
}
|
|
92
|
+
function Ee(e) {
|
|
93
|
+
return e === "localhost" || e === "[::1]" || e === "::1" || /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(e);
|
|
94
|
+
}
|
|
95
|
+
function H(e) {
|
|
96
|
+
return e.replace(/[\u0000-\u0008\u000b-\u001f\u007f-\u009f]/g, "");
|
|
97
|
+
}
|
|
98
|
+
async function De(e) {
|
|
99
|
+
V(e, "Sign-in URL");
|
|
100
|
+
let [t, n] = process.platform === "darwin" ? ["open", [e]] : process.platform === "win32" ? ["rundll32", ["url.dll,FileProtocolHandler", e]] : ["xdg-open", [e]];
|
|
101
|
+
return await new Promise((e) => {
|
|
102
|
+
try {
|
|
103
|
+
let r = be(t, n, {
|
|
104
|
+
stdio: "ignore",
|
|
105
|
+
detached: !0
|
|
106
|
+
});
|
|
107
|
+
r.once("error", () => e(!1)), r.once("spawn", () => {
|
|
108
|
+
r.unref(), e(!0);
|
|
109
|
+
});
|
|
110
|
+
} catch {
|
|
111
|
+
e(!1);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
function Oe() {
|
|
116
|
+
let e = ye(64).toString("base64url");
|
|
117
|
+
return {
|
|
118
|
+
codeVerifier: e,
|
|
119
|
+
codeChallenge: ve("sha256").update(e).digest().toString("base64url"),
|
|
120
|
+
codeChallengeMethod: "S256"
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function U(e) {
|
|
124
|
+
return `<!doctype html><meta charset=utf-8><title>Platforma</title><p>${e}</p><script>window.close()<\/script>`;
|
|
125
|
+
}
|
|
126
|
+
async function ke(e, t, n) {
|
|
127
|
+
if (n?.aborted) throw W();
|
|
128
|
+
let { promise: r, resolve: i, reject: a } = Promise.withResolvers();
|
|
129
|
+
r.catch(() => {});
|
|
130
|
+
let o = !1, s = () => {
|
|
131
|
+
o || (o = !0, c.close(), c.closeAllConnections(), a(W()));
|
|
132
|
+
}, c = xe((e, n) => {
|
|
133
|
+
let r = new URL(e.url ?? "/", "http://127.0.0.1");
|
|
134
|
+
if (r.pathname !== "/callback") {
|
|
135
|
+
n.statusCode = 404, n.end();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
n.setHeader("content-type", "text/html; charset=utf-8");
|
|
139
|
+
let o = r.searchParams;
|
|
140
|
+
if (o.get("state") !== t) {
|
|
141
|
+
n.statusCode = 400, n.end(U("This sign-in request is not valid. Go back to the terminal."));
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
let c = o.get("error"), l = o.get("code");
|
|
145
|
+
c ? (n.end(U("Sign-in failed. Go back to the terminal for the details.")), a(Error(H(`Identity provider error: ${c}: ${o.get("error_description") ?? ""}`)))) : l ? (n.end(U("Sign-in is complete. You can close this tab.")), i(l)) : (n.end(U("Sign-in failed. Go back to the terminal for the details.")), a(/* @__PURE__ */ Error("The SSO callback has no code."))), s();
|
|
146
|
+
});
|
|
147
|
+
n?.addEventListener("abort", s, { once: !0 });
|
|
148
|
+
let l = await Ae(c, e);
|
|
149
|
+
return c.on("error", (e) => a(/* @__PURE__ */ Error(`SSO loopback server error: ${e.message}`))), {
|
|
150
|
+
port: l,
|
|
151
|
+
code: r,
|
|
152
|
+
close: s
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function Ae(e, t) {
|
|
156
|
+
let { promise: n, resolve: r, reject: i } = Promise.withResolvers();
|
|
157
|
+
if (t.length === 0) return i(/* @__PURE__ */ Error("The Backend advertises no SSO redirect ports.")), n;
|
|
158
|
+
let a = -1, o = () => {
|
|
159
|
+
c(), r(t[a]);
|
|
160
|
+
}, s = (e) => {
|
|
161
|
+
e.code === "EADDRINUSE" ? l() : (c(), i(/* @__PURE__ */ Error(`SSO loopback cannot bind port ${t[a]}: ${e.message}`)));
|
|
162
|
+
}, c = () => {
|
|
163
|
+
e.removeListener("error", s), e.removeListener("listening", o);
|
|
164
|
+
}, l = () => {
|
|
165
|
+
a += 1, a >= t.length ? (c(), i(/* @__PURE__ */ Error(`All SSO redirect ports are in use: ${t.join(", ")}`))) : e.listen(t[a], "127.0.0.1");
|
|
166
|
+
};
|
|
167
|
+
return e.on("listening", o), e.on("error", s), l(), n;
|
|
168
|
+
}
|
|
169
|
+
function W() {
|
|
170
|
+
let e = /* @__PURE__ */ Error("SSO sign-in was cancelled.");
|
|
171
|
+
return e.name = "AbortError", e;
|
|
172
|
+
}
|
|
173
|
+
async function je(e, t) {
|
|
174
|
+
let n = `${e.replace(/\/+$/, "")}/.well-known/openid-configuration`, r = await fetch(n, { signal: t });
|
|
175
|
+
if (!r.ok) throw Error(`OIDC discovery ${n} failed: ${r.status}`);
|
|
176
|
+
let i = await r.json();
|
|
177
|
+
if (typeof i != "object" || !i || !("authorization_endpoint" in i) || !("token_endpoint" in i) || typeof i.authorization_endpoint != "string" || typeof i.token_endpoint != "string") throw Error(`OIDC discovery ${n} has no authorization_endpoint or token_endpoint.`);
|
|
178
|
+
return V(i.authorization_endpoint, "SSO authorization endpoint"), V(i.token_endpoint, "SSO token endpoint"), {
|
|
179
|
+
authorizationEndpoint: i.authorization_endpoint,
|
|
180
|
+
tokenEndpoint: i.token_endpoint
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
async function Me(e) {
|
|
184
|
+
let t = {
|
|
185
|
+
grant_type: "authorization_code",
|
|
186
|
+
code: e.code,
|
|
187
|
+
redirect_uri: e.redirectUri,
|
|
188
|
+
client_id: e.clientId,
|
|
189
|
+
code_verifier: e.codeVerifier
|
|
190
|
+
};
|
|
191
|
+
e.resource && (t.resource = e.resource), e.clientSecret && (t.client_secret = e.clientSecret);
|
|
192
|
+
let n = await fetch(e.tokenEndpoint, {
|
|
193
|
+
method: "POST",
|
|
194
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
195
|
+
body: new URLSearchParams(t).toString(),
|
|
196
|
+
signal: e.signal
|
|
197
|
+
}), r = new Uint8Array(await n.arrayBuffer());
|
|
198
|
+
if (!n.ok) throw Error(`Identity provider token endpoint returned ${n.status}: ` + H(Buffer.from(r).toString("utf8")));
|
|
199
|
+
return r;
|
|
200
|
+
}
|
|
201
|
+
// src/cmd/login.ts
|
|
202
|
+
function Ne() {
|
|
203
|
+
let e = new w("login").description(`Sign in to a Backend and store the session. Later commands to the same Backend instance use the stored session. Select the login method with --method <id>; see the IDs with --list-methods. Needs Backend v${d} or newer.`);
|
|
204
|
+
return P(e, F({ required: !0 })), e.addOption(new E("--method <id>", "ID of the login method (SSO provider, password or token method). Required to sign in")), e.addOption(new E("--list-methods", "Show the login methods of the Backend and exit; do not sign in")), e.addOption(new E("-f, --format <format>", "Output format of --list-methods").choices(["text", "json"]).default("text")), e.addOption(new E("-u, --user <user>", "Login for a password method. Asked for when not given").env("PL_USER")), e.addOption(new E("-p, --password <password>", "Password, or the token for a token method. Asked for when not given").env("PL_PASSWORD")), e.addOption(new E("--no-browser", "Do not open a browser for SSO; only print the sign-in URL")), e.addOption(new E("--timeout <seconds>", "How long to wait for the SSO sign-in to complete").default(300).argParser((e) => {
|
|
205
|
+
let t = Number(e);
|
|
206
|
+
if (!Number.isFinite(t) || t <= 0) throw Error("--timeout must be a positive number");
|
|
207
|
+
return t;
|
|
208
|
+
})), e.action(async (e) => {
|
|
209
|
+
e.listMethods ? await Pe(e) : await Fe(e);
|
|
210
|
+
}), e;
|
|
211
|
+
}
|
|
212
|
+
function G(e) {
|
|
213
|
+
return e.length === 0 ? "The Backend advertises no login methods." : h([
|
|
214
|
+
"ID",
|
|
215
|
+
"KIND",
|
|
216
|
+
"TITLE",
|
|
217
|
+
"ISSUER"
|
|
218
|
+
], e.map((e) => [
|
|
219
|
+
H(e.id),
|
|
220
|
+
e.kind,
|
|
221
|
+
H(e.title),
|
|
222
|
+
e.kind === "sso" ? H(e.issuer) : ""
|
|
223
|
+
]));
|
|
224
|
+
}
|
|
225
|
+
async function Pe(e) {
|
|
226
|
+
let t = await le.build(k(e.address));
|
|
227
|
+
try {
|
|
228
|
+
let n = t.loginMethods();
|
|
229
|
+
e.format === "json" ? x(n.map((e) => e.kind === "sso" ? {
|
|
230
|
+
id: e.id,
|
|
231
|
+
kind: e.kind,
|
|
232
|
+
title: e.title,
|
|
233
|
+
issuer: e.issuer
|
|
234
|
+
} : {
|
|
235
|
+
id: e.id,
|
|
236
|
+
kind: e.kind,
|
|
237
|
+
title: e.title
|
|
238
|
+
})) : C(G(n));
|
|
239
|
+
} finally {
|
|
240
|
+
await t.ll.close();
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
async function Fe(e, t = new _()) {
|
|
244
|
+
let a = k(e.address), s = i(e.address), { unauth: c, instanceId: l } = await r(a);
|
|
245
|
+
try {
|
|
246
|
+
if (!await c.requireAuth()) {
|
|
247
|
+
C(`The Backend at ${s} needs no authentication. No session was stored.`);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (!c.hasCapability("auth:v2")) throw Error(S);
|
|
251
|
+
let r = n(l), i = Ie(c, e, s), u = i.kind, d = {
|
|
252
|
+
user: e.user ?? a.user,
|
|
253
|
+
password: e.password ?? a.password
|
|
254
|
+
}, f = (i.kind === "sso" ? await Re(c, i, e) : await Le(c, i, d)).jwtToken;
|
|
255
|
+
if (f === void 0) throw Error("The Backend returned no token.");
|
|
256
|
+
let p = o(f);
|
|
257
|
+
if (p.issuer === void 0) throw Error(S);
|
|
258
|
+
if (p.issuer !== r) throw Error(`The token is for instance ${p.issuer}, but the ping answer names instance ${r}. No session was stored. Make sure that one Backend answers at ${s} and try again.`);
|
|
259
|
+
let m = (/* @__PURE__ */ new Date()).toISOString();
|
|
260
|
+
t.put(r, {
|
|
261
|
+
login: p.login,
|
|
262
|
+
address: s,
|
|
263
|
+
token: f,
|
|
264
|
+
loginMethod: {
|
|
265
|
+
id: i.id,
|
|
266
|
+
kind: u
|
|
267
|
+
},
|
|
268
|
+
createdAt: m,
|
|
269
|
+
updatedAt: m
|
|
270
|
+
});
|
|
271
|
+
let h = p.expiresAt === void 0 ? "" : ` until ${new Date(p.expiresAt).toISOString()}`;
|
|
272
|
+
C(`Signed in to ${s} (instance ${r}) as "${p.login}". The session is stored${h} in ${t.path}.`);
|
|
273
|
+
} finally {
|
|
274
|
+
await c.ll.close();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
function Ie(e, t, n) {
|
|
278
|
+
let r = e.loginMethods();
|
|
279
|
+
if (r.length === 0) throw Error("The Backend advertises no login method that pl-cli can use. Use --user/--password per command.");
|
|
280
|
+
if (t.method === void 0) throw Error(`Select a login method with --method <id>. Login methods of the Backend:\n\n${G(r)}\n\nRun: pl-cli login -a ${n} --method <id>`);
|
|
281
|
+
let i = r.find((e) => e.id === t.method);
|
|
282
|
+
if (i === void 0) throw Error(`The Backend has no login method "${t.method}". Login methods of the Backend:\n\n` + G(r));
|
|
283
|
+
return i;
|
|
284
|
+
}
|
|
285
|
+
async function Le(e, t, n) {
|
|
286
|
+
let r = t.kind === "token", i = r ? n.user ?? "" : n.user ?? await B("Login: "), a = n.password ?? await B(r ? "Access token: " : "Password: ", { hidden: !0 });
|
|
287
|
+
return await e.login(i, a, t.id);
|
|
288
|
+
}
|
|
289
|
+
async function Re(e, t, n) {
|
|
290
|
+
let r = AbortSignal.timeout((n.timeout ?? 300) * 1e3), i = await e.beginSSOLogin(t.id), a = await Te({
|
|
291
|
+
method: t,
|
|
292
|
+
nonce: i.nonce,
|
|
293
|
+
clientSecret: i.clientSecret,
|
|
294
|
+
signal: r,
|
|
295
|
+
openUrl: async (e) => {
|
|
296
|
+
let r = new URL(e).searchParams.get("redirect_uri") ?? "";
|
|
297
|
+
process.stderr.write(`Sign in with ${H(t.title)} in the browser. If no browser opens, open this URL:\n\n ${e}\n\nThe browser must reach ${r} on this computer (when you work over SSH, forward that port).\nWaiting for the sign-in (up to ${n.timeout ?? 300} s)...\n`), n.browser !== !1 && !await De(e) && process.stderr.write("Could not open a browser; open the URL above.\n");
|
|
298
|
+
}
|
|
299
|
+
}).catch((e) => {
|
|
300
|
+
throw r.aborted ? Error(`SSO sign-in did not complete in ${n.timeout ?? 300} s.`, { cause: e }) : e;
|
|
301
|
+
});
|
|
302
|
+
return await e.loginSSO({
|
|
303
|
+
tokenResponse: a,
|
|
304
|
+
idP: t.id
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
// src/instance.ts
|
|
308
|
+
async function K(e) {
|
|
309
|
+
let { unauth: t, instanceId: i } = await r(k(e));
|
|
310
|
+
return await t.ll.close(), n(i);
|
|
311
|
+
}
|
|
312
|
+
async function q(e) {
|
|
313
|
+
if (e.instance !== void 0) return e.instance;
|
|
314
|
+
if (e.address !== void 0) return await K(e.address);
|
|
315
|
+
throw Error("Give --instance <id>, or --address (or PL_ADDRESS) of a running Backend.");
|
|
316
|
+
}
|
|
317
|
+
// src/cmd/logout.ts
|
|
318
|
+
function ze() {
|
|
319
|
+
let e = new w("logout").description("Remove a stored session. With --address: the current session of that Backend (found by its instance ID), or the session of --login there. Without --address: the default session, or with --login, the session of that login on the default session's instance. The Backend keeps the session until its token expires.");
|
|
320
|
+
return P(e, F()), e.addOption(new E("--login <login>", "Remove the session of this login (without --address: on the default session's instance)")), e.action(async (e) => {
|
|
321
|
+
await Be(e);
|
|
322
|
+
}), e;
|
|
323
|
+
}
|
|
324
|
+
async function Be(e, t = new _()) {
|
|
325
|
+
let n, r;
|
|
326
|
+
if (e.address !== void 0) n = await K(e.address), r = `the Backend at ${i(e.address)} (instance ${n})`;
|
|
327
|
+
else {
|
|
328
|
+
let e = t.getDefault();
|
|
329
|
+
if (e === void 0) throw Error("No default session. Give --address (or PL_ADDRESS) of the Backend.");
|
|
330
|
+
n = e.instanceId, r = `instance ${n}`;
|
|
331
|
+
}
|
|
332
|
+
let a = e.login ?? (e.address === void 0 ? t.getDefault()?.session.login : void 0), o = t.remove(n, a);
|
|
333
|
+
if (o === void 0) {
|
|
334
|
+
let e = a === void 0 ? "No current session" : `No session of "${a}"`;
|
|
335
|
+
C(`${e} is stored for ${r}.`);
|
|
336
|
+
} else C(`Removed the session of "${o.login}" on instance ${n}.`);
|
|
337
|
+
}
|
|
338
|
+
// src/cmd/session/list.ts
|
|
339
|
+
function Ve() {
|
|
340
|
+
let e = new w("list").description("List stored sessions: instance ID, login, address, login method and expiry. Never prints tokens. DEFAULT: the session that commands without --address use. CURRENT: the session that commands use when --address selects its instance.");
|
|
341
|
+
return e.addOption(new E("-f, --format <format>", "Output format").choices(["text", "json"]).default("text")), e.action((e) => {
|
|
342
|
+
He(e);
|
|
343
|
+
}), e;
|
|
344
|
+
}
|
|
345
|
+
function He(e, t = new _()) {
|
|
346
|
+
let n = Date.now(), r = t.list().map(({ instanceId: e, current: t, default: r, session: a }) => {
|
|
347
|
+
let s;
|
|
348
|
+
try {
|
|
349
|
+
s = o(a.token).expiresAt;
|
|
350
|
+
} catch {
|
|
351
|
+
s = void 0;
|
|
352
|
+
}
|
|
353
|
+
return {
|
|
354
|
+
instanceId: e,
|
|
355
|
+
default: r,
|
|
356
|
+
current: t,
|
|
357
|
+
login: a.login,
|
|
358
|
+
address: i(a.address),
|
|
359
|
+
loginMethod: a.loginMethod,
|
|
360
|
+
expiresAt: s === void 0 ? void 0 : new Date(s).toISOString(),
|
|
361
|
+
expired: s !== void 0 && s <= n
|
|
362
|
+
};
|
|
363
|
+
});
|
|
364
|
+
e.format === "json" ? x(r) : r.length === 0 ? C(`No stored sessions in ${t.path}.`) : C(h([
|
|
365
|
+
"DEFAULT",
|
|
366
|
+
"CURRENT",
|
|
367
|
+
"INSTANCE",
|
|
368
|
+
"LOGIN",
|
|
369
|
+
"ADDRESS",
|
|
370
|
+
"METHOD",
|
|
371
|
+
"EXPIRES"
|
|
372
|
+
], r.map((e) => [
|
|
373
|
+
e.default ? "*" : "",
|
|
374
|
+
e.current ? "*" : "",
|
|
375
|
+
e.instanceId,
|
|
376
|
+
e.login,
|
|
377
|
+
e.address,
|
|
378
|
+
e.loginMethod === void 0 ? "" : `${e.loginMethod.id} (${e.loginMethod.kind})`,
|
|
379
|
+
e.expiresAt === void 0 ? "(unknown)" : g(new Date(e.expiresAt)) + (e.expired ? " (expired)" : "")
|
|
380
|
+
])));
|
|
381
|
+
}
|
|
382
|
+
// src/cmd/session/select.ts
|
|
383
|
+
var J = () => [new E("--instance <id>", "Instance ID of the Backend (see 'pl-cli session list')"), new E("-a, --address <url>", "Address of the Backend; its ping answer gives the instance ID").env("PL_ADDRESS")];
|
|
384
|
+
function Ue() {
|
|
385
|
+
let e = new w("select").description("Make the stored session of <login> the current session of its Backend instance. Commands without credentials use the current session.");
|
|
386
|
+
e.argument("<login>", "Login of a stored session");
|
|
387
|
+
for (let t of J()) e.addOption(t);
|
|
388
|
+
return e.action(async (e, t) => {
|
|
389
|
+
await We(e, t);
|
|
390
|
+
}), e;
|
|
391
|
+
}
|
|
392
|
+
async function We(e, t, n = new _()) {
|
|
393
|
+
let r = await q(t);
|
|
394
|
+
n.select(r, e), C(`The session of "${e}" is now current on instance ${r}.`);
|
|
395
|
+
}
|
|
396
|
+
// src/cmd/session/remove.ts
|
|
397
|
+
function Ge() {
|
|
398
|
+
let e = new w("remove").description("Remove the stored session of <login>. With --instance, works when the Backend is not reachable.");
|
|
399
|
+
e.argument("<login>", "Login of a stored session");
|
|
400
|
+
for (let t of J()) e.addOption(t);
|
|
401
|
+
return e.action(async (e, t) => {
|
|
402
|
+
await Ke(e, t);
|
|
403
|
+
}), e;
|
|
404
|
+
}
|
|
405
|
+
async function Ke(e, t, n = new _()) {
|
|
406
|
+
let r = await q(t);
|
|
407
|
+
if (n.remove(r, e) === void 0) throw Error(`No stored session for login "${e}" on instance ${r}.`);
|
|
408
|
+
C(`Removed the session of "${e}" on instance ${r}.`);
|
|
409
|
+
}
|
|
8
410
|
// src/base_command.ts
|
|
9
|
-
async function
|
|
10
|
-
|
|
411
|
+
async function Y(e) {
|
|
412
|
+
if (e.adminPassword && !e.adminUser) throw Error("--admin-password needs --admin-user (or PL_ADMIN_USER).");
|
|
413
|
+
if (e.adminUser && e.adminPassword) {
|
|
414
|
+
if (e.address === void 0) throw Error("Give the Backend address (--address or PL_ADDRESS) with admin credentials.");
|
|
415
|
+
return ce({
|
|
416
|
+
address: e.address,
|
|
417
|
+
adminUser: e.adminUser,
|
|
418
|
+
adminPassword: e.adminPassword
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
return e.adminUser ? c({
|
|
11
422
|
address: e.address,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}) : y({
|
|
423
|
+
user: e.adminUser
|
|
424
|
+
}) : c({
|
|
15
425
|
address: e.address,
|
|
16
426
|
user: e.user,
|
|
17
427
|
password: e.password
|
|
18
428
|
});
|
|
19
429
|
}
|
|
20
|
-
async function
|
|
430
|
+
async function X(e) {
|
|
21
431
|
let t = !!e.adminUser, n = !!e.adminPassword, r = !!e.targetUser;
|
|
22
|
-
if (r && !(t && n)) throw Error("--target-user requires --admin-user and --admin-password");
|
|
432
|
+
if (r && !(t && n) && e.password !== void 0) throw Error("--target-user requires --admin-user and --admin-password, or a stored session of an admin user");
|
|
23
433
|
if ((t || n) && !r) throw Error("--admin-user/--admin-password require --target-user for project commands");
|
|
24
|
-
let i = await
|
|
25
|
-
return
|
|
434
|
+
let i = await Y(e), a;
|
|
435
|
+
return a = r ? (await p(i, e.targetUser)).projectListRid : await oe(i), {
|
|
26
436
|
pl: i,
|
|
27
|
-
projectListRid:
|
|
437
|
+
projectListRid: a
|
|
28
438
|
};
|
|
29
439
|
}
|
|
30
|
-
// src/cmd-opts.ts
|
|
31
|
-
function L(e, ...t) {
|
|
32
|
-
for (let n of t) for (let t of n) e.addOption(t);
|
|
33
|
-
return e;
|
|
34
|
-
}
|
|
35
|
-
var R = () => [new C("-a, --address <url>", "Platforma server address").env("PL_ADDRESS").makeOptionMandatory()], z = (e = "text", t = ["text", "json"]) => [...R(), new C("-f, --format <format>", "Output format").choices(t).default(e)], B = () => [new C("-u, --user <user>", "Username for authentication").env("PL_USER"), new C("-p, --password <password>", "Password for authentication").env("PL_PASSWORD")], V = () => [new C("--admin-user <user>", "Admin/controller username").env("PL_ADMIN_USER").makeOptionMandatory(), new C("--admin-password <password>", "Admin/controller password").env("PL_ADMIN_PASSWORD").makeOptionMandatory()], H = () => [
|
|
36
|
-
new C("--admin-user <user>", "Admin/controller username (enables admin mode)").env("PL_ADMIN_USER"),
|
|
37
|
-
new C("--admin-password <password>", "Admin/controller password").env("PL_ADMIN_PASSWORD"),
|
|
38
|
-
new C("--target-user <user>", "Operate on this user's data (requires admin credentials)").env("PL_TARGET_USER")
|
|
39
|
-
];
|
|
40
440
|
// src/cmd/project/list.ts
|
|
41
|
-
function
|
|
42
|
-
let e = new
|
|
43
|
-
return
|
|
44
|
-
let { pl:
|
|
441
|
+
function qe() {
|
|
442
|
+
let e = new w("list").description("List all projects for the authenticated user.");
|
|
443
|
+
return P(e, I(), L(), z()), e.action(async (e) => {
|
|
444
|
+
let { pl: t, projectListRid: n } = await X(e);
|
|
45
445
|
try {
|
|
46
|
-
let
|
|
47
|
-
if (e.format === "json")
|
|
446
|
+
let r = await ne(t, n);
|
|
447
|
+
if (e.format === "json") x(r.map((e) => ({
|
|
48
448
|
id: e.id,
|
|
49
449
|
rid: A(e.rid),
|
|
50
450
|
label: e.label,
|
|
@@ -52,232 +452,233 @@ function U() {
|
|
|
52
452
|
lastModified: e.lastModified.toISOString()
|
|
53
453
|
})));
|
|
54
454
|
else {
|
|
55
|
-
if (
|
|
56
|
-
|
|
455
|
+
if (r.length === 0) {
|
|
456
|
+
C("No projects found.");
|
|
57
457
|
return;
|
|
58
458
|
}
|
|
59
|
-
|
|
459
|
+
C(h([
|
|
60
460
|
"ID",
|
|
61
461
|
"RID",
|
|
62
462
|
"LABEL",
|
|
63
463
|
"CREATED",
|
|
64
464
|
"LAST MODIFIED"
|
|
65
|
-
],
|
|
465
|
+
], r.map((e) => [
|
|
66
466
|
e.id,
|
|
67
467
|
A(e.rid),
|
|
68
468
|
e.label,
|
|
69
469
|
g(e.created),
|
|
70
470
|
g(e.lastModified)
|
|
71
|
-
]))),
|
|
471
|
+
]))), C(`\n${r.length} project(s)`);
|
|
72
472
|
}
|
|
73
473
|
} finally {
|
|
74
|
-
await
|
|
474
|
+
await t.close();
|
|
75
475
|
}
|
|
76
476
|
}), e;
|
|
77
477
|
}
|
|
78
478
|
// src/cmd/project/info.ts
|
|
79
|
-
function
|
|
80
|
-
let e = new
|
|
81
|
-
return e.argument("<project>", "Project ID or label"),
|
|
82
|
-
let { pl:
|
|
479
|
+
function Je() {
|
|
480
|
+
let e = new w("info").description("Show detailed information about a project.");
|
|
481
|
+
return e.argument("<project>", "Project ID or label"), P(e, I(), L(), z()), e.action(async (e, t) => {
|
|
482
|
+
let { pl: n, projectListRid: r } = await X(t);
|
|
83
483
|
try {
|
|
84
|
-
let { id:
|
|
85
|
-
if (
|
|
86
|
-
id:
|
|
87
|
-
rid: A(
|
|
88
|
-
label:
|
|
89
|
-
schemaVersion:
|
|
90
|
-
blockCount:
|
|
91
|
-
blockIds:
|
|
92
|
-
created:
|
|
93
|
-
lastModified:
|
|
484
|
+
let { id: i, rid: a } = await v(n, r, e), o = await ie(n, i, a);
|
|
485
|
+
if (t.format === "json") x({
|
|
486
|
+
id: o.id,
|
|
487
|
+
rid: A(o.rid),
|
|
488
|
+
label: o.label,
|
|
489
|
+
schemaVersion: o.schemaVersion,
|
|
490
|
+
blockCount: o.blockCount,
|
|
491
|
+
blockIds: o.blockIds,
|
|
492
|
+
created: o.created.toISOString(),
|
|
493
|
+
lastModified: o.lastModified.toISOString()
|
|
94
494
|
});
|
|
95
495
|
else {
|
|
96
|
-
if (
|
|
97
|
-
|
|
496
|
+
if (C(`Project: ${o.label}`), C(`ID: ${o.id}`), C(`RID: ${A(o.rid)}`), C(`Schema: ${o.schemaVersion ?? "(unknown)"}`), C(`Blocks: ${o.blockCount}`), o.blockIds.length > 0) for (let e of o.blockIds) C(` - ${e}`);
|
|
497
|
+
C(`Created: ${g(o.created)}`), C(`Last Modified: ${g(o.lastModified)}`);
|
|
98
498
|
}
|
|
99
499
|
} finally {
|
|
100
|
-
await
|
|
500
|
+
await n.close();
|
|
101
501
|
}
|
|
102
502
|
}), e;
|
|
103
503
|
}
|
|
104
504
|
// src/cmd/project/duplicate.ts
|
|
105
|
-
function
|
|
106
|
-
let e = new
|
|
107
|
-
return e.argument("<project>", "Project ID or label"),
|
|
108
|
-
let { pl:
|
|
505
|
+
function Ye() {
|
|
506
|
+
let e = new w("duplicate").description("Duplicate a project within the same user. Auto-renames on collision by default.");
|
|
507
|
+
return e.argument("<project>", "Project ID or label"), P(e, I(), L(), z()), e.option("-n, --name <name>", "Name for the duplicate"), e.addOption(new E("--auto-rename", "Auto-rename on collision (default: true)").default(!0)), e.option("--no-auto-rename", "Fail on name collision instead of auto-renaming"), e.action(async (e, t) => {
|
|
508
|
+
let { pl: n, projectListRid: r } = await X(t);
|
|
109
509
|
try {
|
|
110
|
-
let { rid:
|
|
111
|
-
let
|
|
112
|
-
if (
|
|
113
|
-
if (!
|
|
114
|
-
|
|
115
|
-
} else
|
|
116
|
-
let
|
|
117
|
-
e.createField(
|
|
118
|
-
let d = await
|
|
510
|
+
let { rid: i } = await v(n, r, e), a = j(), o = await n.withWriteTx("duplicateProject", async (e) => {
|
|
511
|
+
let n = await e.getKValueString(i, N), o = JSON.parse(n).label, s = await u(e, r), c;
|
|
512
|
+
if (t.name) {
|
|
513
|
+
if (!t.autoRename && s.includes(t.name)) throw Error(`Project name "${t.name}" already exists.`);
|
|
514
|
+
c = s.includes(t.name) ? b(t.name, s) : t.name;
|
|
515
|
+
} else c = b(o, s);
|
|
516
|
+
let l = await y(e, i, { label: c });
|
|
517
|
+
e.createField(D(r, a), "Dynamic", l), await e.commit();
|
|
518
|
+
let d = await l.globalId;
|
|
119
519
|
return {
|
|
120
520
|
id: A(d),
|
|
121
521
|
rid: d,
|
|
122
|
-
label:
|
|
522
|
+
label: c
|
|
123
523
|
};
|
|
124
524
|
});
|
|
125
|
-
|
|
126
|
-
id:
|
|
127
|
-
rid: A(
|
|
128
|
-
label:
|
|
129
|
-
}) :
|
|
525
|
+
t.format === "json" ? x({
|
|
526
|
+
id: o.id,
|
|
527
|
+
rid: A(o.rid),
|
|
528
|
+
label: o.label
|
|
529
|
+
}) : C(`Duplicated project as "${o.label}" (id: ${o.id})`);
|
|
130
530
|
} finally {
|
|
131
|
-
await
|
|
531
|
+
await n.close();
|
|
132
532
|
}
|
|
133
533
|
}), e;
|
|
134
534
|
}
|
|
135
535
|
// src/cmd/project/rename.ts
|
|
136
|
-
function
|
|
137
|
-
let e = new
|
|
138
|
-
return e.argument("<project>", "Project ID or label"),
|
|
139
|
-
let { pl:
|
|
536
|
+
function Xe() {
|
|
537
|
+
let e = new w("rename").description("Rename a project.");
|
|
538
|
+
return e.argument("<project>", "Project ID or label"), P(e, I(), L(), z()), e.addOption(new E("-n, --name <name>", "New name for the project").makeOptionMandatory()), e.action(async (e, t) => {
|
|
539
|
+
let { pl: n, projectListRid: r } = await X(t);
|
|
140
540
|
try {
|
|
141
|
-
let { id:
|
|
142
|
-
await
|
|
143
|
-
id:
|
|
144
|
-
rid: A(
|
|
145
|
-
label:
|
|
146
|
-
}) :
|
|
541
|
+
let { id: i, rid: a } = await v(n, r, e);
|
|
542
|
+
await ae(n, a, t.name), t.format === "json" ? x({
|
|
543
|
+
id: i,
|
|
544
|
+
rid: A(a),
|
|
545
|
+
label: t.name
|
|
546
|
+
}) : C(`Renamed project to "${t.name}"`);
|
|
147
547
|
} finally {
|
|
148
|
-
await
|
|
548
|
+
await n.close();
|
|
149
549
|
}
|
|
150
550
|
}), e;
|
|
151
551
|
}
|
|
152
552
|
// src/cmd/project/delete.ts
|
|
153
|
-
function
|
|
154
|
-
let e = new
|
|
155
|
-
return e.argument("<project>", "Project ID or label"),
|
|
156
|
-
let { pl:
|
|
553
|
+
function Ze() {
|
|
554
|
+
let e = new w("delete").description("Delete a project. This permanently destroys all project data.");
|
|
555
|
+
return e.argument("<project>", "Project ID or label"), P(e, I(), L(), z()), e.option("--force", "Skip confirmation", !1), e.action(async (e, t) => {
|
|
556
|
+
let { pl: n, projectListRid: r } = await X(t);
|
|
157
557
|
try {
|
|
158
|
-
let { id:
|
|
159
|
-
if (!
|
|
160
|
-
let e =
|
|
558
|
+
let { id: i, rid: a, fieldName: o } = await v(n, r, e), s = await ie(n, i, a);
|
|
559
|
+
if (!t.force) {
|
|
560
|
+
let e = M({
|
|
161
561
|
input: process.stdin,
|
|
162
562
|
output: process.stderr
|
|
163
563
|
});
|
|
164
564
|
try {
|
|
165
565
|
if ((await new Promise((t) => {
|
|
166
|
-
e.question(`Delete project "${
|
|
566
|
+
e.question(`Delete project "${s.label}" (${s.blockCount} blocks)? [y/N] `, t);
|
|
167
567
|
})).toLowerCase() !== "y") {
|
|
168
|
-
|
|
568
|
+
C("Aborted.");
|
|
169
569
|
return;
|
|
170
570
|
}
|
|
171
571
|
} finally {
|
|
172
572
|
e.close();
|
|
173
573
|
}
|
|
174
574
|
}
|
|
175
|
-
await
|
|
575
|
+
await re(n, r, o), t.format === "json" ? x({
|
|
176
576
|
deleted: !0,
|
|
177
|
-
id:
|
|
178
|
-
label:
|
|
179
|
-
}) :
|
|
577
|
+
id: i,
|
|
578
|
+
label: s.label
|
|
579
|
+
}) : C(`Deleted project "${s.label}"`);
|
|
180
580
|
} finally {
|
|
181
|
-
await
|
|
581
|
+
await n.close();
|
|
182
582
|
}
|
|
183
583
|
}), e;
|
|
184
584
|
}
|
|
185
585
|
// src/cmd/admin/copy-project.ts
|
|
186
|
-
function
|
|
187
|
-
let e = new
|
|
188
|
-
return
|
|
189
|
-
let
|
|
586
|
+
function Qe() {
|
|
587
|
+
let e = new w("copy-project").description("Copy a project from one user to another. Requires admin/controller credentials.");
|
|
588
|
+
return P(e, I(), R()), e.addOption(new E("--source-user <user>", "Username of the source project owner").makeOptionMandatory()), e.addOption(new E("--source-project <project>", "Source project ID or label").makeOptionMandatory()), e.option("--target-user <user>", "Username of the target user (defaults to source-user for same-user copy)"), e.option("-n, --name <name>", "Name for the copied project"), e.addOption(new E("--auto-rename", "Auto-rename on collision (default: true)").default(!0)), e.option("--no-auto-rename", "Fail on name collision instead of auto-renaming"), e.action(async (e) => {
|
|
589
|
+
let t = await Y(e);
|
|
190
590
|
try {
|
|
191
|
-
let
|
|
192
|
-
let n = await t.getKValueString(
|
|
193
|
-
if (
|
|
194
|
-
if (!e.autoRename) throw Error(`Project name "${
|
|
195
|
-
|
|
591
|
+
let n = e.targetUser ?? e.sourceUser, r = await p(t, e.sourceUser), { rid: i } = await v(t, r.projectListRid, e.sourceProject), a = n === e.sourceUser ? r : await p(t, n), o = j(), s = await t.withWriteTx("adminCopyProject", async (t) => {
|
|
592
|
+
let n = await t.getKValueString(i, N), r = JSON.parse(n).label, s = await u(t, a.projectListRid), c = e.name ?? r;
|
|
593
|
+
if (s.includes(c)) {
|
|
594
|
+
if (!e.autoRename) throw Error(`Project name "${c}" already exists for target user.`);
|
|
595
|
+
c = b(e.name ?? r, s);
|
|
196
596
|
}
|
|
197
|
-
let
|
|
198
|
-
t.createField(
|
|
199
|
-
let d = await
|
|
597
|
+
let l = await y(t, i, { label: c });
|
|
598
|
+
t.createField(D(a.projectListRid, o), "Dynamic", l), await t.commit();
|
|
599
|
+
let d = await l.globalId;
|
|
200
600
|
return {
|
|
201
601
|
id: A(d),
|
|
202
602
|
rid: d,
|
|
203
|
-
label:
|
|
603
|
+
label: c
|
|
204
604
|
};
|
|
205
605
|
});
|
|
206
|
-
e.format === "json" ?
|
|
207
|
-
id:
|
|
208
|
-
rid: A(
|
|
209
|
-
label:
|
|
606
|
+
e.format === "json" ? x({
|
|
607
|
+
id: s.id,
|
|
608
|
+
rid: A(s.rid),
|
|
609
|
+
label: s.label,
|
|
210
610
|
sourceUser: e.sourceUser,
|
|
211
|
-
targetUser:
|
|
212
|
-
}) :
|
|
611
|
+
targetUser: n
|
|
612
|
+
}) : C(`Copied project from ${e.sourceUser} to ${n} as "${s.label}" (id: ${s.id}, rid: ${A(s.rid)})`);
|
|
213
613
|
} finally {
|
|
214
|
-
await
|
|
614
|
+
await t.close();
|
|
215
615
|
}
|
|
216
616
|
}), e;
|
|
217
617
|
}
|
|
218
618
|
// src/cmd/admin/user-list.ts
|
|
219
|
-
function
|
|
220
|
-
let e = new
|
|
221
|
-
return
|
|
619
|
+
function $e() {
|
|
620
|
+
let e = new w("user-list").description("List all known users' logins from a live server. Requires admin/controller credentials.");
|
|
621
|
+
return P(e, I("csv", [
|
|
222
622
|
"text",
|
|
223
623
|
"json",
|
|
224
624
|
"csv"
|
|
225
|
-
]),
|
|
226
|
-
let
|
|
625
|
+
]), R()), e.action(async (e) => {
|
|
626
|
+
let t = await Y(e);
|
|
227
627
|
try {
|
|
228
|
-
let
|
|
628
|
+
let n;
|
|
229
629
|
try {
|
|
230
|
-
|
|
630
|
+
n = await t.listUsers();
|
|
231
631
|
} catch (e) {
|
|
232
|
-
throw
|
|
632
|
+
throw pe(e) ? Error("The server does not let this user list users. Check the server option '--show-user-list-to' and the role of the user.", { cause: e }) : e;
|
|
233
633
|
}
|
|
234
|
-
let
|
|
235
|
-
e.format === "json" ?
|
|
634
|
+
let r = n.map((e) => e.login).sort();
|
|
635
|
+
e.format === "json" ? x(r) : e.format === "text" ? C(r.join("\n")) : l(["login"], r.map((e) => [e]));
|
|
236
636
|
} finally {
|
|
237
|
-
await
|
|
637
|
+
await t.close();
|
|
238
638
|
}
|
|
239
639
|
}), e;
|
|
240
640
|
}
|
|
241
641
|
// src/cmd/admin/delete-user.ts
|
|
242
|
-
function
|
|
243
|
-
let
|
|
244
|
-
return
|
|
245
|
-
if (
|
|
246
|
-
if (
|
|
247
|
-
if (
|
|
248
|
-
let
|
|
642
|
+
function et() {
|
|
643
|
+
let e = new w("delete-user").description("Delete a user account. Optionally re-homes the user's projects to another user first. Requires admin/controller credentials.");
|
|
644
|
+
return e.argument("<user>", "Username of the account to delete"), P(e, I(), R()), e.addOption(new E("--move-projects-to <user>", "Re-home the deleted user's projects to this user before deleting the account")), e.addOption(new E("--delete-projects", "Delete the user's projects along with the account, losing their data")), e.option("--force", "Skip confirmation", !1), e.action(async (e, n) => {
|
|
645
|
+
if (n.moveProjectsTo && n.deleteProjects) throw Error("--move-projects-to and --delete-projects are mutually exclusive");
|
|
646
|
+
if (n.moveProjectsTo === e) throw Error("--move-projects-to must name a different user than the one being deleted");
|
|
647
|
+
if (n.adminUser === e) throw Error(`Refusing to delete "${e}" — it is the account these credentials authenticate as.`);
|
|
648
|
+
let r = await Y(n);
|
|
249
649
|
try {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
if (!
|
|
253
|
-
|
|
650
|
+
if (t(r) === e) throw Error(`Refusing to delete "${e}" — it is the account this session authenticates as.`);
|
|
651
|
+
let i = await m(r, e), a = i.projectListRid === void 0 ? [] : await f(r, i.projectListRid);
|
|
652
|
+
if (a.length > 0 && !n.moveProjectsTo && !n.deleteProjects) throw Error(`User "${e}" owns ${a.length} project(s). Pass --move-projects-to <user> to re-home them, or --delete-projects to delete them with the account.`);
|
|
653
|
+
if (!await nt(e, a, n)) {
|
|
654
|
+
C("Aborted.");
|
|
254
655
|
return;
|
|
255
656
|
}
|
|
256
|
-
let
|
|
257
|
-
if (
|
|
258
|
-
let
|
|
259
|
-
|
|
657
|
+
let o = [];
|
|
658
|
+
if (n.moveProjectsTo && a.length > 0) {
|
|
659
|
+
let e = await m(r, n.moveProjectsTo), t = await ee(r, e.userRoot);
|
|
660
|
+
o = await se(r, i.projectListRid, t);
|
|
260
661
|
}
|
|
261
|
-
let
|
|
262
|
-
|
|
263
|
-
let
|
|
264
|
-
|
|
662
|
+
let s = i.projectListRid === void 0 ? [] : await f(r, i.projectListRid);
|
|
663
|
+
tt(e, n, a, s);
|
|
664
|
+
let c = await r.deleteUser(e);
|
|
665
|
+
n.format === "json" ? x({
|
|
265
666
|
deleted: !0,
|
|
266
|
-
user:
|
|
267
|
-
movedTo:
|
|
268
|
-
movedProjects:
|
|
269
|
-
deletedProjects:
|
|
270
|
-
userRootId:
|
|
271
|
-
userRootDeleted:
|
|
272
|
-
revokedGrants:
|
|
273
|
-
removedIdentityIndexEntries:
|
|
274
|
-
}) :
|
|
667
|
+
user: e,
|
|
668
|
+
movedTo: n.moveProjectsTo ?? null,
|
|
669
|
+
movedProjects: o,
|
|
670
|
+
deletedProjects: n.moveProjectsTo ? [] : s.map((e) => e.label),
|
|
671
|
+
userRootId: c.userRootId === void 0 ? null : c.userRootId.toString(),
|
|
672
|
+
userRootDeleted: c.userRootDeleted,
|
|
673
|
+
revokedGrants: c.revokedGrants,
|
|
674
|
+
removedIdentityIndexEntries: c.removedIdentityIndexEntries
|
|
675
|
+
}) : C(rt(e, n.moveProjectsTo, o, s));
|
|
275
676
|
} finally {
|
|
276
|
-
await
|
|
677
|
+
await r.close();
|
|
277
678
|
}
|
|
278
|
-
}),
|
|
679
|
+
}), e;
|
|
279
680
|
}
|
|
280
|
-
function
|
|
681
|
+
function tt(e, t, n, r) {
|
|
281
682
|
if (t.moveProjectsTo) {
|
|
282
683
|
if (r.length === 0) return;
|
|
283
684
|
throw Error(`User "${e}" still owns ${r.length} project(s) after the move: ${r.map((e) => e.label).join(", ")}. The account was NOT deleted — the projects were attached while the move ran. Re-run the command to move them too.`);
|
|
@@ -285,11 +686,11 @@ function oe(e, t, n, r) {
|
|
|
285
686
|
let i = new Set(n.map((e) => e.id)), a = r.filter((e) => !i.has(e.id));
|
|
286
687
|
if (a.length !== 0) throw Error(`User "${e}" gained ${a.length} project(s) since the list you confirmed: ${a.map((e) => e.label).join(", ")}. The account was NOT deleted — deleting it now would destroy them unreviewed. Re-run the command to see the current list.`);
|
|
287
688
|
}
|
|
288
|
-
async function
|
|
689
|
+
async function nt(e, t, n) {
|
|
289
690
|
if (n.force) return !0;
|
|
290
691
|
let r = n.moveProjectsTo ? `${t.length} project(s) will move to "${n.moveProjectsTo}"` : t.length > 0 ? `${t.length} project(s) will be PERMANENTLY DELETED` : "the account owns no projects";
|
|
291
692
|
process.stderr.write(`Delete user "${e}"? ${r}.\n`), t.length > 0 && process.stderr.write(t.map((e) => ` - ${e.label}`).join("\n") + "\n");
|
|
292
|
-
let i =
|
|
693
|
+
let i = M({
|
|
293
694
|
input: process.stdin,
|
|
294
695
|
output: process.stderr
|
|
295
696
|
});
|
|
@@ -301,9 +702,9 @@ async function se(e, t, n) {
|
|
|
301
702
|
i.close();
|
|
302
703
|
}
|
|
303
704
|
}
|
|
304
|
-
function
|
|
705
|
+
function rt(e, t, n, r) {
|
|
305
706
|
let i = [];
|
|
306
|
-
return t ? n.length > 0 ? (i.push(`Moved ${n.length} project(s) from ${e} to ${t}:`), i.push(
|
|
707
|
+
return t ? n.length > 0 ? (i.push(`Moved ${n.length} project(s) from ${e} to ${t}:`), i.push(h([
|
|
307
708
|
"id",
|
|
308
709
|
"name",
|
|
309
710
|
"name in target"
|
|
@@ -314,33 +715,33 @@ function ce(e, t, n, r) {
|
|
|
314
715
|
])))) : i.push(`User ${e} had no projects to move.`) : r.length > 0 && (i.push(`Deleted ${r.length} project(s) belonging to ${e}:`), i.push(r.map((e) => ` - ${e.label}`).join("\n"))), i.push(`Deleted user "${e}".`), i.join("\n");
|
|
315
716
|
}
|
|
316
717
|
// src/cmd/admin/instance-stats.ts
|
|
317
|
-
function
|
|
318
|
-
let e = new
|
|
319
|
-
return
|
|
320
|
-
let t = await
|
|
718
|
+
function it() {
|
|
719
|
+
let e = new w("instance-stats").description("Show the instance statistics as JSON.");
|
|
720
|
+
return P(e, I(), R()), e.action(async (e) => {
|
|
721
|
+
let t = await Y(e);
|
|
321
722
|
try {
|
|
322
|
-
let [e] = await t.listSharedResourcesByType(
|
|
723
|
+
let [e] = await t.listSharedResourcesByType(Se);
|
|
323
724
|
if (e === void 0) throw Error("The instance has no instance statistics.");
|
|
324
725
|
let n = await t.withReadTx("instanceStats", (t) => t.listKeyValuesString(e));
|
|
325
|
-
|
|
726
|
+
x(Ce(n));
|
|
326
727
|
} finally {
|
|
327
728
|
await t.close();
|
|
328
729
|
}
|
|
329
730
|
}), e;
|
|
330
731
|
}
|
|
331
732
|
// src/resource_id.ts
|
|
332
|
-
function
|
|
733
|
+
function Z(e) {
|
|
333
734
|
let t = e.trim();
|
|
334
|
-
if (t.includes("|")) return
|
|
335
|
-
let n =
|
|
336
|
-
if (n === void 0 && /^\d+$/.test(t) && (n = BigInt(t)), n === void 0 || n === 0n ||
|
|
337
|
-
return
|
|
735
|
+
if (t.includes("|")) return ue(t);
|
|
736
|
+
let n = he(t);
|
|
737
|
+
if (n === void 0 && /^\d+$/.test(t) && (n = BigInt(t)), n === void 0 || n === 0n || fe(n)) throw Error(`not a global resource ID: ${e}`);
|
|
738
|
+
return de(n);
|
|
338
739
|
}
|
|
339
740
|
// src/cmd/maintenance/list-resources.ts
|
|
340
|
-
function
|
|
341
|
-
let e = new
|
|
342
|
-
return
|
|
343
|
-
let t = await
|
|
741
|
+
function at() {
|
|
742
|
+
let e = new w("list-resources").description("List resources of one type as JSON lines, in ascending order of resource ID. Resource data and key-value values are printed as JSON when they are valid JSON, as text when they are valid UTF-8, and as {\"base64\": ...} in other cases. Requires admin/controller credentials.");
|
|
743
|
+
return P(e, F(), R()), e.addOption(new E("--type <name:version>", "Resource type to list (e.g. 'Blob/work:1')").argParser(lt).makeOptionMandatory()), e.addOption(new E("--limit <n>", "Maximum number of resources to list. 0 lists all resources").argParser(ut).default(0)), e.addOption(new E("--after <resource-id>", "List only resources with IDs greater than this ID. To get the next page, give the ID of the last resource of the previous page").argParser(Z)), e.option("--load-fields", "Load the fields and the error of each resource ('fields' and 'error' keys)", !1), e.option("--load-kv", "Load the key-value entries of each resource ('kv' key)", !1), e.action(async (e) => {
|
|
744
|
+
let t = await Y(e);
|
|
344
745
|
try {
|
|
345
746
|
let n = await t.withReadTx("listResources", async (t) => {
|
|
346
747
|
let n = await t.listResourcesByType(e.type, {
|
|
@@ -354,46 +755,46 @@ function ue() {
|
|
|
354
755
|
})));
|
|
355
756
|
});
|
|
356
757
|
for (let { resource: t, kv: r } of n) {
|
|
357
|
-
let n = JSON.stringify(
|
|
358
|
-
process.stdout.write(n) || await
|
|
758
|
+
let n = JSON.stringify(ot(t, e.loadFields, r)) + "\n";
|
|
759
|
+
process.stdout.write(n) || await we(process.stdout, "drain");
|
|
359
760
|
}
|
|
360
761
|
} finally {
|
|
361
762
|
await t.close();
|
|
362
763
|
}
|
|
363
764
|
}), e;
|
|
364
765
|
}
|
|
365
|
-
function
|
|
766
|
+
function ot(e, t, n) {
|
|
366
767
|
return {
|
|
367
768
|
id: A(e.id),
|
|
368
|
-
type:
|
|
769
|
+
type: _e(e.type),
|
|
369
770
|
kind: e.kind,
|
|
370
|
-
originalId:
|
|
771
|
+
originalId: Q(e.originalResourceId),
|
|
371
772
|
inputsLocked: e.inputsLocked,
|
|
372
773
|
outputsLocked: e.outputsLocked,
|
|
373
774
|
ready: e.resourceReady,
|
|
374
775
|
final: e.final,
|
|
375
|
-
data: e.data === void 0 || e.data.length === 0 ? null :
|
|
776
|
+
data: e.data === void 0 || e.data.length === 0 ? null : ct(e.data),
|
|
376
777
|
...t ? {
|
|
377
|
-
error:
|
|
378
|
-
fields: e.fields.map(
|
|
778
|
+
error: Q(e.error),
|
|
779
|
+
fields: e.fields.map(st)
|
|
379
780
|
} : {},
|
|
380
|
-
...n === void 0 ? {} : { kv: Object.fromEntries(n.map((e) => [e.key,
|
|
781
|
+
...n === void 0 ? {} : { kv: Object.fromEntries(n.map((e) => [e.key, ct(e.value)])) }
|
|
381
782
|
};
|
|
382
783
|
}
|
|
383
|
-
function
|
|
784
|
+
function st(e) {
|
|
384
785
|
return {
|
|
385
786
|
name: e.name,
|
|
386
787
|
type: e.type,
|
|
387
788
|
status: e.status,
|
|
388
|
-
value:
|
|
389
|
-
error:
|
|
789
|
+
value: Q(e.value),
|
|
790
|
+
error: Q(e.error),
|
|
390
791
|
valueIsFinal: e.valueIsFinal
|
|
391
792
|
};
|
|
392
793
|
}
|
|
393
|
-
function
|
|
794
|
+
function Q(e) {
|
|
394
795
|
return O(e) ? A(e) : null;
|
|
395
796
|
}
|
|
396
|
-
function
|
|
797
|
+
function ct(e) {
|
|
397
798
|
let t;
|
|
398
799
|
try {
|
|
399
800
|
t = new TextDecoder("utf-8", { fatal: !0 }).decode(e);
|
|
@@ -406,31 +807,31 @@ function Q(e) {
|
|
|
406
807
|
return t;
|
|
407
808
|
}
|
|
408
809
|
}
|
|
409
|
-
function
|
|
810
|
+
function lt(e) {
|
|
410
811
|
let t = e.lastIndexOf(":");
|
|
411
|
-
if (t <= 0 || t === e.length - 1) throw new
|
|
412
|
-
return
|
|
812
|
+
if (t <= 0 || t === e.length - 1) throw new T(`expected <name>:<version>, e.g. 'Blob/work:1': ${e}`);
|
|
813
|
+
return ge(e.slice(0, t), e.slice(t + 1));
|
|
413
814
|
}
|
|
414
|
-
function
|
|
815
|
+
function ut(e) {
|
|
415
816
|
let t = Number(e);
|
|
416
|
-
if (!Number.isInteger(t) || t < 0) throw new
|
|
817
|
+
if (!Number.isInteger(t) || t < 0) throw new T(`not a non-negative integer: ${e}`);
|
|
417
818
|
return t;
|
|
418
819
|
}
|
|
419
820
|
// src/cmd/maintenance/set-resource-error.ts
|
|
420
|
-
var
|
|
421
|
-
function
|
|
422
|
-
let e = new
|
|
423
|
-
return e.argument("<resource-id...>", "Resource IDs: as printed by the server (NG:0x1A2B), as decimal numbers, or as signed IDs (<decimal ID>|<signature hex>)"),
|
|
424
|
-
let
|
|
821
|
+
var dt = "Manual error set by administrator to trigger blocks re-run. Run block once again to get rid of it.";
|
|
822
|
+
function ft() {
|
|
823
|
+
let e = new w("set-resource-error").description("Put an error on one or more resources in a single transaction. Resources that already have an error are skipped. Requires admin/controller credentials.");
|
|
824
|
+
return e.argument("<resource-id...>", "Resource IDs: as printed by the server (NG:0x1A2B), as decimal numbers, or as signed IDs (<decimal ID>|<signature hex>)"), P(e, F(), R()), e.addOption(new E("-m, --error-message <message>", "Error message to put on the resources").default(dt)), e.option("--skip-missing", "Skip resources that do not exist instead of failing the whole transaction", !1), e.action(async (e, t) => {
|
|
825
|
+
let n = e.map(Z), r = await Y(t);
|
|
425
826
|
try {
|
|
426
|
-
let e = await
|
|
427
|
-
let
|
|
428
|
-
id:
|
|
429
|
-
data:
|
|
430
|
-
}))), i =
|
|
827
|
+
let e = await r.withWriteTx("setResourceError", async (e) => {
|
|
828
|
+
let r = await Promise.all(n.map(async (n) => ({
|
|
829
|
+
id: n,
|
|
830
|
+
data: t.skipMissing ? await e.getResourceDataIfExists(n, !0) : await e.getResourceData(n, !0, !0)
|
|
831
|
+
}))), i = r.filter((e) => e.data === void 0).map((e) => e.id), a = r.flatMap((e) => e.data === void 0 ? [] : [e.data]), o = a.filter((e) => !O(e.error)), s = a.filter((e) => O(e.error));
|
|
431
832
|
if (o.length > 0) {
|
|
432
|
-
let
|
|
433
|
-
for (let
|
|
833
|
+
let n = e.createError(t.errorMessage);
|
|
834
|
+
for (let t of o) e.setResourceError(t.id, n);
|
|
434
835
|
await e.commit();
|
|
435
836
|
}
|
|
436
837
|
return {
|
|
@@ -439,28 +840,46 @@ function ge() {
|
|
|
439
840
|
missing: i
|
|
440
841
|
};
|
|
441
842
|
});
|
|
442
|
-
for (let
|
|
443
|
-
for (let
|
|
444
|
-
for (let
|
|
843
|
+
for (let t of e.missing) C(`skipped ${A(t)}: resource does not exist`);
|
|
844
|
+
for (let t of e.skipped) C(`skipped ${A(t.id)}: resource already has an error`);
|
|
845
|
+
for (let t of e.targets) C(`error set ${A(t.id)}`);
|
|
445
846
|
} finally {
|
|
446
|
-
await
|
|
847
|
+
await r.close();
|
|
447
848
|
}
|
|
448
849
|
}), e;
|
|
449
850
|
}
|
|
450
851
|
// src/cli.ts
|
|
451
852
|
function $() {
|
|
452
|
-
let e = new
|
|
453
|
-
e.name("pl-cli").description("Platforma command-line client");
|
|
454
|
-
let t = new
|
|
455
|
-
t.addCommand(
|
|
456
|
-
let n = new
|
|
457
|
-
n.addCommand(
|
|
458
|
-
let r = new
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
853
|
+
let e = new w();
|
|
854
|
+
e.name("pl-cli").description("Platforma command-line client"), e.addHelpText("after", "\nAuthentication, in order of precedence:\n 1. --admin-user with --admin-password (admin commands), or --user with --password\n (PL_ADMIN_USER/PL_ADMIN_PASSWORD, PL_USER/PL_PASSWORD): sign in with them.\n 2. --admin-user or --user without a password: the stored session of that login.\n 3. No credentials: the current stored session of the Backend instance.\nWithout --address (and PL_ADDRESS), commands use the default session: its stored\naddress, if the Backend there still has the same instance ID. \"pl-cli login\" and\n\"pl-cli session select\" set the default session.\nCredentials in the address URL (http://user:password@host) count as --user/--password.\nEnvironment variables count as the flags: a PL_USER or PL_ADMIN_USER in your shell\nselects that login's stored session, and PL_USER with PL_PASSWORD bypasses the\nstored sessions. pl-cli prints a note when it takes credentials from the environment.\nSign in once with \"pl-cli login\" (needs Backend v3.6.0 or newer; on older Backends\ngive --user/--password with each command). Sessions are keyed by the instance ID from the\nping answer of the Backend, not by its address. They are stored in\n~/.platforma/pl-cli.sessions (override with PL_CLI_SESSIONS_FILE)."), e.hook("preAction", (e, t) => mt(t)), e.addCommand(Ne()), e.addCommand(ze());
|
|
855
|
+
let t = new w("session").description("Manage stored sessions (one file, sessions keyed by Backend instance ID)");
|
|
856
|
+
t.addCommand(Ve()), t.addCommand(Ue()), t.addCommand(Ge()), e.addCommand(t);
|
|
857
|
+
let n = new w("project").description("Manage projects");
|
|
858
|
+
n.addCommand(qe()), n.addCommand(Je()), n.addCommand(Ye()), n.addCommand(Xe()), n.addCommand(Ze()), e.addCommand(n);
|
|
859
|
+
let r = new w("admin").description("Admin operations (requires controller credentials or an admin's stored session)");
|
|
860
|
+
r.addCommand(Qe()), r.addCommand($e()), r.addCommand(et()), r.addCommand(it()), e.addCommand(r);
|
|
861
|
+
let i = new w("maintenance").description("Low-level server state repair (requires admin/controller credentials or an admin's stored session)");
|
|
862
|
+
return i.addCommand(at()), i.addCommand(ft()), e.addCommand(i), e;
|
|
863
|
+
}
|
|
864
|
+
var pt = {
|
|
865
|
+
user: "PL_USER",
|
|
866
|
+
password: "PL_PASSWORD",
|
|
867
|
+
adminUser: "PL_ADMIN_USER",
|
|
868
|
+
adminPassword: "PL_ADMIN_PASSWORD"
|
|
869
|
+
};
|
|
870
|
+
function mt(e) {
|
|
871
|
+
let t = Object.entries(pt).filter(([t]) => e.getOptionValueSource(t) === "env").map(([, e]) => e);
|
|
872
|
+
t.length > 0 && process.stderr.write(`note: using credentials from the environment: ${t.join(", ")}\n`);
|
|
873
|
+
}
|
|
874
|
+
async function ht(t = process.argv) {
|
|
875
|
+
a();
|
|
876
|
+
try {
|
|
877
|
+
await $().parseAsync(t);
|
|
878
|
+
} catch (t) {
|
|
879
|
+
let n = e();
|
|
880
|
+
throw n !== void 0 && !(t instanceof s) && me(t) ? Error(te(n, "refused"), { cause: t }) : t;
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
export { $ as buildProgram, ht as run };
|
|
465
884
|
|
|
466
885
|
//# sourceMappingURL=cli.js.map
|