lldap-cli 1.0.3
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/LICENSE.md +21 -0
- package/README.md +368 -0
- package/dist/cli.js +3626 -0
- package/package.json +58 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,3626 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// @bun
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
var __create = Object.create;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __defProp = Object.defineProperty;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
10
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
11
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
12
|
+
for (let key of __getOwnPropNames(mod))
|
|
13
|
+
if (!__hasOwnProp.call(to, key))
|
|
14
|
+
__defProp(to, key, {
|
|
15
|
+
get: () => mod[key],
|
|
16
|
+
enumerable: true
|
|
17
|
+
});
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
21
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
22
|
+
|
|
23
|
+
// node_modules/commander/lib/error.js
|
|
24
|
+
var require_error = __commonJS((exports) => {
|
|
25
|
+
class CommanderError extends Error {
|
|
26
|
+
constructor(exitCode, code, message) {
|
|
27
|
+
super(message);
|
|
28
|
+
Error.captureStackTrace(this, this.constructor);
|
|
29
|
+
this.name = this.constructor.name;
|
|
30
|
+
this.code = code;
|
|
31
|
+
this.exitCode = exitCode;
|
|
32
|
+
this.nestedError = undefined;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
class InvalidArgumentError extends CommanderError {
|
|
37
|
+
constructor(message) {
|
|
38
|
+
super(1, "commander.invalidArgument", message);
|
|
39
|
+
Error.captureStackTrace(this, this.constructor);
|
|
40
|
+
this.name = this.constructor.name;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.CommanderError = CommanderError;
|
|
44
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// node_modules/commander/lib/argument.js
|
|
48
|
+
var require_argument = __commonJS((exports) => {
|
|
49
|
+
var { InvalidArgumentError } = require_error();
|
|
50
|
+
|
|
51
|
+
class Argument {
|
|
52
|
+
constructor(name, description) {
|
|
53
|
+
this.description = description || "";
|
|
54
|
+
this.variadic = false;
|
|
55
|
+
this.parseArg = undefined;
|
|
56
|
+
this.defaultValue = undefined;
|
|
57
|
+
this.defaultValueDescription = undefined;
|
|
58
|
+
this.argChoices = undefined;
|
|
59
|
+
switch (name[0]) {
|
|
60
|
+
case "<":
|
|
61
|
+
this.required = true;
|
|
62
|
+
this._name = name.slice(1, -1);
|
|
63
|
+
break;
|
|
64
|
+
case "[":
|
|
65
|
+
this.required = false;
|
|
66
|
+
this._name = name.slice(1, -1);
|
|
67
|
+
break;
|
|
68
|
+
default:
|
|
69
|
+
this.required = true;
|
|
70
|
+
this._name = name;
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
if (this._name.length > 3 && this._name.slice(-3) === "...") {
|
|
74
|
+
this.variadic = true;
|
|
75
|
+
this._name = this._name.slice(0, -3);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
name() {
|
|
79
|
+
return this._name;
|
|
80
|
+
}
|
|
81
|
+
_concatValue(value, previous) {
|
|
82
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
83
|
+
return [value];
|
|
84
|
+
}
|
|
85
|
+
return previous.concat(value);
|
|
86
|
+
}
|
|
87
|
+
default(value, description) {
|
|
88
|
+
this.defaultValue = value;
|
|
89
|
+
this.defaultValueDescription = description;
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
argParser(fn) {
|
|
93
|
+
this.parseArg = fn;
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
choices(values) {
|
|
97
|
+
this.argChoices = values.slice();
|
|
98
|
+
this.parseArg = (arg, previous) => {
|
|
99
|
+
if (!this.argChoices.includes(arg)) {
|
|
100
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
101
|
+
}
|
|
102
|
+
if (this.variadic) {
|
|
103
|
+
return this._concatValue(arg, previous);
|
|
104
|
+
}
|
|
105
|
+
return arg;
|
|
106
|
+
};
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
argRequired() {
|
|
110
|
+
this.required = true;
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
argOptional() {
|
|
114
|
+
this.required = false;
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function humanReadableArgName(arg) {
|
|
119
|
+
const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");
|
|
120
|
+
return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
|
|
121
|
+
}
|
|
122
|
+
exports.Argument = Argument;
|
|
123
|
+
exports.humanReadableArgName = humanReadableArgName;
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// node_modules/commander/lib/help.js
|
|
127
|
+
var require_help = __commonJS((exports) => {
|
|
128
|
+
var { humanReadableArgName } = require_argument();
|
|
129
|
+
|
|
130
|
+
class Help {
|
|
131
|
+
constructor() {
|
|
132
|
+
this.helpWidth = undefined;
|
|
133
|
+
this.sortSubcommands = false;
|
|
134
|
+
this.sortOptions = false;
|
|
135
|
+
this.showGlobalOptions = false;
|
|
136
|
+
}
|
|
137
|
+
visibleCommands(cmd) {
|
|
138
|
+
const visibleCommands = cmd.commands.filter((cmd2) => !cmd2._hidden);
|
|
139
|
+
const helpCommand = cmd._getHelpCommand();
|
|
140
|
+
if (helpCommand && !helpCommand._hidden) {
|
|
141
|
+
visibleCommands.push(helpCommand);
|
|
142
|
+
}
|
|
143
|
+
if (this.sortSubcommands) {
|
|
144
|
+
visibleCommands.sort((a, b) => {
|
|
145
|
+
return a.name().localeCompare(b.name());
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
return visibleCommands;
|
|
149
|
+
}
|
|
150
|
+
compareOptions(a, b) {
|
|
151
|
+
const getSortKey = (option) => {
|
|
152
|
+
return option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "");
|
|
153
|
+
};
|
|
154
|
+
return getSortKey(a).localeCompare(getSortKey(b));
|
|
155
|
+
}
|
|
156
|
+
visibleOptions(cmd) {
|
|
157
|
+
const visibleOptions = cmd.options.filter((option) => !option.hidden);
|
|
158
|
+
const helpOption = cmd._getHelpOption();
|
|
159
|
+
if (helpOption && !helpOption.hidden) {
|
|
160
|
+
const removeShort = helpOption.short && cmd._findOption(helpOption.short);
|
|
161
|
+
const removeLong = helpOption.long && cmd._findOption(helpOption.long);
|
|
162
|
+
if (!removeShort && !removeLong) {
|
|
163
|
+
visibleOptions.push(helpOption);
|
|
164
|
+
} else if (helpOption.long && !removeLong) {
|
|
165
|
+
visibleOptions.push(cmd.createOption(helpOption.long, helpOption.description));
|
|
166
|
+
} else if (helpOption.short && !removeShort) {
|
|
167
|
+
visibleOptions.push(cmd.createOption(helpOption.short, helpOption.description));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (this.sortOptions) {
|
|
171
|
+
visibleOptions.sort(this.compareOptions);
|
|
172
|
+
}
|
|
173
|
+
return visibleOptions;
|
|
174
|
+
}
|
|
175
|
+
visibleGlobalOptions(cmd) {
|
|
176
|
+
if (!this.showGlobalOptions)
|
|
177
|
+
return [];
|
|
178
|
+
const globalOptions = [];
|
|
179
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
180
|
+
const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
|
|
181
|
+
globalOptions.push(...visibleOptions);
|
|
182
|
+
}
|
|
183
|
+
if (this.sortOptions) {
|
|
184
|
+
globalOptions.sort(this.compareOptions);
|
|
185
|
+
}
|
|
186
|
+
return globalOptions;
|
|
187
|
+
}
|
|
188
|
+
visibleArguments(cmd) {
|
|
189
|
+
if (cmd._argsDescription) {
|
|
190
|
+
cmd.registeredArguments.forEach((argument) => {
|
|
191
|
+
argument.description = argument.description || cmd._argsDescription[argument.name()] || "";
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
if (cmd.registeredArguments.find((argument) => argument.description)) {
|
|
195
|
+
return cmd.registeredArguments;
|
|
196
|
+
}
|
|
197
|
+
return [];
|
|
198
|
+
}
|
|
199
|
+
subcommandTerm(cmd) {
|
|
200
|
+
const args = cmd.registeredArguments.map((arg) => humanReadableArgName(arg)).join(" ");
|
|
201
|
+
return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (cmd.options.length ? " [options]" : "") + (args ? " " + args : "");
|
|
202
|
+
}
|
|
203
|
+
optionTerm(option) {
|
|
204
|
+
return option.flags;
|
|
205
|
+
}
|
|
206
|
+
argumentTerm(argument) {
|
|
207
|
+
return argument.name();
|
|
208
|
+
}
|
|
209
|
+
longestSubcommandTermLength(cmd, helper) {
|
|
210
|
+
return helper.visibleCommands(cmd).reduce((max, command) => {
|
|
211
|
+
return Math.max(max, helper.subcommandTerm(command).length);
|
|
212
|
+
}, 0);
|
|
213
|
+
}
|
|
214
|
+
longestOptionTermLength(cmd, helper) {
|
|
215
|
+
return helper.visibleOptions(cmd).reduce((max, option) => {
|
|
216
|
+
return Math.max(max, helper.optionTerm(option).length);
|
|
217
|
+
}, 0);
|
|
218
|
+
}
|
|
219
|
+
longestGlobalOptionTermLength(cmd, helper) {
|
|
220
|
+
return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
|
|
221
|
+
return Math.max(max, helper.optionTerm(option).length);
|
|
222
|
+
}, 0);
|
|
223
|
+
}
|
|
224
|
+
longestArgumentTermLength(cmd, helper) {
|
|
225
|
+
return helper.visibleArguments(cmd).reduce((max, argument) => {
|
|
226
|
+
return Math.max(max, helper.argumentTerm(argument).length);
|
|
227
|
+
}, 0);
|
|
228
|
+
}
|
|
229
|
+
commandUsage(cmd) {
|
|
230
|
+
let cmdName = cmd._name;
|
|
231
|
+
if (cmd._aliases[0]) {
|
|
232
|
+
cmdName = cmdName + "|" + cmd._aliases[0];
|
|
233
|
+
}
|
|
234
|
+
let ancestorCmdNames = "";
|
|
235
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
236
|
+
ancestorCmdNames = ancestorCmd.name() + " " + ancestorCmdNames;
|
|
237
|
+
}
|
|
238
|
+
return ancestorCmdNames + cmdName + " " + cmd.usage();
|
|
239
|
+
}
|
|
240
|
+
commandDescription(cmd) {
|
|
241
|
+
return cmd.description();
|
|
242
|
+
}
|
|
243
|
+
subcommandDescription(cmd) {
|
|
244
|
+
return cmd.summary() || cmd.description();
|
|
245
|
+
}
|
|
246
|
+
optionDescription(option) {
|
|
247
|
+
const extraInfo = [];
|
|
248
|
+
if (option.argChoices) {
|
|
249
|
+
extraInfo.push(`choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
250
|
+
}
|
|
251
|
+
if (option.defaultValue !== undefined) {
|
|
252
|
+
const showDefault = option.required || option.optional || option.isBoolean() && typeof option.defaultValue === "boolean";
|
|
253
|
+
if (showDefault) {
|
|
254
|
+
extraInfo.push(`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (option.presetArg !== undefined && option.optional) {
|
|
258
|
+
extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
|
|
259
|
+
}
|
|
260
|
+
if (option.envVar !== undefined) {
|
|
261
|
+
extraInfo.push(`env: ${option.envVar}`);
|
|
262
|
+
}
|
|
263
|
+
if (extraInfo.length > 0) {
|
|
264
|
+
return `${option.description} (${extraInfo.join(", ")})`;
|
|
265
|
+
}
|
|
266
|
+
return option.description;
|
|
267
|
+
}
|
|
268
|
+
argumentDescription(argument) {
|
|
269
|
+
const extraInfo = [];
|
|
270
|
+
if (argument.argChoices) {
|
|
271
|
+
extraInfo.push(`choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
272
|
+
}
|
|
273
|
+
if (argument.defaultValue !== undefined) {
|
|
274
|
+
extraInfo.push(`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`);
|
|
275
|
+
}
|
|
276
|
+
if (extraInfo.length > 0) {
|
|
277
|
+
const extraDescripton = `(${extraInfo.join(", ")})`;
|
|
278
|
+
if (argument.description) {
|
|
279
|
+
return `${argument.description} ${extraDescripton}`;
|
|
280
|
+
}
|
|
281
|
+
return extraDescripton;
|
|
282
|
+
}
|
|
283
|
+
return argument.description;
|
|
284
|
+
}
|
|
285
|
+
formatHelp(cmd, helper) {
|
|
286
|
+
const termWidth = helper.padWidth(cmd, helper);
|
|
287
|
+
const helpWidth = helper.helpWidth || 80;
|
|
288
|
+
const itemIndentWidth = 2;
|
|
289
|
+
const itemSeparatorWidth = 2;
|
|
290
|
+
function formatItem(term, description) {
|
|
291
|
+
if (description) {
|
|
292
|
+
const fullText = `${term.padEnd(termWidth + itemSeparatorWidth)}${description}`;
|
|
293
|
+
return helper.wrap(fullText, helpWidth - itemIndentWidth, termWidth + itemSeparatorWidth);
|
|
294
|
+
}
|
|
295
|
+
return term;
|
|
296
|
+
}
|
|
297
|
+
function formatList(textArray) {
|
|
298
|
+
return textArray.join(`
|
|
299
|
+
`).replace(/^/gm, " ".repeat(itemIndentWidth));
|
|
300
|
+
}
|
|
301
|
+
let output = [`Usage: ${helper.commandUsage(cmd)}`, ""];
|
|
302
|
+
const commandDescription = helper.commandDescription(cmd);
|
|
303
|
+
if (commandDescription.length > 0) {
|
|
304
|
+
output = output.concat([
|
|
305
|
+
helper.wrap(commandDescription, helpWidth, 0),
|
|
306
|
+
""
|
|
307
|
+
]);
|
|
308
|
+
}
|
|
309
|
+
const argumentList = helper.visibleArguments(cmd).map((argument) => {
|
|
310
|
+
return formatItem(helper.argumentTerm(argument), helper.argumentDescription(argument));
|
|
311
|
+
});
|
|
312
|
+
if (argumentList.length > 0) {
|
|
313
|
+
output = output.concat(["Arguments:", formatList(argumentList), ""]);
|
|
314
|
+
}
|
|
315
|
+
const optionList = helper.visibleOptions(cmd).map((option) => {
|
|
316
|
+
return formatItem(helper.optionTerm(option), helper.optionDescription(option));
|
|
317
|
+
});
|
|
318
|
+
if (optionList.length > 0) {
|
|
319
|
+
output = output.concat(["Options:", formatList(optionList), ""]);
|
|
320
|
+
}
|
|
321
|
+
if (this.showGlobalOptions) {
|
|
322
|
+
const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
|
|
323
|
+
return formatItem(helper.optionTerm(option), helper.optionDescription(option));
|
|
324
|
+
});
|
|
325
|
+
if (globalOptionList.length > 0) {
|
|
326
|
+
output = output.concat([
|
|
327
|
+
"Global Options:",
|
|
328
|
+
formatList(globalOptionList),
|
|
329
|
+
""
|
|
330
|
+
]);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
const commandList = helper.visibleCommands(cmd).map((cmd2) => {
|
|
334
|
+
return formatItem(helper.subcommandTerm(cmd2), helper.subcommandDescription(cmd2));
|
|
335
|
+
});
|
|
336
|
+
if (commandList.length > 0) {
|
|
337
|
+
output = output.concat(["Commands:", formatList(commandList), ""]);
|
|
338
|
+
}
|
|
339
|
+
return output.join(`
|
|
340
|
+
`);
|
|
341
|
+
}
|
|
342
|
+
padWidth(cmd, helper) {
|
|
343
|
+
return Math.max(helper.longestOptionTermLength(cmd, helper), helper.longestGlobalOptionTermLength(cmd, helper), helper.longestSubcommandTermLength(cmd, helper), helper.longestArgumentTermLength(cmd, helper));
|
|
344
|
+
}
|
|
345
|
+
wrap(str, width, indent, minColumnWidth = 40) {
|
|
346
|
+
const indents = " \\f\\t\\v - \uFEFF";
|
|
347
|
+
const manualIndent = new RegExp(`[\\n][${indents}]+`);
|
|
348
|
+
if (str.match(manualIndent))
|
|
349
|
+
return str;
|
|
350
|
+
const columnWidth = width - indent;
|
|
351
|
+
if (columnWidth < minColumnWidth)
|
|
352
|
+
return str;
|
|
353
|
+
const leadingStr = str.slice(0, indent);
|
|
354
|
+
const columnText = str.slice(indent).replace(`\r
|
|
355
|
+
`, `
|
|
356
|
+
`);
|
|
357
|
+
const indentString = " ".repeat(indent);
|
|
358
|
+
const zeroWidthSpace = "";
|
|
359
|
+
const breaks = `\\s${zeroWidthSpace}`;
|
|
360
|
+
const regex = new RegExp(`
|
|
361
|
+
|.{1,${columnWidth - 1}}([${breaks}]|$)|[^${breaks}]+?([${breaks}]|$)`, "g");
|
|
362
|
+
const lines = columnText.match(regex) || [];
|
|
363
|
+
return leadingStr + lines.map((line, i) => {
|
|
364
|
+
if (line === `
|
|
365
|
+
`)
|
|
366
|
+
return "";
|
|
367
|
+
return (i > 0 ? indentString : "") + line.trimEnd();
|
|
368
|
+
}).join(`
|
|
369
|
+
`);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
exports.Help = Help;
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
// node_modules/commander/lib/option.js
|
|
376
|
+
var require_option = __commonJS((exports) => {
|
|
377
|
+
var { InvalidArgumentError } = require_error();
|
|
378
|
+
|
|
379
|
+
class Option {
|
|
380
|
+
constructor(flags, description) {
|
|
381
|
+
this.flags = flags;
|
|
382
|
+
this.description = description || "";
|
|
383
|
+
this.required = flags.includes("<");
|
|
384
|
+
this.optional = flags.includes("[");
|
|
385
|
+
this.variadic = /\w\.\.\.[>\]]$/.test(flags);
|
|
386
|
+
this.mandatory = false;
|
|
387
|
+
const optionFlags = splitOptionFlags(flags);
|
|
388
|
+
this.short = optionFlags.shortFlag;
|
|
389
|
+
this.long = optionFlags.longFlag;
|
|
390
|
+
this.negate = false;
|
|
391
|
+
if (this.long) {
|
|
392
|
+
this.negate = this.long.startsWith("--no-");
|
|
393
|
+
}
|
|
394
|
+
this.defaultValue = undefined;
|
|
395
|
+
this.defaultValueDescription = undefined;
|
|
396
|
+
this.presetArg = undefined;
|
|
397
|
+
this.envVar = undefined;
|
|
398
|
+
this.parseArg = undefined;
|
|
399
|
+
this.hidden = false;
|
|
400
|
+
this.argChoices = undefined;
|
|
401
|
+
this.conflictsWith = [];
|
|
402
|
+
this.implied = undefined;
|
|
403
|
+
}
|
|
404
|
+
default(value, description) {
|
|
405
|
+
this.defaultValue = value;
|
|
406
|
+
this.defaultValueDescription = description;
|
|
407
|
+
return this;
|
|
408
|
+
}
|
|
409
|
+
preset(arg) {
|
|
410
|
+
this.presetArg = arg;
|
|
411
|
+
return this;
|
|
412
|
+
}
|
|
413
|
+
conflicts(names) {
|
|
414
|
+
this.conflictsWith = this.conflictsWith.concat(names);
|
|
415
|
+
return this;
|
|
416
|
+
}
|
|
417
|
+
implies(impliedOptionValues) {
|
|
418
|
+
let newImplied = impliedOptionValues;
|
|
419
|
+
if (typeof impliedOptionValues === "string") {
|
|
420
|
+
newImplied = { [impliedOptionValues]: true };
|
|
421
|
+
}
|
|
422
|
+
this.implied = Object.assign(this.implied || {}, newImplied);
|
|
423
|
+
return this;
|
|
424
|
+
}
|
|
425
|
+
env(name) {
|
|
426
|
+
this.envVar = name;
|
|
427
|
+
return this;
|
|
428
|
+
}
|
|
429
|
+
argParser(fn) {
|
|
430
|
+
this.parseArg = fn;
|
|
431
|
+
return this;
|
|
432
|
+
}
|
|
433
|
+
makeOptionMandatory(mandatory = true) {
|
|
434
|
+
this.mandatory = !!mandatory;
|
|
435
|
+
return this;
|
|
436
|
+
}
|
|
437
|
+
hideHelp(hide = true) {
|
|
438
|
+
this.hidden = !!hide;
|
|
439
|
+
return this;
|
|
440
|
+
}
|
|
441
|
+
_concatValue(value, previous) {
|
|
442
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
443
|
+
return [value];
|
|
444
|
+
}
|
|
445
|
+
return previous.concat(value);
|
|
446
|
+
}
|
|
447
|
+
choices(values) {
|
|
448
|
+
this.argChoices = values.slice();
|
|
449
|
+
this.parseArg = (arg, previous) => {
|
|
450
|
+
if (!this.argChoices.includes(arg)) {
|
|
451
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
452
|
+
}
|
|
453
|
+
if (this.variadic) {
|
|
454
|
+
return this._concatValue(arg, previous);
|
|
455
|
+
}
|
|
456
|
+
return arg;
|
|
457
|
+
};
|
|
458
|
+
return this;
|
|
459
|
+
}
|
|
460
|
+
name() {
|
|
461
|
+
if (this.long) {
|
|
462
|
+
return this.long.replace(/^--/, "");
|
|
463
|
+
}
|
|
464
|
+
return this.short.replace(/^-/, "");
|
|
465
|
+
}
|
|
466
|
+
attributeName() {
|
|
467
|
+
return camelcase(this.name().replace(/^no-/, ""));
|
|
468
|
+
}
|
|
469
|
+
is(arg) {
|
|
470
|
+
return this.short === arg || this.long === arg;
|
|
471
|
+
}
|
|
472
|
+
isBoolean() {
|
|
473
|
+
return !this.required && !this.optional && !this.negate;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
class DualOptions {
|
|
478
|
+
constructor(options) {
|
|
479
|
+
this.positiveOptions = new Map;
|
|
480
|
+
this.negativeOptions = new Map;
|
|
481
|
+
this.dualOptions = new Set;
|
|
482
|
+
options.forEach((option) => {
|
|
483
|
+
if (option.negate) {
|
|
484
|
+
this.negativeOptions.set(option.attributeName(), option);
|
|
485
|
+
} else {
|
|
486
|
+
this.positiveOptions.set(option.attributeName(), option);
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
this.negativeOptions.forEach((value, key) => {
|
|
490
|
+
if (this.positiveOptions.has(key)) {
|
|
491
|
+
this.dualOptions.add(key);
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
valueFromOption(value, option) {
|
|
496
|
+
const optionKey = option.attributeName();
|
|
497
|
+
if (!this.dualOptions.has(optionKey))
|
|
498
|
+
return true;
|
|
499
|
+
const preset = this.negativeOptions.get(optionKey).presetArg;
|
|
500
|
+
const negativeValue = preset !== undefined ? preset : false;
|
|
501
|
+
return option.negate === (negativeValue === value);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
function camelcase(str) {
|
|
505
|
+
return str.split("-").reduce((str2, word) => {
|
|
506
|
+
return str2 + word[0].toUpperCase() + word.slice(1);
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
function splitOptionFlags(flags) {
|
|
510
|
+
let shortFlag;
|
|
511
|
+
let longFlag;
|
|
512
|
+
const flagParts = flags.split(/[ |,]+/);
|
|
513
|
+
if (flagParts.length > 1 && !/^[[<]/.test(flagParts[1]))
|
|
514
|
+
shortFlag = flagParts.shift();
|
|
515
|
+
longFlag = flagParts.shift();
|
|
516
|
+
if (!shortFlag && /^-[^-]$/.test(longFlag)) {
|
|
517
|
+
shortFlag = longFlag;
|
|
518
|
+
longFlag = undefined;
|
|
519
|
+
}
|
|
520
|
+
return { shortFlag, longFlag };
|
|
521
|
+
}
|
|
522
|
+
exports.Option = Option;
|
|
523
|
+
exports.DualOptions = DualOptions;
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
// node_modules/commander/lib/suggestSimilar.js
|
|
527
|
+
var require_suggestSimilar = __commonJS((exports) => {
|
|
528
|
+
var maxDistance = 3;
|
|
529
|
+
function editDistance(a, b) {
|
|
530
|
+
if (Math.abs(a.length - b.length) > maxDistance)
|
|
531
|
+
return Math.max(a.length, b.length);
|
|
532
|
+
const d = [];
|
|
533
|
+
for (let i = 0;i <= a.length; i++) {
|
|
534
|
+
d[i] = [i];
|
|
535
|
+
}
|
|
536
|
+
for (let j = 0;j <= b.length; j++) {
|
|
537
|
+
d[0][j] = j;
|
|
538
|
+
}
|
|
539
|
+
for (let j = 1;j <= b.length; j++) {
|
|
540
|
+
for (let i = 1;i <= a.length; i++) {
|
|
541
|
+
let cost = 1;
|
|
542
|
+
if (a[i - 1] === b[j - 1]) {
|
|
543
|
+
cost = 0;
|
|
544
|
+
} else {
|
|
545
|
+
cost = 1;
|
|
546
|
+
}
|
|
547
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
|
548
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
549
|
+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
return d[a.length][b.length];
|
|
554
|
+
}
|
|
555
|
+
function suggestSimilar(word, candidates) {
|
|
556
|
+
if (!candidates || candidates.length === 0)
|
|
557
|
+
return "";
|
|
558
|
+
candidates = Array.from(new Set(candidates));
|
|
559
|
+
const searchingOptions = word.startsWith("--");
|
|
560
|
+
if (searchingOptions) {
|
|
561
|
+
word = word.slice(2);
|
|
562
|
+
candidates = candidates.map((candidate) => candidate.slice(2));
|
|
563
|
+
}
|
|
564
|
+
let similar = [];
|
|
565
|
+
let bestDistance = maxDistance;
|
|
566
|
+
const minSimilarity = 0.4;
|
|
567
|
+
candidates.forEach((candidate) => {
|
|
568
|
+
if (candidate.length <= 1)
|
|
569
|
+
return;
|
|
570
|
+
const distance = editDistance(word, candidate);
|
|
571
|
+
const length = Math.max(word.length, candidate.length);
|
|
572
|
+
const similarity = (length - distance) / length;
|
|
573
|
+
if (similarity > minSimilarity) {
|
|
574
|
+
if (distance < bestDistance) {
|
|
575
|
+
bestDistance = distance;
|
|
576
|
+
similar = [candidate];
|
|
577
|
+
} else if (distance === bestDistance) {
|
|
578
|
+
similar.push(candidate);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
similar.sort((a, b) => a.localeCompare(b));
|
|
583
|
+
if (searchingOptions) {
|
|
584
|
+
similar = similar.map((candidate) => `--${candidate}`);
|
|
585
|
+
}
|
|
586
|
+
if (similar.length > 1) {
|
|
587
|
+
return `
|
|
588
|
+
(Did you mean one of ${similar.join(", ")}?)`;
|
|
589
|
+
}
|
|
590
|
+
if (similar.length === 1) {
|
|
591
|
+
return `
|
|
592
|
+
(Did you mean ${similar[0]}?)`;
|
|
593
|
+
}
|
|
594
|
+
return "";
|
|
595
|
+
}
|
|
596
|
+
exports.suggestSimilar = suggestSimilar;
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
// node_modules/commander/lib/command.js
|
|
600
|
+
var require_command = __commonJS((exports) => {
|
|
601
|
+
var EventEmitter = __require("node:events").EventEmitter;
|
|
602
|
+
var childProcess = __require("node:child_process");
|
|
603
|
+
var path = __require("node:path");
|
|
604
|
+
var fs = __require("node:fs");
|
|
605
|
+
var process2 = __require("node:process");
|
|
606
|
+
var { Argument, humanReadableArgName } = require_argument();
|
|
607
|
+
var { CommanderError } = require_error();
|
|
608
|
+
var { Help } = require_help();
|
|
609
|
+
var { Option, DualOptions } = require_option();
|
|
610
|
+
var { suggestSimilar } = require_suggestSimilar();
|
|
611
|
+
|
|
612
|
+
class Command extends EventEmitter {
|
|
613
|
+
constructor(name) {
|
|
614
|
+
super();
|
|
615
|
+
this.commands = [];
|
|
616
|
+
this.options = [];
|
|
617
|
+
this.parent = null;
|
|
618
|
+
this._allowUnknownOption = false;
|
|
619
|
+
this._allowExcessArguments = true;
|
|
620
|
+
this.registeredArguments = [];
|
|
621
|
+
this._args = this.registeredArguments;
|
|
622
|
+
this.args = [];
|
|
623
|
+
this.rawArgs = [];
|
|
624
|
+
this.processedArgs = [];
|
|
625
|
+
this._scriptPath = null;
|
|
626
|
+
this._name = name || "";
|
|
627
|
+
this._optionValues = {};
|
|
628
|
+
this._optionValueSources = {};
|
|
629
|
+
this._storeOptionsAsProperties = false;
|
|
630
|
+
this._actionHandler = null;
|
|
631
|
+
this._executableHandler = false;
|
|
632
|
+
this._executableFile = null;
|
|
633
|
+
this._executableDir = null;
|
|
634
|
+
this._defaultCommandName = null;
|
|
635
|
+
this._exitCallback = null;
|
|
636
|
+
this._aliases = [];
|
|
637
|
+
this._combineFlagAndOptionalValue = true;
|
|
638
|
+
this._description = "";
|
|
639
|
+
this._summary = "";
|
|
640
|
+
this._argsDescription = undefined;
|
|
641
|
+
this._enablePositionalOptions = false;
|
|
642
|
+
this._passThroughOptions = false;
|
|
643
|
+
this._lifeCycleHooks = {};
|
|
644
|
+
this._showHelpAfterError = false;
|
|
645
|
+
this._showSuggestionAfterError = true;
|
|
646
|
+
this._outputConfiguration = {
|
|
647
|
+
writeOut: (str) => process2.stdout.write(str),
|
|
648
|
+
writeErr: (str) => process2.stderr.write(str),
|
|
649
|
+
getOutHelpWidth: () => process2.stdout.isTTY ? process2.stdout.columns : undefined,
|
|
650
|
+
getErrHelpWidth: () => process2.stderr.isTTY ? process2.stderr.columns : undefined,
|
|
651
|
+
outputError: (str, write) => write(str)
|
|
652
|
+
};
|
|
653
|
+
this._hidden = false;
|
|
654
|
+
this._helpOption = undefined;
|
|
655
|
+
this._addImplicitHelpCommand = undefined;
|
|
656
|
+
this._helpCommand = undefined;
|
|
657
|
+
this._helpConfiguration = {};
|
|
658
|
+
}
|
|
659
|
+
copyInheritedSettings(sourceCommand) {
|
|
660
|
+
this._outputConfiguration = sourceCommand._outputConfiguration;
|
|
661
|
+
this._helpOption = sourceCommand._helpOption;
|
|
662
|
+
this._helpCommand = sourceCommand._helpCommand;
|
|
663
|
+
this._helpConfiguration = sourceCommand._helpConfiguration;
|
|
664
|
+
this._exitCallback = sourceCommand._exitCallback;
|
|
665
|
+
this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
|
|
666
|
+
this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
|
|
667
|
+
this._allowExcessArguments = sourceCommand._allowExcessArguments;
|
|
668
|
+
this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
|
|
669
|
+
this._showHelpAfterError = sourceCommand._showHelpAfterError;
|
|
670
|
+
this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
|
|
671
|
+
return this;
|
|
672
|
+
}
|
|
673
|
+
_getCommandAndAncestors() {
|
|
674
|
+
const result = [];
|
|
675
|
+
for (let command = this;command; command = command.parent) {
|
|
676
|
+
result.push(command);
|
|
677
|
+
}
|
|
678
|
+
return result;
|
|
679
|
+
}
|
|
680
|
+
command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
|
|
681
|
+
let desc = actionOptsOrExecDesc;
|
|
682
|
+
let opts = execOpts;
|
|
683
|
+
if (typeof desc === "object" && desc !== null) {
|
|
684
|
+
opts = desc;
|
|
685
|
+
desc = null;
|
|
686
|
+
}
|
|
687
|
+
opts = opts || {};
|
|
688
|
+
const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
689
|
+
const cmd = this.createCommand(name);
|
|
690
|
+
if (desc) {
|
|
691
|
+
cmd.description(desc);
|
|
692
|
+
cmd._executableHandler = true;
|
|
693
|
+
}
|
|
694
|
+
if (opts.isDefault)
|
|
695
|
+
this._defaultCommandName = cmd._name;
|
|
696
|
+
cmd._hidden = !!(opts.noHelp || opts.hidden);
|
|
697
|
+
cmd._executableFile = opts.executableFile || null;
|
|
698
|
+
if (args)
|
|
699
|
+
cmd.arguments(args);
|
|
700
|
+
this._registerCommand(cmd);
|
|
701
|
+
cmd.parent = this;
|
|
702
|
+
cmd.copyInheritedSettings(this);
|
|
703
|
+
if (desc)
|
|
704
|
+
return this;
|
|
705
|
+
return cmd;
|
|
706
|
+
}
|
|
707
|
+
createCommand(name) {
|
|
708
|
+
return new Command(name);
|
|
709
|
+
}
|
|
710
|
+
createHelp() {
|
|
711
|
+
return Object.assign(new Help, this.configureHelp());
|
|
712
|
+
}
|
|
713
|
+
configureHelp(configuration) {
|
|
714
|
+
if (configuration === undefined)
|
|
715
|
+
return this._helpConfiguration;
|
|
716
|
+
this._helpConfiguration = configuration;
|
|
717
|
+
return this;
|
|
718
|
+
}
|
|
719
|
+
configureOutput(configuration) {
|
|
720
|
+
if (configuration === undefined)
|
|
721
|
+
return this._outputConfiguration;
|
|
722
|
+
Object.assign(this._outputConfiguration, configuration);
|
|
723
|
+
return this;
|
|
724
|
+
}
|
|
725
|
+
showHelpAfterError(displayHelp = true) {
|
|
726
|
+
if (typeof displayHelp !== "string")
|
|
727
|
+
displayHelp = !!displayHelp;
|
|
728
|
+
this._showHelpAfterError = displayHelp;
|
|
729
|
+
return this;
|
|
730
|
+
}
|
|
731
|
+
showSuggestionAfterError(displaySuggestion = true) {
|
|
732
|
+
this._showSuggestionAfterError = !!displaySuggestion;
|
|
733
|
+
return this;
|
|
734
|
+
}
|
|
735
|
+
addCommand(cmd, opts) {
|
|
736
|
+
if (!cmd._name) {
|
|
737
|
+
throw new Error(`Command passed to .addCommand() must have a name
|
|
738
|
+
- specify the name in Command constructor or using .name()`);
|
|
739
|
+
}
|
|
740
|
+
opts = opts || {};
|
|
741
|
+
if (opts.isDefault)
|
|
742
|
+
this._defaultCommandName = cmd._name;
|
|
743
|
+
if (opts.noHelp || opts.hidden)
|
|
744
|
+
cmd._hidden = true;
|
|
745
|
+
this._registerCommand(cmd);
|
|
746
|
+
cmd.parent = this;
|
|
747
|
+
cmd._checkForBrokenPassThrough();
|
|
748
|
+
return this;
|
|
749
|
+
}
|
|
750
|
+
createArgument(name, description) {
|
|
751
|
+
return new Argument(name, description);
|
|
752
|
+
}
|
|
753
|
+
argument(name, description, fn, defaultValue) {
|
|
754
|
+
const argument = this.createArgument(name, description);
|
|
755
|
+
if (typeof fn === "function") {
|
|
756
|
+
argument.default(defaultValue).argParser(fn);
|
|
757
|
+
} else {
|
|
758
|
+
argument.default(fn);
|
|
759
|
+
}
|
|
760
|
+
this.addArgument(argument);
|
|
761
|
+
return this;
|
|
762
|
+
}
|
|
763
|
+
arguments(names) {
|
|
764
|
+
names.trim().split(/ +/).forEach((detail) => {
|
|
765
|
+
this.argument(detail);
|
|
766
|
+
});
|
|
767
|
+
return this;
|
|
768
|
+
}
|
|
769
|
+
addArgument(argument) {
|
|
770
|
+
const previousArgument = this.registeredArguments.slice(-1)[0];
|
|
771
|
+
if (previousArgument && previousArgument.variadic) {
|
|
772
|
+
throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);
|
|
773
|
+
}
|
|
774
|
+
if (argument.required && argument.defaultValue !== undefined && argument.parseArg === undefined) {
|
|
775
|
+
throw new Error(`a default value for a required argument is never used: '${argument.name()}'`);
|
|
776
|
+
}
|
|
777
|
+
this.registeredArguments.push(argument);
|
|
778
|
+
return this;
|
|
779
|
+
}
|
|
780
|
+
helpCommand(enableOrNameAndArgs, description) {
|
|
781
|
+
if (typeof enableOrNameAndArgs === "boolean") {
|
|
782
|
+
this._addImplicitHelpCommand = enableOrNameAndArgs;
|
|
783
|
+
return this;
|
|
784
|
+
}
|
|
785
|
+
enableOrNameAndArgs = enableOrNameAndArgs ?? "help [command]";
|
|
786
|
+
const [, helpName, helpArgs] = enableOrNameAndArgs.match(/([^ ]+) *(.*)/);
|
|
787
|
+
const helpDescription = description ?? "display help for command";
|
|
788
|
+
const helpCommand = this.createCommand(helpName);
|
|
789
|
+
helpCommand.helpOption(false);
|
|
790
|
+
if (helpArgs)
|
|
791
|
+
helpCommand.arguments(helpArgs);
|
|
792
|
+
if (helpDescription)
|
|
793
|
+
helpCommand.description(helpDescription);
|
|
794
|
+
this._addImplicitHelpCommand = true;
|
|
795
|
+
this._helpCommand = helpCommand;
|
|
796
|
+
return this;
|
|
797
|
+
}
|
|
798
|
+
addHelpCommand(helpCommand, deprecatedDescription) {
|
|
799
|
+
if (typeof helpCommand !== "object") {
|
|
800
|
+
this.helpCommand(helpCommand, deprecatedDescription);
|
|
801
|
+
return this;
|
|
802
|
+
}
|
|
803
|
+
this._addImplicitHelpCommand = true;
|
|
804
|
+
this._helpCommand = helpCommand;
|
|
805
|
+
return this;
|
|
806
|
+
}
|
|
807
|
+
_getHelpCommand() {
|
|
808
|
+
const hasImplicitHelpCommand = this._addImplicitHelpCommand ?? (this.commands.length && !this._actionHandler && !this._findCommand("help"));
|
|
809
|
+
if (hasImplicitHelpCommand) {
|
|
810
|
+
if (this._helpCommand === undefined) {
|
|
811
|
+
this.helpCommand(undefined, undefined);
|
|
812
|
+
}
|
|
813
|
+
return this._helpCommand;
|
|
814
|
+
}
|
|
815
|
+
return null;
|
|
816
|
+
}
|
|
817
|
+
hook(event, listener) {
|
|
818
|
+
const allowedValues = ["preSubcommand", "preAction", "postAction"];
|
|
819
|
+
if (!allowedValues.includes(event)) {
|
|
820
|
+
throw new Error(`Unexpected value for event passed to hook : '${event}'.
|
|
821
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
822
|
+
}
|
|
823
|
+
if (this._lifeCycleHooks[event]) {
|
|
824
|
+
this._lifeCycleHooks[event].push(listener);
|
|
825
|
+
} else {
|
|
826
|
+
this._lifeCycleHooks[event] = [listener];
|
|
827
|
+
}
|
|
828
|
+
return this;
|
|
829
|
+
}
|
|
830
|
+
exitOverride(fn) {
|
|
831
|
+
if (fn) {
|
|
832
|
+
this._exitCallback = fn;
|
|
833
|
+
} else {
|
|
834
|
+
this._exitCallback = (err) => {
|
|
835
|
+
if (err.code !== "commander.executeSubCommandAsync") {
|
|
836
|
+
throw err;
|
|
837
|
+
} else {}
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
return this;
|
|
841
|
+
}
|
|
842
|
+
_exit(exitCode, code, message) {
|
|
843
|
+
if (this._exitCallback) {
|
|
844
|
+
this._exitCallback(new CommanderError(exitCode, code, message));
|
|
845
|
+
}
|
|
846
|
+
process2.exit(exitCode);
|
|
847
|
+
}
|
|
848
|
+
action(fn) {
|
|
849
|
+
const listener = (args) => {
|
|
850
|
+
const expectedArgsCount = this.registeredArguments.length;
|
|
851
|
+
const actionArgs = args.slice(0, expectedArgsCount);
|
|
852
|
+
if (this._storeOptionsAsProperties) {
|
|
853
|
+
actionArgs[expectedArgsCount] = this;
|
|
854
|
+
} else {
|
|
855
|
+
actionArgs[expectedArgsCount] = this.opts();
|
|
856
|
+
}
|
|
857
|
+
actionArgs.push(this);
|
|
858
|
+
return fn.apply(this, actionArgs);
|
|
859
|
+
};
|
|
860
|
+
this._actionHandler = listener;
|
|
861
|
+
return this;
|
|
862
|
+
}
|
|
863
|
+
createOption(flags, description) {
|
|
864
|
+
return new Option(flags, description);
|
|
865
|
+
}
|
|
866
|
+
_callParseArg(target, value, previous, invalidArgumentMessage) {
|
|
867
|
+
try {
|
|
868
|
+
return target.parseArg(value, previous);
|
|
869
|
+
} catch (err) {
|
|
870
|
+
if (err.code === "commander.invalidArgument") {
|
|
871
|
+
const message = `${invalidArgumentMessage} ${err.message}`;
|
|
872
|
+
this.error(message, { exitCode: err.exitCode, code: err.code });
|
|
873
|
+
}
|
|
874
|
+
throw err;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
_registerOption(option) {
|
|
878
|
+
const matchingOption = option.short && this._findOption(option.short) || option.long && this._findOption(option.long);
|
|
879
|
+
if (matchingOption) {
|
|
880
|
+
const matchingFlag = option.long && this._findOption(option.long) ? option.long : option.short;
|
|
881
|
+
throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
|
|
882
|
+
- already used by option '${matchingOption.flags}'`);
|
|
883
|
+
}
|
|
884
|
+
this.options.push(option);
|
|
885
|
+
}
|
|
886
|
+
_registerCommand(command) {
|
|
887
|
+
const knownBy = (cmd) => {
|
|
888
|
+
return [cmd.name()].concat(cmd.aliases());
|
|
889
|
+
};
|
|
890
|
+
const alreadyUsed = knownBy(command).find((name) => this._findCommand(name));
|
|
891
|
+
if (alreadyUsed) {
|
|
892
|
+
const existingCmd = knownBy(this._findCommand(alreadyUsed)).join("|");
|
|
893
|
+
const newCmd = knownBy(command).join("|");
|
|
894
|
+
throw new Error(`cannot add command '${newCmd}' as already have command '${existingCmd}'`);
|
|
895
|
+
}
|
|
896
|
+
this.commands.push(command);
|
|
897
|
+
}
|
|
898
|
+
addOption(option) {
|
|
899
|
+
this._registerOption(option);
|
|
900
|
+
const oname = option.name();
|
|
901
|
+
const name = option.attributeName();
|
|
902
|
+
if (option.negate) {
|
|
903
|
+
const positiveLongFlag = option.long.replace(/^--no-/, "--");
|
|
904
|
+
if (!this._findOption(positiveLongFlag)) {
|
|
905
|
+
this.setOptionValueWithSource(name, option.defaultValue === undefined ? true : option.defaultValue, "default");
|
|
906
|
+
}
|
|
907
|
+
} else if (option.defaultValue !== undefined) {
|
|
908
|
+
this.setOptionValueWithSource(name, option.defaultValue, "default");
|
|
909
|
+
}
|
|
910
|
+
const handleOptionValue = (val, invalidValueMessage, valueSource) => {
|
|
911
|
+
if (val == null && option.presetArg !== undefined) {
|
|
912
|
+
val = option.presetArg;
|
|
913
|
+
}
|
|
914
|
+
const oldValue = this.getOptionValue(name);
|
|
915
|
+
if (val !== null && option.parseArg) {
|
|
916
|
+
val = this._callParseArg(option, val, oldValue, invalidValueMessage);
|
|
917
|
+
} else if (val !== null && option.variadic) {
|
|
918
|
+
val = option._concatValue(val, oldValue);
|
|
919
|
+
}
|
|
920
|
+
if (val == null) {
|
|
921
|
+
if (option.negate) {
|
|
922
|
+
val = false;
|
|
923
|
+
} else if (option.isBoolean() || option.optional) {
|
|
924
|
+
val = true;
|
|
925
|
+
} else {
|
|
926
|
+
val = "";
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
this.setOptionValueWithSource(name, val, valueSource);
|
|
930
|
+
};
|
|
931
|
+
this.on("option:" + oname, (val) => {
|
|
932
|
+
const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
|
|
933
|
+
handleOptionValue(val, invalidValueMessage, "cli");
|
|
934
|
+
});
|
|
935
|
+
if (option.envVar) {
|
|
936
|
+
this.on("optionEnv:" + oname, (val) => {
|
|
937
|
+
const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
|
|
938
|
+
handleOptionValue(val, invalidValueMessage, "env");
|
|
939
|
+
});
|
|
940
|
+
}
|
|
941
|
+
return this;
|
|
942
|
+
}
|
|
943
|
+
_optionEx(config, flags, description, fn, defaultValue) {
|
|
944
|
+
if (typeof flags === "object" && flags instanceof Option) {
|
|
945
|
+
throw new Error("To add an Option object use addOption() instead of option() or requiredOption()");
|
|
946
|
+
}
|
|
947
|
+
const option = this.createOption(flags, description);
|
|
948
|
+
option.makeOptionMandatory(!!config.mandatory);
|
|
949
|
+
if (typeof fn === "function") {
|
|
950
|
+
option.default(defaultValue).argParser(fn);
|
|
951
|
+
} else if (fn instanceof RegExp) {
|
|
952
|
+
const regex = fn;
|
|
953
|
+
fn = (val, def) => {
|
|
954
|
+
const m = regex.exec(val);
|
|
955
|
+
return m ? m[0] : def;
|
|
956
|
+
};
|
|
957
|
+
option.default(defaultValue).argParser(fn);
|
|
958
|
+
} else {
|
|
959
|
+
option.default(fn);
|
|
960
|
+
}
|
|
961
|
+
return this.addOption(option);
|
|
962
|
+
}
|
|
963
|
+
option(flags, description, parseArg, defaultValue) {
|
|
964
|
+
return this._optionEx({}, flags, description, parseArg, defaultValue);
|
|
965
|
+
}
|
|
966
|
+
requiredOption(flags, description, parseArg, defaultValue) {
|
|
967
|
+
return this._optionEx({ mandatory: true }, flags, description, parseArg, defaultValue);
|
|
968
|
+
}
|
|
969
|
+
combineFlagAndOptionalValue(combine = true) {
|
|
970
|
+
this._combineFlagAndOptionalValue = !!combine;
|
|
971
|
+
return this;
|
|
972
|
+
}
|
|
973
|
+
allowUnknownOption(allowUnknown = true) {
|
|
974
|
+
this._allowUnknownOption = !!allowUnknown;
|
|
975
|
+
return this;
|
|
976
|
+
}
|
|
977
|
+
allowExcessArguments(allowExcess = true) {
|
|
978
|
+
this._allowExcessArguments = !!allowExcess;
|
|
979
|
+
return this;
|
|
980
|
+
}
|
|
981
|
+
enablePositionalOptions(positional = true) {
|
|
982
|
+
this._enablePositionalOptions = !!positional;
|
|
983
|
+
return this;
|
|
984
|
+
}
|
|
985
|
+
passThroughOptions(passThrough = true) {
|
|
986
|
+
this._passThroughOptions = !!passThrough;
|
|
987
|
+
this._checkForBrokenPassThrough();
|
|
988
|
+
return this;
|
|
989
|
+
}
|
|
990
|
+
_checkForBrokenPassThrough() {
|
|
991
|
+
if (this.parent && this._passThroughOptions && !this.parent._enablePositionalOptions) {
|
|
992
|
+
throw new Error(`passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`);
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
storeOptionsAsProperties(storeAsProperties = true) {
|
|
996
|
+
if (this.options.length) {
|
|
997
|
+
throw new Error("call .storeOptionsAsProperties() before adding options");
|
|
998
|
+
}
|
|
999
|
+
if (Object.keys(this._optionValues).length) {
|
|
1000
|
+
throw new Error("call .storeOptionsAsProperties() before setting option values");
|
|
1001
|
+
}
|
|
1002
|
+
this._storeOptionsAsProperties = !!storeAsProperties;
|
|
1003
|
+
return this;
|
|
1004
|
+
}
|
|
1005
|
+
getOptionValue(key) {
|
|
1006
|
+
if (this._storeOptionsAsProperties) {
|
|
1007
|
+
return this[key];
|
|
1008
|
+
}
|
|
1009
|
+
return this._optionValues[key];
|
|
1010
|
+
}
|
|
1011
|
+
setOptionValue(key, value) {
|
|
1012
|
+
return this.setOptionValueWithSource(key, value, undefined);
|
|
1013
|
+
}
|
|
1014
|
+
setOptionValueWithSource(key, value, source) {
|
|
1015
|
+
if (this._storeOptionsAsProperties) {
|
|
1016
|
+
this[key] = value;
|
|
1017
|
+
} else {
|
|
1018
|
+
this._optionValues[key] = value;
|
|
1019
|
+
}
|
|
1020
|
+
this._optionValueSources[key] = source;
|
|
1021
|
+
return this;
|
|
1022
|
+
}
|
|
1023
|
+
getOptionValueSource(key) {
|
|
1024
|
+
return this._optionValueSources[key];
|
|
1025
|
+
}
|
|
1026
|
+
getOptionValueSourceWithGlobals(key) {
|
|
1027
|
+
let source;
|
|
1028
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1029
|
+
if (cmd.getOptionValueSource(key) !== undefined) {
|
|
1030
|
+
source = cmd.getOptionValueSource(key);
|
|
1031
|
+
}
|
|
1032
|
+
});
|
|
1033
|
+
return source;
|
|
1034
|
+
}
|
|
1035
|
+
_prepareUserArgs(argv, parseOptions) {
|
|
1036
|
+
if (argv !== undefined && !Array.isArray(argv)) {
|
|
1037
|
+
throw new Error("first parameter to parse must be array or undefined");
|
|
1038
|
+
}
|
|
1039
|
+
parseOptions = parseOptions || {};
|
|
1040
|
+
if (argv === undefined && parseOptions.from === undefined) {
|
|
1041
|
+
if (process2.versions?.electron) {
|
|
1042
|
+
parseOptions.from = "electron";
|
|
1043
|
+
}
|
|
1044
|
+
const execArgv = process2.execArgv ?? [];
|
|
1045
|
+
if (execArgv.includes("-e") || execArgv.includes("--eval") || execArgv.includes("-p") || execArgv.includes("--print")) {
|
|
1046
|
+
parseOptions.from = "eval";
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
if (argv === undefined) {
|
|
1050
|
+
argv = process2.argv;
|
|
1051
|
+
}
|
|
1052
|
+
this.rawArgs = argv.slice();
|
|
1053
|
+
let userArgs;
|
|
1054
|
+
switch (parseOptions.from) {
|
|
1055
|
+
case undefined:
|
|
1056
|
+
case "node":
|
|
1057
|
+
this._scriptPath = argv[1];
|
|
1058
|
+
userArgs = argv.slice(2);
|
|
1059
|
+
break;
|
|
1060
|
+
case "electron":
|
|
1061
|
+
if (process2.defaultApp) {
|
|
1062
|
+
this._scriptPath = argv[1];
|
|
1063
|
+
userArgs = argv.slice(2);
|
|
1064
|
+
} else {
|
|
1065
|
+
userArgs = argv.slice(1);
|
|
1066
|
+
}
|
|
1067
|
+
break;
|
|
1068
|
+
case "user":
|
|
1069
|
+
userArgs = argv.slice(0);
|
|
1070
|
+
break;
|
|
1071
|
+
case "eval":
|
|
1072
|
+
userArgs = argv.slice(1);
|
|
1073
|
+
break;
|
|
1074
|
+
default:
|
|
1075
|
+
throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`);
|
|
1076
|
+
}
|
|
1077
|
+
if (!this._name && this._scriptPath)
|
|
1078
|
+
this.nameFromFilename(this._scriptPath);
|
|
1079
|
+
this._name = this._name || "program";
|
|
1080
|
+
return userArgs;
|
|
1081
|
+
}
|
|
1082
|
+
parse(argv, parseOptions) {
|
|
1083
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1084
|
+
this._parseCommand([], userArgs);
|
|
1085
|
+
return this;
|
|
1086
|
+
}
|
|
1087
|
+
async parseAsync(argv, parseOptions) {
|
|
1088
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1089
|
+
await this._parseCommand([], userArgs);
|
|
1090
|
+
return this;
|
|
1091
|
+
}
|
|
1092
|
+
_executeSubCommand(subcommand, args) {
|
|
1093
|
+
args = args.slice();
|
|
1094
|
+
let launchWithNode = false;
|
|
1095
|
+
const sourceExt = [".js", ".ts", ".tsx", ".mjs", ".cjs"];
|
|
1096
|
+
function findFile(baseDir, baseName) {
|
|
1097
|
+
const localBin = path.resolve(baseDir, baseName);
|
|
1098
|
+
if (fs.existsSync(localBin))
|
|
1099
|
+
return localBin;
|
|
1100
|
+
if (sourceExt.includes(path.extname(baseName)))
|
|
1101
|
+
return;
|
|
1102
|
+
const foundExt = sourceExt.find((ext) => fs.existsSync(`${localBin}${ext}`));
|
|
1103
|
+
if (foundExt)
|
|
1104
|
+
return `${localBin}${foundExt}`;
|
|
1105
|
+
return;
|
|
1106
|
+
}
|
|
1107
|
+
this._checkForMissingMandatoryOptions();
|
|
1108
|
+
this._checkForConflictingOptions();
|
|
1109
|
+
let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
|
|
1110
|
+
let executableDir = this._executableDir || "";
|
|
1111
|
+
if (this._scriptPath) {
|
|
1112
|
+
let resolvedScriptPath;
|
|
1113
|
+
try {
|
|
1114
|
+
resolvedScriptPath = fs.realpathSync(this._scriptPath);
|
|
1115
|
+
} catch (err) {
|
|
1116
|
+
resolvedScriptPath = this._scriptPath;
|
|
1117
|
+
}
|
|
1118
|
+
executableDir = path.resolve(path.dirname(resolvedScriptPath), executableDir);
|
|
1119
|
+
}
|
|
1120
|
+
if (executableDir) {
|
|
1121
|
+
let localFile = findFile(executableDir, executableFile);
|
|
1122
|
+
if (!localFile && !subcommand._executableFile && this._scriptPath) {
|
|
1123
|
+
const legacyName = path.basename(this._scriptPath, path.extname(this._scriptPath));
|
|
1124
|
+
if (legacyName !== this._name) {
|
|
1125
|
+
localFile = findFile(executableDir, `${legacyName}-${subcommand._name}`);
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
executableFile = localFile || executableFile;
|
|
1129
|
+
}
|
|
1130
|
+
launchWithNode = sourceExt.includes(path.extname(executableFile));
|
|
1131
|
+
let proc;
|
|
1132
|
+
if (process2.platform !== "win32") {
|
|
1133
|
+
if (launchWithNode) {
|
|
1134
|
+
args.unshift(executableFile);
|
|
1135
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1136
|
+
proc = childProcess.spawn(process2.argv[0], args, { stdio: "inherit" });
|
|
1137
|
+
} else {
|
|
1138
|
+
proc = childProcess.spawn(executableFile, args, { stdio: "inherit" });
|
|
1139
|
+
}
|
|
1140
|
+
} else {
|
|
1141
|
+
args.unshift(executableFile);
|
|
1142
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1143
|
+
proc = childProcess.spawn(process2.execPath, args, { stdio: "inherit" });
|
|
1144
|
+
}
|
|
1145
|
+
if (!proc.killed) {
|
|
1146
|
+
const signals = ["SIGUSR1", "SIGUSR2", "SIGTERM", "SIGINT", "SIGHUP"];
|
|
1147
|
+
signals.forEach((signal) => {
|
|
1148
|
+
process2.on(signal, () => {
|
|
1149
|
+
if (proc.killed === false && proc.exitCode === null) {
|
|
1150
|
+
proc.kill(signal);
|
|
1151
|
+
}
|
|
1152
|
+
});
|
|
1153
|
+
});
|
|
1154
|
+
}
|
|
1155
|
+
const exitCallback = this._exitCallback;
|
|
1156
|
+
proc.on("close", (code) => {
|
|
1157
|
+
code = code ?? 1;
|
|
1158
|
+
if (!exitCallback) {
|
|
1159
|
+
process2.exit(code);
|
|
1160
|
+
} else {
|
|
1161
|
+
exitCallback(new CommanderError(code, "commander.executeSubCommandAsync", "(close)"));
|
|
1162
|
+
}
|
|
1163
|
+
});
|
|
1164
|
+
proc.on("error", (err) => {
|
|
1165
|
+
if (err.code === "ENOENT") {
|
|
1166
|
+
const executableDirMessage = executableDir ? `searched for local subcommand relative to directory '${executableDir}'` : "no directory for search for local subcommand, use .executableDir() to supply a custom directory";
|
|
1167
|
+
const executableMissing = `'${executableFile}' does not exist
|
|
1168
|
+
- if '${subcommand._name}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
|
|
1169
|
+
- if the default executable name is not suitable, use the executableFile option to supply a custom name or path
|
|
1170
|
+
- ${executableDirMessage}`;
|
|
1171
|
+
throw new Error(executableMissing);
|
|
1172
|
+
} else if (err.code === "EACCES") {
|
|
1173
|
+
throw new Error(`'${executableFile}' not executable`);
|
|
1174
|
+
}
|
|
1175
|
+
if (!exitCallback) {
|
|
1176
|
+
process2.exit(1);
|
|
1177
|
+
} else {
|
|
1178
|
+
const wrappedError = new CommanderError(1, "commander.executeSubCommandAsync", "(error)");
|
|
1179
|
+
wrappedError.nestedError = err;
|
|
1180
|
+
exitCallback(wrappedError);
|
|
1181
|
+
}
|
|
1182
|
+
});
|
|
1183
|
+
this.runningCommand = proc;
|
|
1184
|
+
}
|
|
1185
|
+
_dispatchSubcommand(commandName, operands, unknown) {
|
|
1186
|
+
const subCommand = this._findCommand(commandName);
|
|
1187
|
+
if (!subCommand)
|
|
1188
|
+
this.help({ error: true });
|
|
1189
|
+
let promiseChain;
|
|
1190
|
+
promiseChain = this._chainOrCallSubCommandHook(promiseChain, subCommand, "preSubcommand");
|
|
1191
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1192
|
+
if (subCommand._executableHandler) {
|
|
1193
|
+
this._executeSubCommand(subCommand, operands.concat(unknown));
|
|
1194
|
+
} else {
|
|
1195
|
+
return subCommand._parseCommand(operands, unknown);
|
|
1196
|
+
}
|
|
1197
|
+
});
|
|
1198
|
+
return promiseChain;
|
|
1199
|
+
}
|
|
1200
|
+
_dispatchHelpCommand(subcommandName) {
|
|
1201
|
+
if (!subcommandName) {
|
|
1202
|
+
this.help();
|
|
1203
|
+
}
|
|
1204
|
+
const subCommand = this._findCommand(subcommandName);
|
|
1205
|
+
if (subCommand && !subCommand._executableHandler) {
|
|
1206
|
+
subCommand.help();
|
|
1207
|
+
}
|
|
1208
|
+
return this._dispatchSubcommand(subcommandName, [], [this._getHelpOption()?.long ?? this._getHelpOption()?.short ?? "--help"]);
|
|
1209
|
+
}
|
|
1210
|
+
_checkNumberOfArguments() {
|
|
1211
|
+
this.registeredArguments.forEach((arg, i) => {
|
|
1212
|
+
if (arg.required && this.args[i] == null) {
|
|
1213
|
+
this.missingArgument(arg.name());
|
|
1214
|
+
}
|
|
1215
|
+
});
|
|
1216
|
+
if (this.registeredArguments.length > 0 && this.registeredArguments[this.registeredArguments.length - 1].variadic) {
|
|
1217
|
+
return;
|
|
1218
|
+
}
|
|
1219
|
+
if (this.args.length > this.registeredArguments.length) {
|
|
1220
|
+
this._excessArguments(this.args);
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
_processArguments() {
|
|
1224
|
+
const myParseArg = (argument, value, previous) => {
|
|
1225
|
+
let parsedValue = value;
|
|
1226
|
+
if (value !== null && argument.parseArg) {
|
|
1227
|
+
const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;
|
|
1228
|
+
parsedValue = this._callParseArg(argument, value, previous, invalidValueMessage);
|
|
1229
|
+
}
|
|
1230
|
+
return parsedValue;
|
|
1231
|
+
};
|
|
1232
|
+
this._checkNumberOfArguments();
|
|
1233
|
+
const processedArgs = [];
|
|
1234
|
+
this.registeredArguments.forEach((declaredArg, index) => {
|
|
1235
|
+
let value = declaredArg.defaultValue;
|
|
1236
|
+
if (declaredArg.variadic) {
|
|
1237
|
+
if (index < this.args.length) {
|
|
1238
|
+
value = this.args.slice(index);
|
|
1239
|
+
if (declaredArg.parseArg) {
|
|
1240
|
+
value = value.reduce((processed, v) => {
|
|
1241
|
+
return myParseArg(declaredArg, v, processed);
|
|
1242
|
+
}, declaredArg.defaultValue);
|
|
1243
|
+
}
|
|
1244
|
+
} else if (value === undefined) {
|
|
1245
|
+
value = [];
|
|
1246
|
+
}
|
|
1247
|
+
} else if (index < this.args.length) {
|
|
1248
|
+
value = this.args[index];
|
|
1249
|
+
if (declaredArg.parseArg) {
|
|
1250
|
+
value = myParseArg(declaredArg, value, declaredArg.defaultValue);
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
processedArgs[index] = value;
|
|
1254
|
+
});
|
|
1255
|
+
this.processedArgs = processedArgs;
|
|
1256
|
+
}
|
|
1257
|
+
_chainOrCall(promise, fn) {
|
|
1258
|
+
if (promise && promise.then && typeof promise.then === "function") {
|
|
1259
|
+
return promise.then(() => fn());
|
|
1260
|
+
}
|
|
1261
|
+
return fn();
|
|
1262
|
+
}
|
|
1263
|
+
_chainOrCallHooks(promise, event) {
|
|
1264
|
+
let result = promise;
|
|
1265
|
+
const hooks = [];
|
|
1266
|
+
this._getCommandAndAncestors().reverse().filter((cmd) => cmd._lifeCycleHooks[event] !== undefined).forEach((hookedCommand) => {
|
|
1267
|
+
hookedCommand._lifeCycleHooks[event].forEach((callback) => {
|
|
1268
|
+
hooks.push({ hookedCommand, callback });
|
|
1269
|
+
});
|
|
1270
|
+
});
|
|
1271
|
+
if (event === "postAction") {
|
|
1272
|
+
hooks.reverse();
|
|
1273
|
+
}
|
|
1274
|
+
hooks.forEach((hookDetail) => {
|
|
1275
|
+
result = this._chainOrCall(result, () => {
|
|
1276
|
+
return hookDetail.callback(hookDetail.hookedCommand, this);
|
|
1277
|
+
});
|
|
1278
|
+
});
|
|
1279
|
+
return result;
|
|
1280
|
+
}
|
|
1281
|
+
_chainOrCallSubCommandHook(promise, subCommand, event) {
|
|
1282
|
+
let result = promise;
|
|
1283
|
+
if (this._lifeCycleHooks[event] !== undefined) {
|
|
1284
|
+
this._lifeCycleHooks[event].forEach((hook) => {
|
|
1285
|
+
result = this._chainOrCall(result, () => {
|
|
1286
|
+
return hook(this, subCommand);
|
|
1287
|
+
});
|
|
1288
|
+
});
|
|
1289
|
+
}
|
|
1290
|
+
return result;
|
|
1291
|
+
}
|
|
1292
|
+
_parseCommand(operands, unknown) {
|
|
1293
|
+
const parsed = this.parseOptions(unknown);
|
|
1294
|
+
this._parseOptionsEnv();
|
|
1295
|
+
this._parseOptionsImplied();
|
|
1296
|
+
operands = operands.concat(parsed.operands);
|
|
1297
|
+
unknown = parsed.unknown;
|
|
1298
|
+
this.args = operands.concat(unknown);
|
|
1299
|
+
if (operands && this._findCommand(operands[0])) {
|
|
1300
|
+
return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
|
|
1301
|
+
}
|
|
1302
|
+
if (this._getHelpCommand() && operands[0] === this._getHelpCommand().name()) {
|
|
1303
|
+
return this._dispatchHelpCommand(operands[1]);
|
|
1304
|
+
}
|
|
1305
|
+
if (this._defaultCommandName) {
|
|
1306
|
+
this._outputHelpIfRequested(unknown);
|
|
1307
|
+
return this._dispatchSubcommand(this._defaultCommandName, operands, unknown);
|
|
1308
|
+
}
|
|
1309
|
+
if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
|
|
1310
|
+
this.help({ error: true });
|
|
1311
|
+
}
|
|
1312
|
+
this._outputHelpIfRequested(parsed.unknown);
|
|
1313
|
+
this._checkForMissingMandatoryOptions();
|
|
1314
|
+
this._checkForConflictingOptions();
|
|
1315
|
+
const checkForUnknownOptions = () => {
|
|
1316
|
+
if (parsed.unknown.length > 0) {
|
|
1317
|
+
this.unknownOption(parsed.unknown[0]);
|
|
1318
|
+
}
|
|
1319
|
+
};
|
|
1320
|
+
const commandEvent = `command:${this.name()}`;
|
|
1321
|
+
if (this._actionHandler) {
|
|
1322
|
+
checkForUnknownOptions();
|
|
1323
|
+
this._processArguments();
|
|
1324
|
+
let promiseChain;
|
|
1325
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "preAction");
|
|
1326
|
+
promiseChain = this._chainOrCall(promiseChain, () => this._actionHandler(this.processedArgs));
|
|
1327
|
+
if (this.parent) {
|
|
1328
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1329
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1330
|
+
});
|
|
1331
|
+
}
|
|
1332
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "postAction");
|
|
1333
|
+
return promiseChain;
|
|
1334
|
+
}
|
|
1335
|
+
if (this.parent && this.parent.listenerCount(commandEvent)) {
|
|
1336
|
+
checkForUnknownOptions();
|
|
1337
|
+
this._processArguments();
|
|
1338
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1339
|
+
} else if (operands.length) {
|
|
1340
|
+
if (this._findCommand("*")) {
|
|
1341
|
+
return this._dispatchSubcommand("*", operands, unknown);
|
|
1342
|
+
}
|
|
1343
|
+
if (this.listenerCount("command:*")) {
|
|
1344
|
+
this.emit("command:*", operands, unknown);
|
|
1345
|
+
} else if (this.commands.length) {
|
|
1346
|
+
this.unknownCommand();
|
|
1347
|
+
} else {
|
|
1348
|
+
checkForUnknownOptions();
|
|
1349
|
+
this._processArguments();
|
|
1350
|
+
}
|
|
1351
|
+
} else if (this.commands.length) {
|
|
1352
|
+
checkForUnknownOptions();
|
|
1353
|
+
this.help({ error: true });
|
|
1354
|
+
} else {
|
|
1355
|
+
checkForUnknownOptions();
|
|
1356
|
+
this._processArguments();
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
_findCommand(name) {
|
|
1360
|
+
if (!name)
|
|
1361
|
+
return;
|
|
1362
|
+
return this.commands.find((cmd) => cmd._name === name || cmd._aliases.includes(name));
|
|
1363
|
+
}
|
|
1364
|
+
_findOption(arg) {
|
|
1365
|
+
return this.options.find((option) => option.is(arg));
|
|
1366
|
+
}
|
|
1367
|
+
_checkForMissingMandatoryOptions() {
|
|
1368
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1369
|
+
cmd.options.forEach((anOption) => {
|
|
1370
|
+
if (anOption.mandatory && cmd.getOptionValue(anOption.attributeName()) === undefined) {
|
|
1371
|
+
cmd.missingMandatoryOptionValue(anOption);
|
|
1372
|
+
}
|
|
1373
|
+
});
|
|
1374
|
+
});
|
|
1375
|
+
}
|
|
1376
|
+
_checkForConflictingLocalOptions() {
|
|
1377
|
+
const definedNonDefaultOptions = this.options.filter((option) => {
|
|
1378
|
+
const optionKey = option.attributeName();
|
|
1379
|
+
if (this.getOptionValue(optionKey) === undefined) {
|
|
1380
|
+
return false;
|
|
1381
|
+
}
|
|
1382
|
+
return this.getOptionValueSource(optionKey) !== "default";
|
|
1383
|
+
});
|
|
1384
|
+
const optionsWithConflicting = definedNonDefaultOptions.filter((option) => option.conflictsWith.length > 0);
|
|
1385
|
+
optionsWithConflicting.forEach((option) => {
|
|
1386
|
+
const conflictingAndDefined = definedNonDefaultOptions.find((defined) => option.conflictsWith.includes(defined.attributeName()));
|
|
1387
|
+
if (conflictingAndDefined) {
|
|
1388
|
+
this._conflictingOption(option, conflictingAndDefined);
|
|
1389
|
+
}
|
|
1390
|
+
});
|
|
1391
|
+
}
|
|
1392
|
+
_checkForConflictingOptions() {
|
|
1393
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1394
|
+
cmd._checkForConflictingLocalOptions();
|
|
1395
|
+
});
|
|
1396
|
+
}
|
|
1397
|
+
parseOptions(argv) {
|
|
1398
|
+
const operands = [];
|
|
1399
|
+
const unknown = [];
|
|
1400
|
+
let dest = operands;
|
|
1401
|
+
const args = argv.slice();
|
|
1402
|
+
function maybeOption(arg) {
|
|
1403
|
+
return arg.length > 1 && arg[0] === "-";
|
|
1404
|
+
}
|
|
1405
|
+
let activeVariadicOption = null;
|
|
1406
|
+
while (args.length) {
|
|
1407
|
+
const arg = args.shift();
|
|
1408
|
+
if (arg === "--") {
|
|
1409
|
+
if (dest === unknown)
|
|
1410
|
+
dest.push(arg);
|
|
1411
|
+
dest.push(...args);
|
|
1412
|
+
break;
|
|
1413
|
+
}
|
|
1414
|
+
if (activeVariadicOption && !maybeOption(arg)) {
|
|
1415
|
+
this.emit(`option:${activeVariadicOption.name()}`, arg);
|
|
1416
|
+
continue;
|
|
1417
|
+
}
|
|
1418
|
+
activeVariadicOption = null;
|
|
1419
|
+
if (maybeOption(arg)) {
|
|
1420
|
+
const option = this._findOption(arg);
|
|
1421
|
+
if (option) {
|
|
1422
|
+
if (option.required) {
|
|
1423
|
+
const value = args.shift();
|
|
1424
|
+
if (value === undefined)
|
|
1425
|
+
this.optionMissingArgument(option);
|
|
1426
|
+
this.emit(`option:${option.name()}`, value);
|
|
1427
|
+
} else if (option.optional) {
|
|
1428
|
+
let value = null;
|
|
1429
|
+
if (args.length > 0 && !maybeOption(args[0])) {
|
|
1430
|
+
value = args.shift();
|
|
1431
|
+
}
|
|
1432
|
+
this.emit(`option:${option.name()}`, value);
|
|
1433
|
+
} else {
|
|
1434
|
+
this.emit(`option:${option.name()}`);
|
|
1435
|
+
}
|
|
1436
|
+
activeVariadicOption = option.variadic ? option : null;
|
|
1437
|
+
continue;
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
if (arg.length > 2 && arg[0] === "-" && arg[1] !== "-") {
|
|
1441
|
+
const option = this._findOption(`-${arg[1]}`);
|
|
1442
|
+
if (option) {
|
|
1443
|
+
if (option.required || option.optional && this._combineFlagAndOptionalValue) {
|
|
1444
|
+
this.emit(`option:${option.name()}`, arg.slice(2));
|
|
1445
|
+
} else {
|
|
1446
|
+
this.emit(`option:${option.name()}`);
|
|
1447
|
+
args.unshift(`-${arg.slice(2)}`);
|
|
1448
|
+
}
|
|
1449
|
+
continue;
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
if (/^--[^=]+=/.test(arg)) {
|
|
1453
|
+
const index = arg.indexOf("=");
|
|
1454
|
+
const option = this._findOption(arg.slice(0, index));
|
|
1455
|
+
if (option && (option.required || option.optional)) {
|
|
1456
|
+
this.emit(`option:${option.name()}`, arg.slice(index + 1));
|
|
1457
|
+
continue;
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
if (maybeOption(arg)) {
|
|
1461
|
+
dest = unknown;
|
|
1462
|
+
}
|
|
1463
|
+
if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
|
|
1464
|
+
if (this._findCommand(arg)) {
|
|
1465
|
+
operands.push(arg);
|
|
1466
|
+
if (args.length > 0)
|
|
1467
|
+
unknown.push(...args);
|
|
1468
|
+
break;
|
|
1469
|
+
} else if (this._getHelpCommand() && arg === this._getHelpCommand().name()) {
|
|
1470
|
+
operands.push(arg);
|
|
1471
|
+
if (args.length > 0)
|
|
1472
|
+
operands.push(...args);
|
|
1473
|
+
break;
|
|
1474
|
+
} else if (this._defaultCommandName) {
|
|
1475
|
+
unknown.push(arg);
|
|
1476
|
+
if (args.length > 0)
|
|
1477
|
+
unknown.push(...args);
|
|
1478
|
+
break;
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
if (this._passThroughOptions) {
|
|
1482
|
+
dest.push(arg);
|
|
1483
|
+
if (args.length > 0)
|
|
1484
|
+
dest.push(...args);
|
|
1485
|
+
break;
|
|
1486
|
+
}
|
|
1487
|
+
dest.push(arg);
|
|
1488
|
+
}
|
|
1489
|
+
return { operands, unknown };
|
|
1490
|
+
}
|
|
1491
|
+
opts() {
|
|
1492
|
+
if (this._storeOptionsAsProperties) {
|
|
1493
|
+
const result = {};
|
|
1494
|
+
const len = this.options.length;
|
|
1495
|
+
for (let i = 0;i < len; i++) {
|
|
1496
|
+
const key = this.options[i].attributeName();
|
|
1497
|
+
result[key] = key === this._versionOptionName ? this._version : this[key];
|
|
1498
|
+
}
|
|
1499
|
+
return result;
|
|
1500
|
+
}
|
|
1501
|
+
return this._optionValues;
|
|
1502
|
+
}
|
|
1503
|
+
optsWithGlobals() {
|
|
1504
|
+
return this._getCommandAndAncestors().reduce((combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()), {});
|
|
1505
|
+
}
|
|
1506
|
+
error(message, errorOptions) {
|
|
1507
|
+
this._outputConfiguration.outputError(`${message}
|
|
1508
|
+
`, this._outputConfiguration.writeErr);
|
|
1509
|
+
if (typeof this._showHelpAfterError === "string") {
|
|
1510
|
+
this._outputConfiguration.writeErr(`${this._showHelpAfterError}
|
|
1511
|
+
`);
|
|
1512
|
+
} else if (this._showHelpAfterError) {
|
|
1513
|
+
this._outputConfiguration.writeErr(`
|
|
1514
|
+
`);
|
|
1515
|
+
this.outputHelp({ error: true });
|
|
1516
|
+
}
|
|
1517
|
+
const config = errorOptions || {};
|
|
1518
|
+
const exitCode = config.exitCode || 1;
|
|
1519
|
+
const code = config.code || "commander.error";
|
|
1520
|
+
this._exit(exitCode, code, message);
|
|
1521
|
+
}
|
|
1522
|
+
_parseOptionsEnv() {
|
|
1523
|
+
this.options.forEach((option) => {
|
|
1524
|
+
if (option.envVar && option.envVar in process2.env) {
|
|
1525
|
+
const optionKey = option.attributeName();
|
|
1526
|
+
if (this.getOptionValue(optionKey) === undefined || ["default", "config", "env"].includes(this.getOptionValueSource(optionKey))) {
|
|
1527
|
+
if (option.required || option.optional) {
|
|
1528
|
+
this.emit(`optionEnv:${option.name()}`, process2.env[option.envVar]);
|
|
1529
|
+
} else {
|
|
1530
|
+
this.emit(`optionEnv:${option.name()}`);
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
});
|
|
1535
|
+
}
|
|
1536
|
+
_parseOptionsImplied() {
|
|
1537
|
+
const dualHelper = new DualOptions(this.options);
|
|
1538
|
+
const hasCustomOptionValue = (optionKey) => {
|
|
1539
|
+
return this.getOptionValue(optionKey) !== undefined && !["default", "implied"].includes(this.getOptionValueSource(optionKey));
|
|
1540
|
+
};
|
|
1541
|
+
this.options.filter((option) => option.implied !== undefined && hasCustomOptionValue(option.attributeName()) && dualHelper.valueFromOption(this.getOptionValue(option.attributeName()), option)).forEach((option) => {
|
|
1542
|
+
Object.keys(option.implied).filter((impliedKey) => !hasCustomOptionValue(impliedKey)).forEach((impliedKey) => {
|
|
1543
|
+
this.setOptionValueWithSource(impliedKey, option.implied[impliedKey], "implied");
|
|
1544
|
+
});
|
|
1545
|
+
});
|
|
1546
|
+
}
|
|
1547
|
+
missingArgument(name) {
|
|
1548
|
+
const message = `error: missing required argument '${name}'`;
|
|
1549
|
+
this.error(message, { code: "commander.missingArgument" });
|
|
1550
|
+
}
|
|
1551
|
+
optionMissingArgument(option) {
|
|
1552
|
+
const message = `error: option '${option.flags}' argument missing`;
|
|
1553
|
+
this.error(message, { code: "commander.optionMissingArgument" });
|
|
1554
|
+
}
|
|
1555
|
+
missingMandatoryOptionValue(option) {
|
|
1556
|
+
const message = `error: required option '${option.flags}' not specified`;
|
|
1557
|
+
this.error(message, { code: "commander.missingMandatoryOptionValue" });
|
|
1558
|
+
}
|
|
1559
|
+
_conflictingOption(option, conflictingOption) {
|
|
1560
|
+
const findBestOptionFromValue = (option2) => {
|
|
1561
|
+
const optionKey = option2.attributeName();
|
|
1562
|
+
const optionValue = this.getOptionValue(optionKey);
|
|
1563
|
+
const negativeOption = this.options.find((target) => target.negate && optionKey === target.attributeName());
|
|
1564
|
+
const positiveOption = this.options.find((target) => !target.negate && optionKey === target.attributeName());
|
|
1565
|
+
if (negativeOption && (negativeOption.presetArg === undefined && optionValue === false || negativeOption.presetArg !== undefined && optionValue === negativeOption.presetArg)) {
|
|
1566
|
+
return negativeOption;
|
|
1567
|
+
}
|
|
1568
|
+
return positiveOption || option2;
|
|
1569
|
+
};
|
|
1570
|
+
const getErrorMessage = (option2) => {
|
|
1571
|
+
const bestOption = findBestOptionFromValue(option2);
|
|
1572
|
+
const optionKey = bestOption.attributeName();
|
|
1573
|
+
const source = this.getOptionValueSource(optionKey);
|
|
1574
|
+
if (source === "env") {
|
|
1575
|
+
return `environment variable '${bestOption.envVar}'`;
|
|
1576
|
+
}
|
|
1577
|
+
return `option '${bestOption.flags}'`;
|
|
1578
|
+
};
|
|
1579
|
+
const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
|
|
1580
|
+
this.error(message, { code: "commander.conflictingOption" });
|
|
1581
|
+
}
|
|
1582
|
+
unknownOption(flag) {
|
|
1583
|
+
if (this._allowUnknownOption)
|
|
1584
|
+
return;
|
|
1585
|
+
let suggestion = "";
|
|
1586
|
+
if (flag.startsWith("--") && this._showSuggestionAfterError) {
|
|
1587
|
+
let candidateFlags = [];
|
|
1588
|
+
let command = this;
|
|
1589
|
+
do {
|
|
1590
|
+
const moreFlags = command.createHelp().visibleOptions(command).filter((option) => option.long).map((option) => option.long);
|
|
1591
|
+
candidateFlags = candidateFlags.concat(moreFlags);
|
|
1592
|
+
command = command.parent;
|
|
1593
|
+
} while (command && !command._enablePositionalOptions);
|
|
1594
|
+
suggestion = suggestSimilar(flag, candidateFlags);
|
|
1595
|
+
}
|
|
1596
|
+
const message = `error: unknown option '${flag}'${suggestion}`;
|
|
1597
|
+
this.error(message, { code: "commander.unknownOption" });
|
|
1598
|
+
}
|
|
1599
|
+
_excessArguments(receivedArgs) {
|
|
1600
|
+
if (this._allowExcessArguments)
|
|
1601
|
+
return;
|
|
1602
|
+
const expected = this.registeredArguments.length;
|
|
1603
|
+
const s = expected === 1 ? "" : "s";
|
|
1604
|
+
const forSubcommand = this.parent ? ` for '${this.name()}'` : "";
|
|
1605
|
+
const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
|
|
1606
|
+
this.error(message, { code: "commander.excessArguments" });
|
|
1607
|
+
}
|
|
1608
|
+
unknownCommand() {
|
|
1609
|
+
const unknownName = this.args[0];
|
|
1610
|
+
let suggestion = "";
|
|
1611
|
+
if (this._showSuggestionAfterError) {
|
|
1612
|
+
const candidateNames = [];
|
|
1613
|
+
this.createHelp().visibleCommands(this).forEach((command) => {
|
|
1614
|
+
candidateNames.push(command.name());
|
|
1615
|
+
if (command.alias())
|
|
1616
|
+
candidateNames.push(command.alias());
|
|
1617
|
+
});
|
|
1618
|
+
suggestion = suggestSimilar(unknownName, candidateNames);
|
|
1619
|
+
}
|
|
1620
|
+
const message = `error: unknown command '${unknownName}'${suggestion}`;
|
|
1621
|
+
this.error(message, { code: "commander.unknownCommand" });
|
|
1622
|
+
}
|
|
1623
|
+
version(str, flags, description) {
|
|
1624
|
+
if (str === undefined)
|
|
1625
|
+
return this._version;
|
|
1626
|
+
this._version = str;
|
|
1627
|
+
flags = flags || "-V, --version";
|
|
1628
|
+
description = description || "output the version number";
|
|
1629
|
+
const versionOption = this.createOption(flags, description);
|
|
1630
|
+
this._versionOptionName = versionOption.attributeName();
|
|
1631
|
+
this._registerOption(versionOption);
|
|
1632
|
+
this.on("option:" + versionOption.name(), () => {
|
|
1633
|
+
this._outputConfiguration.writeOut(`${str}
|
|
1634
|
+
`);
|
|
1635
|
+
this._exit(0, "commander.version", str);
|
|
1636
|
+
});
|
|
1637
|
+
return this;
|
|
1638
|
+
}
|
|
1639
|
+
description(str, argsDescription) {
|
|
1640
|
+
if (str === undefined && argsDescription === undefined)
|
|
1641
|
+
return this._description;
|
|
1642
|
+
this._description = str;
|
|
1643
|
+
if (argsDescription) {
|
|
1644
|
+
this._argsDescription = argsDescription;
|
|
1645
|
+
}
|
|
1646
|
+
return this;
|
|
1647
|
+
}
|
|
1648
|
+
summary(str) {
|
|
1649
|
+
if (str === undefined)
|
|
1650
|
+
return this._summary;
|
|
1651
|
+
this._summary = str;
|
|
1652
|
+
return this;
|
|
1653
|
+
}
|
|
1654
|
+
alias(alias) {
|
|
1655
|
+
if (alias === undefined)
|
|
1656
|
+
return this._aliases[0];
|
|
1657
|
+
let command = this;
|
|
1658
|
+
if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
|
|
1659
|
+
command = this.commands[this.commands.length - 1];
|
|
1660
|
+
}
|
|
1661
|
+
if (alias === command._name)
|
|
1662
|
+
throw new Error("Command alias can't be the same as its name");
|
|
1663
|
+
const matchingCommand = this.parent?._findCommand(alias);
|
|
1664
|
+
if (matchingCommand) {
|
|
1665
|
+
const existingCmd = [matchingCommand.name()].concat(matchingCommand.aliases()).join("|");
|
|
1666
|
+
throw new Error(`cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`);
|
|
1667
|
+
}
|
|
1668
|
+
command._aliases.push(alias);
|
|
1669
|
+
return this;
|
|
1670
|
+
}
|
|
1671
|
+
aliases(aliases) {
|
|
1672
|
+
if (aliases === undefined)
|
|
1673
|
+
return this._aliases;
|
|
1674
|
+
aliases.forEach((alias) => this.alias(alias));
|
|
1675
|
+
return this;
|
|
1676
|
+
}
|
|
1677
|
+
usage(str) {
|
|
1678
|
+
if (str === undefined) {
|
|
1679
|
+
if (this._usage)
|
|
1680
|
+
return this._usage;
|
|
1681
|
+
const args = this.registeredArguments.map((arg) => {
|
|
1682
|
+
return humanReadableArgName(arg);
|
|
1683
|
+
});
|
|
1684
|
+
return [].concat(this.options.length || this._helpOption !== null ? "[options]" : [], this.commands.length ? "[command]" : [], this.registeredArguments.length ? args : []).join(" ");
|
|
1685
|
+
}
|
|
1686
|
+
this._usage = str;
|
|
1687
|
+
return this;
|
|
1688
|
+
}
|
|
1689
|
+
name(str) {
|
|
1690
|
+
if (str === undefined)
|
|
1691
|
+
return this._name;
|
|
1692
|
+
this._name = str;
|
|
1693
|
+
return this;
|
|
1694
|
+
}
|
|
1695
|
+
nameFromFilename(filename) {
|
|
1696
|
+
this._name = path.basename(filename, path.extname(filename));
|
|
1697
|
+
return this;
|
|
1698
|
+
}
|
|
1699
|
+
executableDir(path2) {
|
|
1700
|
+
if (path2 === undefined)
|
|
1701
|
+
return this._executableDir;
|
|
1702
|
+
this._executableDir = path2;
|
|
1703
|
+
return this;
|
|
1704
|
+
}
|
|
1705
|
+
helpInformation(contextOptions) {
|
|
1706
|
+
const helper = this.createHelp();
|
|
1707
|
+
if (helper.helpWidth === undefined) {
|
|
1708
|
+
helper.helpWidth = contextOptions && contextOptions.error ? this._outputConfiguration.getErrHelpWidth() : this._outputConfiguration.getOutHelpWidth();
|
|
1709
|
+
}
|
|
1710
|
+
return helper.formatHelp(this, helper);
|
|
1711
|
+
}
|
|
1712
|
+
_getHelpContext(contextOptions) {
|
|
1713
|
+
contextOptions = contextOptions || {};
|
|
1714
|
+
const context = { error: !!contextOptions.error };
|
|
1715
|
+
let write;
|
|
1716
|
+
if (context.error) {
|
|
1717
|
+
write = (arg) => this._outputConfiguration.writeErr(arg);
|
|
1718
|
+
} else {
|
|
1719
|
+
write = (arg) => this._outputConfiguration.writeOut(arg);
|
|
1720
|
+
}
|
|
1721
|
+
context.write = contextOptions.write || write;
|
|
1722
|
+
context.command = this;
|
|
1723
|
+
return context;
|
|
1724
|
+
}
|
|
1725
|
+
outputHelp(contextOptions) {
|
|
1726
|
+
let deprecatedCallback;
|
|
1727
|
+
if (typeof contextOptions === "function") {
|
|
1728
|
+
deprecatedCallback = contextOptions;
|
|
1729
|
+
contextOptions = undefined;
|
|
1730
|
+
}
|
|
1731
|
+
const context = this._getHelpContext(contextOptions);
|
|
1732
|
+
this._getCommandAndAncestors().reverse().forEach((command) => command.emit("beforeAllHelp", context));
|
|
1733
|
+
this.emit("beforeHelp", context);
|
|
1734
|
+
let helpInformation = this.helpInformation(context);
|
|
1735
|
+
if (deprecatedCallback) {
|
|
1736
|
+
helpInformation = deprecatedCallback(helpInformation);
|
|
1737
|
+
if (typeof helpInformation !== "string" && !Buffer.isBuffer(helpInformation)) {
|
|
1738
|
+
throw new Error("outputHelp callback must return a string or a Buffer");
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
context.write(helpInformation);
|
|
1742
|
+
if (this._getHelpOption()?.long) {
|
|
1743
|
+
this.emit(this._getHelpOption().long);
|
|
1744
|
+
}
|
|
1745
|
+
this.emit("afterHelp", context);
|
|
1746
|
+
this._getCommandAndAncestors().forEach((command) => command.emit("afterAllHelp", context));
|
|
1747
|
+
}
|
|
1748
|
+
helpOption(flags, description) {
|
|
1749
|
+
if (typeof flags === "boolean") {
|
|
1750
|
+
if (flags) {
|
|
1751
|
+
this._helpOption = this._helpOption ?? undefined;
|
|
1752
|
+
} else {
|
|
1753
|
+
this._helpOption = null;
|
|
1754
|
+
}
|
|
1755
|
+
return this;
|
|
1756
|
+
}
|
|
1757
|
+
flags = flags ?? "-h, --help";
|
|
1758
|
+
description = description ?? "display help for command";
|
|
1759
|
+
this._helpOption = this.createOption(flags, description);
|
|
1760
|
+
return this;
|
|
1761
|
+
}
|
|
1762
|
+
_getHelpOption() {
|
|
1763
|
+
if (this._helpOption === undefined) {
|
|
1764
|
+
this.helpOption(undefined, undefined);
|
|
1765
|
+
}
|
|
1766
|
+
return this._helpOption;
|
|
1767
|
+
}
|
|
1768
|
+
addHelpOption(option) {
|
|
1769
|
+
this._helpOption = option;
|
|
1770
|
+
return this;
|
|
1771
|
+
}
|
|
1772
|
+
help(contextOptions) {
|
|
1773
|
+
this.outputHelp(contextOptions);
|
|
1774
|
+
let exitCode = process2.exitCode || 0;
|
|
1775
|
+
if (exitCode === 0 && contextOptions && typeof contextOptions !== "function" && contextOptions.error) {
|
|
1776
|
+
exitCode = 1;
|
|
1777
|
+
}
|
|
1778
|
+
this._exit(exitCode, "commander.help", "(outputHelp)");
|
|
1779
|
+
}
|
|
1780
|
+
addHelpText(position, text) {
|
|
1781
|
+
const allowedValues = ["beforeAll", "before", "after", "afterAll"];
|
|
1782
|
+
if (!allowedValues.includes(position)) {
|
|
1783
|
+
throw new Error(`Unexpected value for position to addHelpText.
|
|
1784
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
1785
|
+
}
|
|
1786
|
+
const helpEvent = `${position}Help`;
|
|
1787
|
+
this.on(helpEvent, (context) => {
|
|
1788
|
+
let helpStr;
|
|
1789
|
+
if (typeof text === "function") {
|
|
1790
|
+
helpStr = text({ error: context.error, command: context.command });
|
|
1791
|
+
} else {
|
|
1792
|
+
helpStr = text;
|
|
1793
|
+
}
|
|
1794
|
+
if (helpStr) {
|
|
1795
|
+
context.write(`${helpStr}
|
|
1796
|
+
`);
|
|
1797
|
+
}
|
|
1798
|
+
});
|
|
1799
|
+
return this;
|
|
1800
|
+
}
|
|
1801
|
+
_outputHelpIfRequested(args) {
|
|
1802
|
+
const helpOption = this._getHelpOption();
|
|
1803
|
+
const helpRequested = helpOption && args.find((arg) => helpOption.is(arg));
|
|
1804
|
+
if (helpRequested) {
|
|
1805
|
+
this.outputHelp();
|
|
1806
|
+
this._exit(0, "commander.helpDisplayed", "(outputHelp)");
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
function incrementNodeInspectorPort(args) {
|
|
1811
|
+
return args.map((arg) => {
|
|
1812
|
+
if (!arg.startsWith("--inspect")) {
|
|
1813
|
+
return arg;
|
|
1814
|
+
}
|
|
1815
|
+
let debugOption;
|
|
1816
|
+
let debugHost = "127.0.0.1";
|
|
1817
|
+
let debugPort = "9229";
|
|
1818
|
+
let match;
|
|
1819
|
+
if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
|
|
1820
|
+
debugOption = match[1];
|
|
1821
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
|
|
1822
|
+
debugOption = match[1];
|
|
1823
|
+
if (/^\d+$/.test(match[3])) {
|
|
1824
|
+
debugPort = match[3];
|
|
1825
|
+
} else {
|
|
1826
|
+
debugHost = match[3];
|
|
1827
|
+
}
|
|
1828
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
|
|
1829
|
+
debugOption = match[1];
|
|
1830
|
+
debugHost = match[3];
|
|
1831
|
+
debugPort = match[4];
|
|
1832
|
+
}
|
|
1833
|
+
if (debugOption && debugPort !== "0") {
|
|
1834
|
+
return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
|
|
1835
|
+
}
|
|
1836
|
+
return arg;
|
|
1837
|
+
});
|
|
1838
|
+
}
|
|
1839
|
+
exports.Command = Command;
|
|
1840
|
+
});
|
|
1841
|
+
|
|
1842
|
+
// node_modules/commander/index.js
|
|
1843
|
+
var require_commander = __commonJS((exports) => {
|
|
1844
|
+
var { Argument } = require_argument();
|
|
1845
|
+
var { Command } = require_command();
|
|
1846
|
+
var { CommanderError, InvalidArgumentError } = require_error();
|
|
1847
|
+
var { Help } = require_help();
|
|
1848
|
+
var { Option } = require_option();
|
|
1849
|
+
exports.program = new Command;
|
|
1850
|
+
exports.createCommand = (name) => new Command(name);
|
|
1851
|
+
exports.createOption = (flags, description) => new Option(flags, description);
|
|
1852
|
+
exports.createArgument = (name, description) => new Argument(name, description);
|
|
1853
|
+
exports.Command = Command;
|
|
1854
|
+
exports.Option = Option;
|
|
1855
|
+
exports.Argument = Argument;
|
|
1856
|
+
exports.Help = Help;
|
|
1857
|
+
exports.CommanderError = CommanderError;
|
|
1858
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
1859
|
+
exports.InvalidOptionArgumentError = InvalidArgumentError;
|
|
1860
|
+
});
|
|
1861
|
+
|
|
1862
|
+
// src/cli.ts
|
|
1863
|
+
import { existsSync as existsSync3, readFileSync as readFileSync3 } from "fs";
|
|
1864
|
+
|
|
1865
|
+
// node_modules/commander/esm.mjs
|
|
1866
|
+
var import__ = __toESM(require_commander(), 1);
|
|
1867
|
+
var {
|
|
1868
|
+
program,
|
|
1869
|
+
createCommand,
|
|
1870
|
+
createArgument,
|
|
1871
|
+
createOption,
|
|
1872
|
+
CommanderError,
|
|
1873
|
+
InvalidArgumentError,
|
|
1874
|
+
InvalidOptionArgumentError,
|
|
1875
|
+
Command,
|
|
1876
|
+
Argument,
|
|
1877
|
+
Option,
|
|
1878
|
+
Help
|
|
1879
|
+
} = import__.default;
|
|
1880
|
+
|
|
1881
|
+
// src/config.ts
|
|
1882
|
+
import { readFileSync, existsSync } from "fs";
|
|
1883
|
+
|
|
1884
|
+
// src/exitCodes.ts
|
|
1885
|
+
var ExitCode = {
|
|
1886
|
+
SUCCESS: 0,
|
|
1887
|
+
ERROR: 1,
|
|
1888
|
+
USAGE: 64,
|
|
1889
|
+
DATAERR: 65,
|
|
1890
|
+
NOINPUT: 66,
|
|
1891
|
+
NOUSER: 67,
|
|
1892
|
+
NOHOST: 68,
|
|
1893
|
+
UNAVAILABLE: 69,
|
|
1894
|
+
SOFTWARE: 70,
|
|
1895
|
+
OSERR: 71,
|
|
1896
|
+
OSFILE: 72,
|
|
1897
|
+
CANTCREAT: 73,
|
|
1898
|
+
IOERR: 74,
|
|
1899
|
+
TEMPFAIL: 75,
|
|
1900
|
+
PROTOCOL: 76,
|
|
1901
|
+
NOPERM: 77,
|
|
1902
|
+
CONFIG: 78
|
|
1903
|
+
};
|
|
1904
|
+
|
|
1905
|
+
// src/errors.ts
|
|
1906
|
+
class CliError extends Error {
|
|
1907
|
+
exitCode;
|
|
1908
|
+
constructor(message, exitCode = ExitCode.ERROR) {
|
|
1909
|
+
super(message);
|
|
1910
|
+
this.name = "CliError";
|
|
1911
|
+
this.exitCode = exitCode;
|
|
1912
|
+
}
|
|
1913
|
+
}
|
|
1914
|
+
|
|
1915
|
+
class AuthError extends CliError {
|
|
1916
|
+
constructor(message) {
|
|
1917
|
+
super(message, ExitCode.NOPERM);
|
|
1918
|
+
this.name = "AuthError";
|
|
1919
|
+
}
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
class ConfigError extends CliError {
|
|
1923
|
+
constructor(message) {
|
|
1924
|
+
super(message, ExitCode.CONFIG);
|
|
1925
|
+
this.name = "ConfigError";
|
|
1926
|
+
}
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1929
|
+
class UsageError extends CliError {
|
|
1930
|
+
constructor(message) {
|
|
1931
|
+
super(message, ExitCode.USAGE);
|
|
1932
|
+
this.name = "UsageError";
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
class ServiceError extends CliError {
|
|
1936
|
+
constructor(message) {
|
|
1937
|
+
super(message, ExitCode.UNAVAILABLE);
|
|
1938
|
+
this.name = "ServiceError";
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
class IOError extends CliError {
|
|
1942
|
+
constructor(message) {
|
|
1943
|
+
super(message, ExitCode.IOERR);
|
|
1944
|
+
this.name = "IOError";
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
|
|
1948
|
+
class TempError extends CliError {
|
|
1949
|
+
constructor(message) {
|
|
1950
|
+
super(message, ExitCode.TEMPFAIL);
|
|
1951
|
+
this.name = "TempError";
|
|
1952
|
+
}
|
|
1953
|
+
}
|
|
1954
|
+
|
|
1955
|
+
// src/config.ts
|
|
1956
|
+
var DEFAULT_CONFIG = {
|
|
1957
|
+
httpUrl: "http://localhost:17170",
|
|
1958
|
+
username: "",
|
|
1959
|
+
endpoints: {
|
|
1960
|
+
auth: "/auth/simple/login",
|
|
1961
|
+
graphql: "/api/graphql",
|
|
1962
|
+
logout: "/auth/logout",
|
|
1963
|
+
refresh: "/auth/refresh"
|
|
1964
|
+
}
|
|
1965
|
+
};
|
|
1966
|
+
function parseConfigFile(filePath) {
|
|
1967
|
+
if (!existsSync(filePath)) {
|
|
1968
|
+
return {};
|
|
1969
|
+
}
|
|
1970
|
+
try {
|
|
1971
|
+
const content = readFileSync(filePath, "utf-8");
|
|
1972
|
+
const result = {};
|
|
1973
|
+
let currentSection = "general";
|
|
1974
|
+
for (const line of content.split(`
|
|
1975
|
+
`)) {
|
|
1976
|
+
const trimmed = line.trim();
|
|
1977
|
+
if (!trimmed || trimmed.startsWith("#"))
|
|
1978
|
+
continue;
|
|
1979
|
+
const sectionMatch = trimmed.match(/^\[(\w+)\]$/);
|
|
1980
|
+
if (sectionMatch) {
|
|
1981
|
+
currentSection = sectionMatch[1];
|
|
1982
|
+
continue;
|
|
1983
|
+
}
|
|
1984
|
+
const kvMatch = trimmed.match(/^(\w+)\s*=\s*(.+)$/);
|
|
1985
|
+
if (kvMatch) {
|
|
1986
|
+
const [, key, rawValue] = kvMatch;
|
|
1987
|
+
const value = rawValue.replace(/^["']|["']$/g, "").trim();
|
|
1988
|
+
if (currentSection === "general") {
|
|
1989
|
+
if (key === "http_host") {
|
|
1990
|
+
const port = result.httpUrl?.match(/:(\d+)$/)?.[1] || "17170";
|
|
1991
|
+
result.httpUrl = `http://${value}:${port}`;
|
|
1992
|
+
} else if (key === "http_port") {
|
|
1993
|
+
const host = result.httpUrl?.replace(/^http:\/\//, "").replace(/:\d+$/, "") || "localhost";
|
|
1994
|
+
result.httpUrl = `http://${host}:${value}`;
|
|
1995
|
+
}
|
|
1996
|
+
} else if (currentSection === "ldap") {
|
|
1997
|
+
if (key === "ldap_user_dn") {
|
|
1998
|
+
result.username = value;
|
|
1999
|
+
} else if (key === "ldap_user_pass") {
|
|
2000
|
+
result.password = value;
|
|
2001
|
+
}
|
|
2002
|
+
}
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
return result;
|
|
2006
|
+
} catch {
|
|
2007
|
+
return {};
|
|
2008
|
+
}
|
|
2009
|
+
}
|
|
2010
|
+
function buildConfig(cliOptions = {}) {
|
|
2011
|
+
const configFile = cliOptions.configFile || process.env.LLDAP_CONFIG || "/etc/lldap.toml";
|
|
2012
|
+
const fileConfig = parseConfigFile(configFile);
|
|
2013
|
+
const filteredCliOptions = Object.fromEntries(Object.entries(cliOptions).filter(([, v]) => v !== undefined));
|
|
2014
|
+
const config = {
|
|
2015
|
+
...DEFAULT_CONFIG,
|
|
2016
|
+
...fileConfig,
|
|
2017
|
+
...filteredCliOptions,
|
|
2018
|
+
endpoints: {
|
|
2019
|
+
auth: process.env.LLDAP_HTTPENDPOINT_AUTH || DEFAULT_CONFIG.endpoints.auth,
|
|
2020
|
+
graphql: process.env.LLDAP_HTTPENDPOINT_GRAPH || DEFAULT_CONFIG.endpoints.graphql,
|
|
2021
|
+
logout: process.env.LLDAP_HTTPENDPOINT_LOGOUT || DEFAULT_CONFIG.endpoints.logout,
|
|
2022
|
+
refresh: process.env.LLDAP_HTTPENDPOINT_REFRESH || DEFAULT_CONFIG.endpoints.refresh
|
|
2023
|
+
}
|
|
2024
|
+
};
|
|
2025
|
+
if (process.env.LLDAP_HTTP_URL || process.env.LLDAP_HTTPURL) {
|
|
2026
|
+
config.httpUrl = process.env.LLDAP_HTTP_URL || process.env.LLDAP_HTTPURL || config.httpUrl;
|
|
2027
|
+
}
|
|
2028
|
+
if (process.env.LLDAP_USERNAME)
|
|
2029
|
+
config.username = process.env.LLDAP_USERNAME;
|
|
2030
|
+
if (process.env.LLDAP_PASSWORD)
|
|
2031
|
+
config.password = process.env.LLDAP_PASSWORD;
|
|
2032
|
+
if (process.env.LLDAP_TOKEN)
|
|
2033
|
+
config.token = process.env.LLDAP_TOKEN;
|
|
2034
|
+
if (process.env.LLDAP_REFRESH_TOKEN || process.env.LLDAP_REFRESHTOKEN) {
|
|
2035
|
+
config.refreshToken = process.env.LLDAP_REFRESH_TOKEN || process.env.LLDAP_REFRESHTOKEN;
|
|
2036
|
+
}
|
|
2037
|
+
if (cliOptions.httpUrl)
|
|
2038
|
+
config.httpUrl = cliOptions.httpUrl;
|
|
2039
|
+
if (cliOptions.username)
|
|
2040
|
+
config.username = cliOptions.username;
|
|
2041
|
+
if (cliOptions.password)
|
|
2042
|
+
config.password = cliOptions.password;
|
|
2043
|
+
if (cliOptions.token)
|
|
2044
|
+
config.token = cliOptions.token;
|
|
2045
|
+
if (cliOptions.refreshToken)
|
|
2046
|
+
config.refreshToken = cliOptions.refreshToken;
|
|
2047
|
+
if (!config.token && !config.refreshToken) {
|
|
2048
|
+
if (!config.username) {
|
|
2049
|
+
throw new ConfigError("Username is required. Set via -D option, LLDAP_USERNAME env var, or config file.");
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
2052
|
+
if (config.httpUrl.startsWith("http://") && !config.httpUrl.includes("localhost") && !config.httpUrl.includes("127.0.0.1")) {
|
|
2053
|
+
console.error("WARNING: Using insecure HTTP connection to non-localhost server.");
|
|
2054
|
+
console.error("Consider using HTTPS for production environments.");
|
|
2055
|
+
}
|
|
2056
|
+
return config;
|
|
2057
|
+
}
|
|
2058
|
+
|
|
2059
|
+
// src/client.ts
|
|
2060
|
+
import { readFileSync as readFileSync2, existsSync as existsSync2 } from "fs";
|
|
2061
|
+
import { resolve, normalize } from "path";
|
|
2062
|
+
class LldapClient {
|
|
2063
|
+
config;
|
|
2064
|
+
token;
|
|
2065
|
+
refreshToken;
|
|
2066
|
+
shouldLogout = false;
|
|
2067
|
+
rateLimitRetryCount = 0;
|
|
2068
|
+
lastActivityTime;
|
|
2069
|
+
static MAX_RATE_LIMIT_RETRIES = 3;
|
|
2070
|
+
static RATE_LIMIT_BACKOFF_MS = 1000;
|
|
2071
|
+
static SESSION_TIMEOUT_MS = 30 * 60 * 1000;
|
|
2072
|
+
static MAX_INPUT_LENGTH = 1000;
|
|
2073
|
+
static MAX_EMAIL_LENGTH = 254;
|
|
2074
|
+
static MAX_USERNAME_LENGTH = 64;
|
|
2075
|
+
static debugEnabled = false;
|
|
2076
|
+
constructor(config) {
|
|
2077
|
+
this.config = config;
|
|
2078
|
+
this.token = config.token;
|
|
2079
|
+
this.refreshToken = config.refreshToken;
|
|
2080
|
+
this.lastActivityTime = Date.now();
|
|
2081
|
+
}
|
|
2082
|
+
static setDebugEnabled(enabled) {
|
|
2083
|
+
LldapClient.debugEnabled = enabled;
|
|
2084
|
+
}
|
|
2085
|
+
debug(message) {
|
|
2086
|
+
if (LldapClient.debugEnabled) {
|
|
2087
|
+
const sanitized = message.replace(/token[=:]\S+/gi, "token=[REDACTED]").replace(/password[=:]\S+/gi, "password=[REDACTED]").replace(/Bearer \S+/gi, "Bearer [REDACTED]");
|
|
2088
|
+
console.error(`[DEBUG] ${new Date().toISOString()} ${sanitized}`);
|
|
2089
|
+
}
|
|
2090
|
+
}
|
|
2091
|
+
audit(level, action, details) {
|
|
2092
|
+
const entry = {
|
|
2093
|
+
timestamp: new Date().toISOString(),
|
|
2094
|
+
level,
|
|
2095
|
+
action,
|
|
2096
|
+
...details
|
|
2097
|
+
};
|
|
2098
|
+
console.error(`[AUDIT:${level.toUpperCase()}] ${JSON.stringify(entry)}`);
|
|
2099
|
+
}
|
|
2100
|
+
checkSessionTimeout() {
|
|
2101
|
+
const now = Date.now();
|
|
2102
|
+
const elapsed = now - this.lastActivityTime;
|
|
2103
|
+
if (elapsed > LldapClient.SESSION_TIMEOUT_MS) {
|
|
2104
|
+
this.audit("security", "session_timeout", { elapsed_ms: elapsed });
|
|
2105
|
+
this.token = undefined;
|
|
2106
|
+
this.refreshToken = undefined;
|
|
2107
|
+
throw new AuthError("Session timed out due to inactivity. Please re-authenticate.");
|
|
2108
|
+
}
|
|
2109
|
+
this.lastActivityTime = now;
|
|
2110
|
+
}
|
|
2111
|
+
validateInputLength(value, fieldName, maxLength = LldapClient.MAX_INPUT_LENGTH) {
|
|
2112
|
+
if (value.length > maxLength) {
|
|
2113
|
+
this.audit("warn", "input_length_exceeded", { field: fieldName, length: value.length, max: maxLength });
|
|
2114
|
+
throw new UsageError(`${fieldName} exceeds maximum length of ${maxLength} characters`);
|
|
2115
|
+
}
|
|
2116
|
+
}
|
|
2117
|
+
validateUsername(username) {
|
|
2118
|
+
this.validateInputLength(username, "username", LldapClient.MAX_USERNAME_LENGTH);
|
|
2119
|
+
if (!/^[a-zA-Z0-9_.-]+$/.test(username)) {
|
|
2120
|
+
throw new UsageError("Username contains invalid characters. Only alphanumeric, underscore, dot, and hyphen allowed.");
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
validateEmail(email) {
|
|
2124
|
+
this.validateInputLength(email, "email", LldapClient.MAX_EMAIL_LENGTH);
|
|
2125
|
+
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
|
2126
|
+
throw new UsageError("Invalid email format");
|
|
2127
|
+
}
|
|
2128
|
+
}
|
|
2129
|
+
validateStringInput(value, fieldName) {
|
|
2130
|
+
this.validateInputLength(value, fieldName);
|
|
2131
|
+
}
|
|
2132
|
+
isTokenExpired(token, bufferSeconds = 60) {
|
|
2133
|
+
try {
|
|
2134
|
+
const parts = token.split(".");
|
|
2135
|
+
if (parts.length !== 3) {
|
|
2136
|
+
return true;
|
|
2137
|
+
}
|
|
2138
|
+
const payload = parts[1].replace(/-/g, "+").replace(/_/g, "/");
|
|
2139
|
+
const decoded = JSON.parse(atob(payload));
|
|
2140
|
+
if (!decoded.exp) {
|
|
2141
|
+
return false;
|
|
2142
|
+
}
|
|
2143
|
+
const expirationTime = decoded.exp * 1000;
|
|
2144
|
+
const now = Date.now();
|
|
2145
|
+
const bufferMs = bufferSeconds * 1000;
|
|
2146
|
+
return now >= expirationTime - bufferMs;
|
|
2147
|
+
} catch {
|
|
2148
|
+
return true;
|
|
2149
|
+
}
|
|
2150
|
+
}
|
|
2151
|
+
validateFilePath(filePath, allowedDir) {
|
|
2152
|
+
const resolved = resolve(filePath);
|
|
2153
|
+
const normalized = normalize(resolved);
|
|
2154
|
+
if (normalized.includes("..")) {
|
|
2155
|
+
throw new UsageError("Invalid file path: path traversal detected");
|
|
2156
|
+
}
|
|
2157
|
+
if (allowedDir) {
|
|
2158
|
+
const allowedResolved = resolve(allowedDir);
|
|
2159
|
+
if (!normalized.startsWith(allowedResolved)) {
|
|
2160
|
+
throw new UsageError(`Invalid file path: must be within ${allowedDir}`);
|
|
2161
|
+
}
|
|
2162
|
+
}
|
|
2163
|
+
return normalized;
|
|
2164
|
+
}
|
|
2165
|
+
async handleRateLimit(operation) {
|
|
2166
|
+
while (this.rateLimitRetryCount < LldapClient.MAX_RATE_LIMIT_RETRIES) {
|
|
2167
|
+
try {
|
|
2168
|
+
const result = await operation();
|
|
2169
|
+
this.rateLimitRetryCount = 0;
|
|
2170
|
+
return result;
|
|
2171
|
+
} catch (error) {
|
|
2172
|
+
if (error instanceof Error && error.message.includes("429")) {
|
|
2173
|
+
this.rateLimitRetryCount++;
|
|
2174
|
+
if (this.rateLimitRetryCount >= LldapClient.MAX_RATE_LIMIT_RETRIES) {
|
|
2175
|
+
throw new TempError("Rate limit exceeded. Please try again later.");
|
|
2176
|
+
}
|
|
2177
|
+
const backoff = LldapClient.RATE_LIMIT_BACKOFF_MS * Math.pow(2, this.rateLimitRetryCount - 1);
|
|
2178
|
+
console.error(`Rate limited. Retrying in ${backoff}ms... (attempt ${this.rateLimitRetryCount}/${LldapClient.MAX_RATE_LIMIT_RETRIES})`);
|
|
2179
|
+
await new Promise((resolve2) => setTimeout(resolve2, backoff));
|
|
2180
|
+
} else {
|
|
2181
|
+
throw error;
|
|
2182
|
+
}
|
|
2183
|
+
}
|
|
2184
|
+
}
|
|
2185
|
+
throw new TempError("Rate limit exceeded. Please try again later.");
|
|
2186
|
+
}
|
|
2187
|
+
async login(username, password) {
|
|
2188
|
+
return this.handleRateLimit(async () => {
|
|
2189
|
+
const url = `${this.config.httpUrl}${this.config.endpoints.auth}`;
|
|
2190
|
+
const user = username || this.config.username;
|
|
2191
|
+
const pass = password || this.config.password;
|
|
2192
|
+
if (!user || !pass) {
|
|
2193
|
+
throw new ConfigError("Username and password are required for login");
|
|
2194
|
+
}
|
|
2195
|
+
this.validateUsername(user);
|
|
2196
|
+
this.debug(`Attempting login for user: ${user}`);
|
|
2197
|
+
this.audit("info", "login_attempt", { username: user });
|
|
2198
|
+
const response = await fetch(url, {
|
|
2199
|
+
method: "POST",
|
|
2200
|
+
headers: { "Content-Type": "application/json" },
|
|
2201
|
+
body: JSON.stringify({ username: user, password: pass })
|
|
2202
|
+
});
|
|
2203
|
+
if (response.status === 429) {
|
|
2204
|
+
this.audit("warn", "login_rate_limited", { username: user });
|
|
2205
|
+
throw new TempError("429 Rate limit exceeded");
|
|
2206
|
+
}
|
|
2207
|
+
if (!response.ok) {
|
|
2208
|
+
const text = await response.text();
|
|
2209
|
+
this.audit("security", "login_failed", { username: user, status: response.status });
|
|
2210
|
+
const sanitizedText = text.replace(/password[=:]\S+/gi, "password=[REDACTED]");
|
|
2211
|
+
throw new AuthError(`Login failed: ${sanitizedText}`);
|
|
2212
|
+
}
|
|
2213
|
+
const data = await response.json();
|
|
2214
|
+
this.token = data.token;
|
|
2215
|
+
this.refreshToken = data.refreshToken;
|
|
2216
|
+
this.lastActivityTime = Date.now();
|
|
2217
|
+
this.audit("info", "login_success", { username: user });
|
|
2218
|
+
this.debug("Login successful");
|
|
2219
|
+
return { token: data.token, refreshToken: data.refreshToken };
|
|
2220
|
+
});
|
|
2221
|
+
}
|
|
2222
|
+
async refresh() {
|
|
2223
|
+
if (!this.refreshToken) {
|
|
2224
|
+
throw new AuthError("Refresh token is required");
|
|
2225
|
+
}
|
|
2226
|
+
const url = `${this.config.httpUrl}${this.config.endpoints.refresh}`;
|
|
2227
|
+
const response = await fetch(url, {
|
|
2228
|
+
headers: { Cookie: `refresh_token=${this.refreshToken}` }
|
|
2229
|
+
});
|
|
2230
|
+
if (!response.ok) {
|
|
2231
|
+
const text = await response.text();
|
|
2232
|
+
throw new AuthError(`Token refresh failed: ${text}`);
|
|
2233
|
+
}
|
|
2234
|
+
const data = await response.json();
|
|
2235
|
+
this.token = data.token;
|
|
2236
|
+
return data.token;
|
|
2237
|
+
}
|
|
2238
|
+
async logout() {
|
|
2239
|
+
if (!this.refreshToken) {
|
|
2240
|
+
throw new AuthError("Refresh token is required for logout");
|
|
2241
|
+
}
|
|
2242
|
+
const url = `${this.config.httpUrl}${this.config.endpoints.logout}`;
|
|
2243
|
+
await fetch(url, {
|
|
2244
|
+
headers: { Cookie: `refresh_token=${this.refreshToken}` }
|
|
2245
|
+
});
|
|
2246
|
+
}
|
|
2247
|
+
async ensureAuthenticated() {
|
|
2248
|
+
this.checkSessionTimeout();
|
|
2249
|
+
if (this.token && !this.isTokenExpired(this.token)) {
|
|
2250
|
+
this.debug("Using existing valid token");
|
|
2251
|
+
return;
|
|
2252
|
+
}
|
|
2253
|
+
if (this.refreshToken) {
|
|
2254
|
+
if (this.isTokenExpired(this.refreshToken, 300)) {
|
|
2255
|
+
this.audit("warn", "refresh_token_expiring");
|
|
2256
|
+
console.error("WARNING: Refresh token is expired or about to expire. Re-authentication may be required.");
|
|
2257
|
+
}
|
|
2258
|
+
this.debug("Refreshing token");
|
|
2259
|
+
await this.refresh();
|
|
2260
|
+
return;
|
|
2261
|
+
}
|
|
2262
|
+
this.debug("No valid token, initiating login");
|
|
2263
|
+
await this.login();
|
|
2264
|
+
this.shouldLogout = true;
|
|
2265
|
+
}
|
|
2266
|
+
async cleanup() {
|
|
2267
|
+
if (this.shouldLogout && this.refreshToken) {
|
|
2268
|
+
await this.logout();
|
|
2269
|
+
}
|
|
2270
|
+
}
|
|
2271
|
+
async query(query, variables, file) {
|
|
2272
|
+
return this.handleRateLimit(async () => {
|
|
2273
|
+
await this.ensureAuthenticated();
|
|
2274
|
+
const url = `${this.config.httpUrl}${this.config.endpoints.graphql}`;
|
|
2275
|
+
let body;
|
|
2276
|
+
if (file) {
|
|
2277
|
+
const validatedPath = this.validateFilePath(file);
|
|
2278
|
+
if (!existsSync2(validatedPath)) {
|
|
2279
|
+
throw new IOError(`File not found: ${file}`);
|
|
2280
|
+
}
|
|
2281
|
+
const fileContent = readFileSync2(validatedPath);
|
|
2282
|
+
const base64 = fileContent.toString("base64");
|
|
2283
|
+
const varsWithFile = { ...variables };
|
|
2284
|
+
body = JSON.stringify({
|
|
2285
|
+
query,
|
|
2286
|
+
variables: varsWithFile
|
|
2287
|
+
});
|
|
2288
|
+
if (body.includes('""')) {
|
|
2289
|
+
body = body.replace('""', `"${base64}"`);
|
|
2290
|
+
}
|
|
2291
|
+
} else {
|
|
2292
|
+
body = JSON.stringify({
|
|
2293
|
+
query,
|
|
2294
|
+
variables: variables || {}
|
|
2295
|
+
});
|
|
2296
|
+
}
|
|
2297
|
+
const response = await fetch(url, {
|
|
2298
|
+
method: "POST",
|
|
2299
|
+
headers: {
|
|
2300
|
+
"Content-Type": "application/json",
|
|
2301
|
+
Authorization: `Bearer ${this.token}`
|
|
2302
|
+
},
|
|
2303
|
+
body
|
|
2304
|
+
});
|
|
2305
|
+
if (response.status === 401) {
|
|
2306
|
+
throw new AuthError("Authentication failed. Token may be expired. Try logging in again.");
|
|
2307
|
+
}
|
|
2308
|
+
if (response.status === 429) {
|
|
2309
|
+
throw new TempError("429 Rate limit exceeded");
|
|
2310
|
+
}
|
|
2311
|
+
if (!response.ok) {
|
|
2312
|
+
const text = await response.text();
|
|
2313
|
+
const sanitizedText = text.replace(/token[=:]\S+/gi, "token=[REDACTED]");
|
|
2314
|
+
throw new ServiceError(`GraphQL request failed (${response.status}): ${sanitizedText}`);
|
|
2315
|
+
}
|
|
2316
|
+
const result = await response.json();
|
|
2317
|
+
if (result.errors?.length) {
|
|
2318
|
+
const sanitizedErrors = result.errors.map((e) => e.message.replace(/token[=:]\S+/gi, "token=[REDACTED]"));
|
|
2319
|
+
throw new ServiceError(`GraphQL error: ${sanitizedErrors.join(", ")}`);
|
|
2320
|
+
}
|
|
2321
|
+
if (!result.data) {
|
|
2322
|
+
throw new ServiceError("No data returned from GraphQL query");
|
|
2323
|
+
}
|
|
2324
|
+
return result.data;
|
|
2325
|
+
});
|
|
2326
|
+
}
|
|
2327
|
+
getToken() {
|
|
2328
|
+
return this.token;
|
|
2329
|
+
}
|
|
2330
|
+
getRefreshToken() {
|
|
2331
|
+
return this.refreshToken;
|
|
2332
|
+
}
|
|
2333
|
+
}
|
|
2334
|
+
|
|
2335
|
+
// src/users.ts
|
|
2336
|
+
class UserService {
|
|
2337
|
+
client;
|
|
2338
|
+
constructor(client) {
|
|
2339
|
+
this.client = client;
|
|
2340
|
+
}
|
|
2341
|
+
async getUsers() {
|
|
2342
|
+
const query = "{users{id creationDate uuid email displayName firstName lastName}}";
|
|
2343
|
+
const data = await this.client.query(query);
|
|
2344
|
+
return data.users;
|
|
2345
|
+
}
|
|
2346
|
+
async listUserIds() {
|
|
2347
|
+
const users = await this.getUsers();
|
|
2348
|
+
return users.map((u) => u.id);
|
|
2349
|
+
}
|
|
2350
|
+
async listUserEmails() {
|
|
2351
|
+
const users = await this.getUsers();
|
|
2352
|
+
return users.map((u) => u.email).sort();
|
|
2353
|
+
}
|
|
2354
|
+
async searchUsers(pattern) {
|
|
2355
|
+
const users = await this.getUsers();
|
|
2356
|
+
const regex = this.globToRegex(pattern);
|
|
2357
|
+
return users.filter((u) => regex.test(u.id) || regex.test(u.email) || regex.test(u.displayName || ""));
|
|
2358
|
+
}
|
|
2359
|
+
async getUsersByGroup(groupName) {
|
|
2360
|
+
const query = "{users{id creationDate uuid email displayName firstName lastName groups{displayName}}}";
|
|
2361
|
+
const data = await this.client.query(query);
|
|
2362
|
+
return data.users.filter((u) => u.groups.some((g) => g.displayName.toLowerCase() === groupName.toLowerCase())).map(({ groups, ...user }) => user);
|
|
2363
|
+
}
|
|
2364
|
+
globToRegex(pattern) {
|
|
2365
|
+
const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, ".");
|
|
2366
|
+
return new RegExp(`^${escaped}$`, "i");
|
|
2367
|
+
}
|
|
2368
|
+
async getUserIdByEmail(email) {
|
|
2369
|
+
const users = await this.getUsers();
|
|
2370
|
+
const user = users.find((u) => u.email === email);
|
|
2371
|
+
return user?.id || null;
|
|
2372
|
+
}
|
|
2373
|
+
async resolveUserId(identifier) {
|
|
2374
|
+
const emailRegex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
|
|
2375
|
+
if (emailRegex.test(identifier)) {
|
|
2376
|
+
const userId = await this.getUserIdByEmail(identifier);
|
|
2377
|
+
if (!userId) {
|
|
2378
|
+
throw new Error(`No user found with email: ${identifier}`);
|
|
2379
|
+
}
|
|
2380
|
+
return userId;
|
|
2381
|
+
}
|
|
2382
|
+
return identifier;
|
|
2383
|
+
}
|
|
2384
|
+
async getUserGroups(userId) {
|
|
2385
|
+
const query = "query getUserGroups($id:String!){user(userId:$id){groups{displayName}}}";
|
|
2386
|
+
const data = await this.client.query(query, { id: userId });
|
|
2387
|
+
return data.user.groups.map((g) => g.displayName);
|
|
2388
|
+
}
|
|
2389
|
+
async listUserAttributes(userId) {
|
|
2390
|
+
const query = "query getUserInfo($id:String!){user(userId:$id){attributes{name}}}";
|
|
2391
|
+
const data = await this.client.query(query, { id: userId });
|
|
2392
|
+
return data.user.attributes.map((a) => a.name).sort();
|
|
2393
|
+
}
|
|
2394
|
+
async getUserAttributeValues(userId, attribute) {
|
|
2395
|
+
const query = "query getUserInfo($id:String!){user(userId:$id){attributes{name,value}}}";
|
|
2396
|
+
const data = await this.client.query(query, { id: userId });
|
|
2397
|
+
const attr = data.user.attributes.find((a) => a.name === attribute);
|
|
2398
|
+
return attr?.value || [];
|
|
2399
|
+
}
|
|
2400
|
+
async createUser(id, email, options = {}) {
|
|
2401
|
+
this.client.validateUsername(id);
|
|
2402
|
+
this.client.validateEmail(email);
|
|
2403
|
+
if (options.displayName)
|
|
2404
|
+
this.client.validateStringInput(options.displayName, "displayName");
|
|
2405
|
+
if (options.firstName)
|
|
2406
|
+
this.client.validateStringInput(options.firstName, "firstName");
|
|
2407
|
+
if (options.lastName)
|
|
2408
|
+
this.client.validateStringInput(options.lastName, "lastName");
|
|
2409
|
+
const query = `mutation createUser($user:CreateUserInput!){
|
|
2410
|
+
createUser(user:$user){id email displayName firstName lastName avatar}
|
|
2411
|
+
}`;
|
|
2412
|
+
const variables = {
|
|
2413
|
+
user: {
|
|
2414
|
+
id,
|
|
2415
|
+
email,
|
|
2416
|
+
displayName: options.displayName || "",
|
|
2417
|
+
firstName: options.firstName || "",
|
|
2418
|
+
lastName: options.lastName || "",
|
|
2419
|
+
avatar: options.avatar || ""
|
|
2420
|
+
}
|
|
2421
|
+
};
|
|
2422
|
+
await this.client.query(query, variables, options.avatar);
|
|
2423
|
+
console.log(`Created user: ${id}`);
|
|
2424
|
+
}
|
|
2425
|
+
async deleteUser(userId) {
|
|
2426
|
+
const query = "mutation deleteUser($userId:String!){deleteUser(userId:$userId){ok}}";
|
|
2427
|
+
const data = await this.client.query(query, { userId });
|
|
2428
|
+
if (data.deleteUser.ok) {
|
|
2429
|
+
console.log(`Deleted user: ${userId}`);
|
|
2430
|
+
} else {
|
|
2431
|
+
throw new Error(`Failed to delete user: ${userId}`);
|
|
2432
|
+
}
|
|
2433
|
+
}
|
|
2434
|
+
async getSchemaAttribute(attribute) {
|
|
2435
|
+
const query = "{schema{userSchema{attributes{name,attributeType,isList,isVisible,isEditable}}}}";
|
|
2436
|
+
const data = await this.client.query(query);
|
|
2437
|
+
return data.schema.userSchema.attributes.find((a) => a.name === attribute) || null;
|
|
2438
|
+
}
|
|
2439
|
+
async updateUserAttribute(mutation, userId, attribute, value) {
|
|
2440
|
+
const schemaAttr = await this.getSchemaAttribute(attribute);
|
|
2441
|
+
if (!schemaAttr) {
|
|
2442
|
+
throw new Error(`Attribute ${attribute} is not part of user schema.`);
|
|
2443
|
+
}
|
|
2444
|
+
const isList = schemaAttr.isList;
|
|
2445
|
+
if (mutation === "set" && isList) {
|
|
2446
|
+
throw new Error(`Attribute ${attribute} is a list and cannot be modified using the ${mutation} mutation.`);
|
|
2447
|
+
}
|
|
2448
|
+
if ((mutation === "add" || mutation === "del") && !isList) {
|
|
2449
|
+
throw new Error(`Attribute ${attribute} is not a list and cannot be modified using the ${mutation} mutation.`);
|
|
2450
|
+
}
|
|
2451
|
+
const query = "mutation updateUser($user:UpdateUserInput!){updateUser(user:$user){ok}}";
|
|
2452
|
+
let variables;
|
|
2453
|
+
let resultLine;
|
|
2454
|
+
switch (mutation) {
|
|
2455
|
+
case "set":
|
|
2456
|
+
variables = {
|
|
2457
|
+
user: {
|
|
2458
|
+
id: userId,
|
|
2459
|
+
insertAttributes: { name: attribute, value: value || "" }
|
|
2460
|
+
}
|
|
2461
|
+
};
|
|
2462
|
+
resultLine = `Attribute set for user: ${userId}, attribute: ${attribute}`;
|
|
2463
|
+
if (value && schemaAttr.attributeType !== "JPEG_PHOTO") {
|
|
2464
|
+
resultLine += `, value: ${value}`;
|
|
2465
|
+
}
|
|
2466
|
+
break;
|
|
2467
|
+
case "clear": {
|
|
2468
|
+
const currentValues = await this.getUserAttributeValues(userId, attribute);
|
|
2469
|
+
if (currentValues.length === 0) {
|
|
2470
|
+
throw new Error(`Attribute ${attribute} has no value set for user ${userId}, so nothing to clear.`);
|
|
2471
|
+
}
|
|
2472
|
+
variables = {
|
|
2473
|
+
user: {
|
|
2474
|
+
id: userId,
|
|
2475
|
+
removeAttributes: attribute
|
|
2476
|
+
}
|
|
2477
|
+
};
|
|
2478
|
+
resultLine = `Attribute cleared for user: ${userId}, attribute: ${attribute}`;
|
|
2479
|
+
break;
|
|
2480
|
+
}
|
|
2481
|
+
case "add": {
|
|
2482
|
+
if (!value) {
|
|
2483
|
+
throw new Error("Value is required for add mutation");
|
|
2484
|
+
}
|
|
2485
|
+
const currentValues = await this.getUserAttributeValues(userId, attribute);
|
|
2486
|
+
const newValues = [...currentValues, value];
|
|
2487
|
+
variables = {
|
|
2488
|
+
user: {
|
|
2489
|
+
id: userId,
|
|
2490
|
+
insertAttributes: { name: attribute, value: newValues }
|
|
2491
|
+
}
|
|
2492
|
+
};
|
|
2493
|
+
resultLine = `Attribute list value added for user: ${userId}, attribute: ${attribute}, value: ${value}`;
|
|
2494
|
+
break;
|
|
2495
|
+
}
|
|
2496
|
+
case "del": {
|
|
2497
|
+
if (!value) {
|
|
2498
|
+
throw new Error("Value is required for del mutation");
|
|
2499
|
+
}
|
|
2500
|
+
const currentValues = await this.getUserAttributeValues(userId, attribute);
|
|
2501
|
+
if (!currentValues.includes(value)) {
|
|
2502
|
+
throw new Error(`Attribute ${attribute} has no listed value ${value} for user ${userId}, so no value to delete.`);
|
|
2503
|
+
}
|
|
2504
|
+
const newValues = currentValues.filter((v) => v !== value);
|
|
2505
|
+
if (newValues.length === 0) {
|
|
2506
|
+
return this.updateUserAttribute("clear", userId, attribute);
|
|
2507
|
+
}
|
|
2508
|
+
variables = {
|
|
2509
|
+
user: {
|
|
2510
|
+
id: userId,
|
|
2511
|
+
insertAttributes: { name: attribute, value: newValues }
|
|
2512
|
+
}
|
|
2513
|
+
};
|
|
2514
|
+
resultLine = `Attribute list value deleted for user: ${userId}, attribute: ${attribute}, value: ${value}`;
|
|
2515
|
+
break;
|
|
2516
|
+
}
|
|
2517
|
+
default:
|
|
2518
|
+
throw new Error(`Unknown mutation type: ${mutation}`);
|
|
2519
|
+
}
|
|
2520
|
+
await this.client.query(query, variables);
|
|
2521
|
+
console.log(resultLine);
|
|
2522
|
+
}
|
|
2523
|
+
validateInput(value, fieldName) {
|
|
2524
|
+
const dangerousChars = /[;&|`$()<>\\"\n\r\t\0]/;
|
|
2525
|
+
if (dangerousChars.test(value)) {
|
|
2526
|
+
throw new Error(`Invalid ${fieldName}: contains potentially dangerous characters`);
|
|
2527
|
+
}
|
|
2528
|
+
if (!value || value.trim().length === 0) {
|
|
2529
|
+
throw new Error(`Invalid ${fieldName}: cannot be empty`);
|
|
2530
|
+
}
|
|
2531
|
+
}
|
|
2532
|
+
validatePassword(password) {
|
|
2533
|
+
if (password.length < 8) {
|
|
2534
|
+
throw new Error("New password is too short, expected at least 8 characters");
|
|
2535
|
+
}
|
|
2536
|
+
if (password.length > 128) {
|
|
2537
|
+
throw new Error("New password is too long, maximum 128 characters");
|
|
2538
|
+
}
|
|
2539
|
+
const hasLetter = /[a-zA-Z]/.test(password);
|
|
2540
|
+
const hasNumber = /[0-9]/.test(password);
|
|
2541
|
+
if (!hasLetter || !hasNumber) {
|
|
2542
|
+
throw new Error("Password must contain at least one letter and one number");
|
|
2543
|
+
}
|
|
2544
|
+
}
|
|
2545
|
+
async setPassword(userId, password, httpUrl, token) {
|
|
2546
|
+
this.validateInput(userId, "userId");
|
|
2547
|
+
this.validatePassword(password);
|
|
2548
|
+
this.validateInput(httpUrl, "httpUrl");
|
|
2549
|
+
this.validateInput(token, "token");
|
|
2550
|
+
const proc = Bun.spawn(["lldap_set_password", "-b", httpUrl, `--token=${token}`, "-u", userId], {
|
|
2551
|
+
stdin: "pipe",
|
|
2552
|
+
stdout: "pipe",
|
|
2553
|
+
stderr: "pipe"
|
|
2554
|
+
});
|
|
2555
|
+
const stdin = proc.stdin;
|
|
2556
|
+
stdin.write(new TextEncoder().encode(password + `
|
|
2557
|
+
`));
|
|
2558
|
+
stdin.end();
|
|
2559
|
+
const exitCode = await proc.exited;
|
|
2560
|
+
if (exitCode !== 0) {
|
|
2561
|
+
const stderr = await new Response(proc.stderr).text();
|
|
2562
|
+
const sanitizedError = stderr.replace(/token[=:]\S+/gi, "token=[REDACTED]");
|
|
2563
|
+
throw new Error(`Failed to set password: ${sanitizedError}`);
|
|
2564
|
+
}
|
|
2565
|
+
}
|
|
2566
|
+
async addToGroup(userId, groupId) {
|
|
2567
|
+
const query = "mutation addUserToGroup($userId:String!,$groupId:Int!){addUserToGroup(userId:$userId,groupId:$groupId){ok}}";
|
|
2568
|
+
await this.client.query(query, { userId, groupId });
|
|
2569
|
+
}
|
|
2570
|
+
async removeFromGroup(userId, groupId) {
|
|
2571
|
+
const query = "mutation removeUserFromGroup($userId:String!,$groupId:Int!){removeUserFromGroup(userId:$userId,groupId:$groupId){ok}}";
|
|
2572
|
+
await this.client.query(query, { userId, groupId });
|
|
2573
|
+
}
|
|
2574
|
+
}
|
|
2575
|
+
|
|
2576
|
+
// src/groups.ts
|
|
2577
|
+
class GroupService {
|
|
2578
|
+
client;
|
|
2579
|
+
constructor(client) {
|
|
2580
|
+
this.client = client;
|
|
2581
|
+
}
|
|
2582
|
+
async getGroups() {
|
|
2583
|
+
const query = "{groups{id creationDate uuid displayName}}";
|
|
2584
|
+
const data = await this.client.query(query);
|
|
2585
|
+
return data.groups;
|
|
2586
|
+
}
|
|
2587
|
+
async getGroupId(name) {
|
|
2588
|
+
const groups = await this.getGroups();
|
|
2589
|
+
const group = groups.find((g) => g.displayName === name);
|
|
2590
|
+
if (!group) {
|
|
2591
|
+
throw new Error(`Failed to retrieve group ID for group: ${name}`);
|
|
2592
|
+
}
|
|
2593
|
+
return group.id;
|
|
2594
|
+
}
|
|
2595
|
+
async searchGroups(pattern) {
|
|
2596
|
+
const groups = await this.getGroups();
|
|
2597
|
+
const regex = this.globToRegex(pattern);
|
|
2598
|
+
return groups.filter((g) => regex.test(g.displayName));
|
|
2599
|
+
}
|
|
2600
|
+
globToRegex(pattern) {
|
|
2601
|
+
const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, ".");
|
|
2602
|
+
return new RegExp(`^${escaped}$`, "i");
|
|
2603
|
+
}
|
|
2604
|
+
async listUserIdsByGroupName(groupName) {
|
|
2605
|
+
const groupId = await this.getGroupId(groupName);
|
|
2606
|
+
const query = "query listUsersByGroupName($id:Int!){group:group(groupId:$id){users{id}}}";
|
|
2607
|
+
const data = await this.client.query(query, { id: groupId });
|
|
2608
|
+
return data.group.users.map((u) => u.id);
|
|
2609
|
+
}
|
|
2610
|
+
async listUserEmailsByGroupName(groupName) {
|
|
2611
|
+
const groupId = await this.getGroupId(groupName);
|
|
2612
|
+
const query = "query listUsersByGroupName($id:Int!){group:group(groupId:$id){users{email}}}";
|
|
2613
|
+
const data = await this.client.query(query, { id: groupId });
|
|
2614
|
+
return data.group.users.map((u) => u.email);
|
|
2615
|
+
}
|
|
2616
|
+
async listGroupAttributes(groupId) {
|
|
2617
|
+
const query = "query getGroupInfo($id:Int!){group(groupId:$id){attributes{name}}}";
|
|
2618
|
+
const data = await this.client.query(query, { id: groupId });
|
|
2619
|
+
return data.group.attributes.map((a) => a.name).sort();
|
|
2620
|
+
}
|
|
2621
|
+
async getGroupAttributeValues(groupId, attribute) {
|
|
2622
|
+
const query = "query getGroupInfo($id:Int!){group(groupId:$id){attributes{name,value}}}";
|
|
2623
|
+
const data = await this.client.query(query, { id: groupId });
|
|
2624
|
+
const attr = data.group.attributes.find((a) => a.name === attribute);
|
|
2625
|
+
return attr?.value || [];
|
|
2626
|
+
}
|
|
2627
|
+
async createGroup(name) {
|
|
2628
|
+
const query = "mutation createGroup($group:String!){createGroup(name:$group){id}}";
|
|
2629
|
+
await this.client.query(query, { group: name });
|
|
2630
|
+
console.log(`Created group: ${name}`);
|
|
2631
|
+
}
|
|
2632
|
+
async deleteGroup(name) {
|
|
2633
|
+
const groupId = await this.getGroupId(name);
|
|
2634
|
+
const query = "mutation deleteGroup($id:Int!){deleteGroup(groupId:$id){ok}}";
|
|
2635
|
+
const data = await this.client.query(query, { id: groupId });
|
|
2636
|
+
if (data.deleteGroup.ok) {
|
|
2637
|
+
console.log(`Deleted group: ${name}`);
|
|
2638
|
+
} else {
|
|
2639
|
+
throw new Error(`Failed to delete group: ${name}`);
|
|
2640
|
+
}
|
|
2641
|
+
}
|
|
2642
|
+
async getSchemaAttribute(attribute) {
|
|
2643
|
+
const query = "{schema{groupSchema{attributes{name,attributeType,isList,isVisible,isEditable}}}}";
|
|
2644
|
+
const data = await this.client.query(query);
|
|
2645
|
+
return data.schema.groupSchema.attributes.find((a) => a.name === attribute) || null;
|
|
2646
|
+
}
|
|
2647
|
+
async updateGroupAttribute(mutation, groupName, attribute, value) {
|
|
2648
|
+
const groupId = await this.getGroupId(groupName);
|
|
2649
|
+
const schemaAttr = await this.getSchemaAttribute(attribute);
|
|
2650
|
+
if (!schemaAttr) {
|
|
2651
|
+
throw new Error(`Attribute ${attribute} is not part of group schema.`);
|
|
2652
|
+
}
|
|
2653
|
+
const isList = schemaAttr.isList;
|
|
2654
|
+
if (mutation === "set" && isList) {
|
|
2655
|
+
throw new Error(`Attribute ${attribute} is a list and cannot be modified using the ${mutation} mutation.`);
|
|
2656
|
+
}
|
|
2657
|
+
if ((mutation === "add" || mutation === "del") && !isList) {
|
|
2658
|
+
throw new Error(`Attribute ${attribute} is not a list and cannot be modified using the ${mutation} mutation.`);
|
|
2659
|
+
}
|
|
2660
|
+
const query = "mutation updateGroup($group:UpdateGroupInput!){updateGroup(group:$group){ok}}";
|
|
2661
|
+
let variables;
|
|
2662
|
+
let resultLine;
|
|
2663
|
+
switch (mutation) {
|
|
2664
|
+
case "set":
|
|
2665
|
+
variables = {
|
|
2666
|
+
group: {
|
|
2667
|
+
id: groupId,
|
|
2668
|
+
insertAttributes: { name: attribute, value: value || "" }
|
|
2669
|
+
}
|
|
2670
|
+
};
|
|
2671
|
+
resultLine = `Attribute set for group: ${groupName}, attribute: ${attribute}`;
|
|
2672
|
+
if (value && schemaAttr.attributeType !== "JPEG_PHOTO") {
|
|
2673
|
+
resultLine += `, value: ${value}`;
|
|
2674
|
+
}
|
|
2675
|
+
break;
|
|
2676
|
+
case "clear": {
|
|
2677
|
+
const currentValues = await this.getGroupAttributeValues(groupId, attribute);
|
|
2678
|
+
if (currentValues.length === 0) {
|
|
2679
|
+
throw new Error(`Attribute ${attribute} has no value set for group ${groupName}, so nothing to clear.`);
|
|
2680
|
+
}
|
|
2681
|
+
variables = {
|
|
2682
|
+
group: {
|
|
2683
|
+
id: groupId,
|
|
2684
|
+
removeAttributes: attribute
|
|
2685
|
+
}
|
|
2686
|
+
};
|
|
2687
|
+
resultLine = `Attribute cleared for group: ${groupName}, attribute: ${attribute}`;
|
|
2688
|
+
break;
|
|
2689
|
+
}
|
|
2690
|
+
case "add": {
|
|
2691
|
+
if (!value) {
|
|
2692
|
+
throw new Error("Value is required for add mutation");
|
|
2693
|
+
}
|
|
2694
|
+
const currentValues = await this.getGroupAttributeValues(groupId, attribute);
|
|
2695
|
+
const newValues = [...currentValues, value];
|
|
2696
|
+
variables = {
|
|
2697
|
+
group: {
|
|
2698
|
+
id: groupId,
|
|
2699
|
+
insertAttributes: { name: attribute, value: newValues }
|
|
2700
|
+
}
|
|
2701
|
+
};
|
|
2702
|
+
resultLine = `Attribute list value added for group: ${groupName}, attribute: ${attribute}, value: ${value}`;
|
|
2703
|
+
break;
|
|
2704
|
+
}
|
|
2705
|
+
case "del": {
|
|
2706
|
+
if (!value) {
|
|
2707
|
+
throw new Error("Value is required for del mutation");
|
|
2708
|
+
}
|
|
2709
|
+
const currentValues = await this.getGroupAttributeValues(groupId, attribute);
|
|
2710
|
+
if (!currentValues.includes(value)) {
|
|
2711
|
+
throw new Error(`Attribute ${attribute} has no listed value ${value} for group ${groupName}, so no value to delete.`);
|
|
2712
|
+
}
|
|
2713
|
+
const newValues = currentValues.filter((v) => v !== value);
|
|
2714
|
+
if (newValues.length === 0) {
|
|
2715
|
+
return this.updateGroupAttribute("clear", groupName, attribute);
|
|
2716
|
+
}
|
|
2717
|
+
variables = {
|
|
2718
|
+
group: {
|
|
2719
|
+
id: groupId,
|
|
2720
|
+
insertAttributes: { name: attribute, value: newValues }
|
|
2721
|
+
}
|
|
2722
|
+
};
|
|
2723
|
+
resultLine = `Attribute list value deleted for group: ${groupName}, attribute: ${attribute}, value: ${value}`;
|
|
2724
|
+
break;
|
|
2725
|
+
}
|
|
2726
|
+
default:
|
|
2727
|
+
throw new Error(`Unknown mutation type: ${mutation}`);
|
|
2728
|
+
}
|
|
2729
|
+
await this.client.query(query, variables);
|
|
2730
|
+
console.log(resultLine);
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2733
|
+
|
|
2734
|
+
// src/schema.ts
|
|
2735
|
+
class SchemaService {
|
|
2736
|
+
client;
|
|
2737
|
+
constructor(client) {
|
|
2738
|
+
this.client = client;
|
|
2739
|
+
}
|
|
2740
|
+
async getUserAttributes() {
|
|
2741
|
+
const query = "{schema{userSchema{attributes{name,attributeType,isList,isVisible,isEditable}}}}";
|
|
2742
|
+
const data = await this.client.query(query);
|
|
2743
|
+
return data.schema.userSchema.attributes;
|
|
2744
|
+
}
|
|
2745
|
+
async getUserAttributeType(name) {
|
|
2746
|
+
const attributes = await this.getUserAttributes();
|
|
2747
|
+
const attr = attributes.find((a) => a.name === name);
|
|
2748
|
+
if (!attr) {
|
|
2749
|
+
throw new Error(`Attribute ${name} is not part of user schema.`);
|
|
2750
|
+
}
|
|
2751
|
+
return attr.attributeType;
|
|
2752
|
+
}
|
|
2753
|
+
async isUserAttributeList(name) {
|
|
2754
|
+
const attributes = await this.getUserAttributes();
|
|
2755
|
+
const attr = attributes.find((a) => a.name === name);
|
|
2756
|
+
if (!attr) {
|
|
2757
|
+
throw new Error(`Attribute ${name} is not part of user schema.`);
|
|
2758
|
+
}
|
|
2759
|
+
return attr.isList;
|
|
2760
|
+
}
|
|
2761
|
+
async addUserAttribute(name, type, options = {}) {
|
|
2762
|
+
const query = `mutation addUserAttribute($name:String!,$type:AttributeType!,$isList:Boolean!,$isVisible:Boolean!,$isEditable:Boolean!){
|
|
2763
|
+
addUserAttribute(name:$name,attributeType:$type,isList:$isList,isVisible:$isVisible,isEditable:$isEditable){ok}
|
|
2764
|
+
}`;
|
|
2765
|
+
const variables = {
|
|
2766
|
+
name,
|
|
2767
|
+
type,
|
|
2768
|
+
isList: options.isList ?? false,
|
|
2769
|
+
isVisible: options.isVisible ?? false,
|
|
2770
|
+
isEditable: options.isEditable ?? false
|
|
2771
|
+
};
|
|
2772
|
+
const data = await this.client.query(query, variables);
|
|
2773
|
+
if (data.addUserAttribute.ok) {
|
|
2774
|
+
console.log(`Added in schema new user attribute: ${name}`);
|
|
2775
|
+
}
|
|
2776
|
+
}
|
|
2777
|
+
async deleteUserAttribute(name) {
|
|
2778
|
+
const query = "mutation deleteUserAttribute($name:String!){deleteUserAttribute(name:$name){ok}}";
|
|
2779
|
+
const data = await this.client.query(query, { name });
|
|
2780
|
+
if (data.deleteUserAttribute.ok) {
|
|
2781
|
+
console.log(`Deleted from schema user attribute: ${name}`);
|
|
2782
|
+
}
|
|
2783
|
+
}
|
|
2784
|
+
async getGroupAttributes() {
|
|
2785
|
+
const query = "{schema{groupSchema{attributes{name,attributeType,isList,isVisible,isEditable}}}}";
|
|
2786
|
+
const data = await this.client.query(query);
|
|
2787
|
+
return data.schema.groupSchema.attributes;
|
|
2788
|
+
}
|
|
2789
|
+
async getGroupAttributeType(name) {
|
|
2790
|
+
const attributes = await this.getGroupAttributes();
|
|
2791
|
+
const attr = attributes.find((a) => a.name === name);
|
|
2792
|
+
if (!attr) {
|
|
2793
|
+
throw new Error(`Attribute ${name} is not part of group schema.`);
|
|
2794
|
+
}
|
|
2795
|
+
return attr.attributeType;
|
|
2796
|
+
}
|
|
2797
|
+
async isGroupAttributeList(name) {
|
|
2798
|
+
const attributes = await this.getGroupAttributes();
|
|
2799
|
+
const attr = attributes.find((a) => a.name === name);
|
|
2800
|
+
if (!attr) {
|
|
2801
|
+
throw new Error(`Attribute ${name} is not part of group schema.`);
|
|
2802
|
+
}
|
|
2803
|
+
return attr.isList;
|
|
2804
|
+
}
|
|
2805
|
+
async addGroupAttribute(name, type, options = {}) {
|
|
2806
|
+
const query = `mutation addGroupAttribute($name:String!,$type:AttributeType!,$isList:Boolean!,$isVisible:Boolean!,$isEditable:Boolean!){
|
|
2807
|
+
addGroupAttribute(name:$name,attributeType:$type,isList:$isList,isVisible:$isVisible,isEditable:$isEditable){ok}
|
|
2808
|
+
}`;
|
|
2809
|
+
const variables = {
|
|
2810
|
+
name,
|
|
2811
|
+
type,
|
|
2812
|
+
isList: options.isList ?? false,
|
|
2813
|
+
isVisible: options.isVisible ?? false,
|
|
2814
|
+
isEditable: options.isEditable ?? false
|
|
2815
|
+
};
|
|
2816
|
+
const data = await this.client.query(query, variables);
|
|
2817
|
+
if (data.addGroupAttribute.ok) {
|
|
2818
|
+
console.log(`Added in schema new group attribute: ${name}`);
|
|
2819
|
+
}
|
|
2820
|
+
}
|
|
2821
|
+
async deleteGroupAttribute(name) {
|
|
2822
|
+
const query = "mutation deleteGroupAttribute($name:String!){deleteGroupAttribute(name:$name){ok}}";
|
|
2823
|
+
const data = await this.client.query(query, { name });
|
|
2824
|
+
if (data.deleteGroupAttribute.ok) {
|
|
2825
|
+
console.log(`Deleted from schema group attribute: ${name}`);
|
|
2826
|
+
}
|
|
2827
|
+
}
|
|
2828
|
+
async listUserObjectClasses() {
|
|
2829
|
+
const query = "{schema{userSchema{extraLdapObjectClasses}}}";
|
|
2830
|
+
const data = await this.client.query(query);
|
|
2831
|
+
return data.schema.userSchema.extraLdapObjectClasses;
|
|
2832
|
+
}
|
|
2833
|
+
async addUserObjectClass(name) {
|
|
2834
|
+
const query = "mutation addUserObjectClass($name:String!){addUserObjectClass(name:$name){ok}}";
|
|
2835
|
+
const data = await this.client.query(query, { name });
|
|
2836
|
+
if (data.addUserObjectClass.ok) {
|
|
2837
|
+
console.log(`Defined in schema new LDAP extra user object class: ${name}`);
|
|
2838
|
+
}
|
|
2839
|
+
}
|
|
2840
|
+
async deleteUserObjectClass(name) {
|
|
2841
|
+
const query = "mutation deleteUserObjectClass($name:String!){deleteUserObjectClass(name:$name){ok}}";
|
|
2842
|
+
const data = await this.client.query(query, { name });
|
|
2843
|
+
if (data.deleteUserObjectClass.ok) {
|
|
2844
|
+
console.log(`Deleted from schema LDAP extra user object class: ${name}`);
|
|
2845
|
+
}
|
|
2846
|
+
}
|
|
2847
|
+
async listGroupObjectClasses() {
|
|
2848
|
+
const query = "{schema{groupSchema{extraLdapObjectClasses}}}";
|
|
2849
|
+
const data = await this.client.query(query);
|
|
2850
|
+
return data.schema.groupSchema.extraLdapObjectClasses;
|
|
2851
|
+
}
|
|
2852
|
+
async addGroupObjectClass(name) {
|
|
2853
|
+
const query = "mutation addGroupObjectClass($name:String!){addGroupObjectClass(name:$name){ok}}";
|
|
2854
|
+
const data = await this.client.query(query, { name });
|
|
2855
|
+
if (data.addGroupObjectClass.ok) {
|
|
2856
|
+
console.log(`Defined in schema new LDAP extra group object class: ${name}`);
|
|
2857
|
+
}
|
|
2858
|
+
}
|
|
2859
|
+
async deleteGroupObjectClass(name) {
|
|
2860
|
+
const query = "mutation deleteGroupObjectClass($name:String!){deleteGroupObjectClass(name:$name){ok}}";
|
|
2861
|
+
const data = await this.client.query(query, { name });
|
|
2862
|
+
if (data.deleteGroupObjectClass.ok) {
|
|
2863
|
+
console.log(`Deleted from schema LDAP extra group object class: ${name}`);
|
|
2864
|
+
}
|
|
2865
|
+
}
|
|
2866
|
+
}
|
|
2867
|
+
|
|
2868
|
+
// src/formatters.ts
|
|
2869
|
+
function formatUsersTable(users) {
|
|
2870
|
+
const headers = ["User ID (user_id)", "Email (mail)", "Display Name (display_name)"];
|
|
2871
|
+
const rows = users.map((u) => [u.id, u.email, u.displayName]);
|
|
2872
|
+
return formatTable(headers, rows);
|
|
2873
|
+
}
|
|
2874
|
+
function formatGroupsTable(groups) {
|
|
2875
|
+
const headers = ["Group ID", "Creation date", "UUID", "Display Name"];
|
|
2876
|
+
const rows = groups.map((g) => [String(g.id), g.creationDate, g.uuid, g.displayName]);
|
|
2877
|
+
return formatTable(headers, rows);
|
|
2878
|
+
}
|
|
2879
|
+
function formatSchemaAttributesTable(attributes) {
|
|
2880
|
+
const headers = ["Name", "Type", "Is list", "Is visible", "Is editable"];
|
|
2881
|
+
const rows = attributes.map((a) => [
|
|
2882
|
+
a.name,
|
|
2883
|
+
a.attributeType,
|
|
2884
|
+
String(a.isList),
|
|
2885
|
+
String(a.isVisible),
|
|
2886
|
+
String(a.isEditable)
|
|
2887
|
+
]);
|
|
2888
|
+
return formatTable(headers, rows);
|
|
2889
|
+
}
|
|
2890
|
+
function formatTable(headers, rows) {
|
|
2891
|
+
const allRows = [headers, ...rows];
|
|
2892
|
+
const widths = headers.map((_, i) => Math.max(...allRows.map((row) => (row[i] || "").length)));
|
|
2893
|
+
const headerLine = headers.map((h, i) => h.padEnd(widths[i])).join(" ");
|
|
2894
|
+
const separatorLine = widths.map((w) => "-".repeat(w)).join(" ");
|
|
2895
|
+
const dataLines = rows.map((row) => row.map((cell, i) => (cell || "").padEnd(widths[i])).join(" "));
|
|
2896
|
+
return [headerLine, separatorLine, ...dataLines].join(`
|
|
2897
|
+
`);
|
|
2898
|
+
}
|
|
2899
|
+
function formatList(items) {
|
|
2900
|
+
return items.join(`
|
|
2901
|
+
`);
|
|
2902
|
+
}
|
|
2903
|
+
|
|
2904
|
+
// src/errors.ts
|
|
2905
|
+
class CliError2 extends Error {
|
|
2906
|
+
exitCode;
|
|
2907
|
+
constructor(message, exitCode = ExitCode.ERROR) {
|
|
2908
|
+
super(message);
|
|
2909
|
+
this.name = "CliError";
|
|
2910
|
+
this.exitCode = exitCode;
|
|
2911
|
+
}
|
|
2912
|
+
}
|
|
2913
|
+
|
|
2914
|
+
// src/exitCodes.ts
|
|
2915
|
+
var ExitCode2 = {
|
|
2916
|
+
SUCCESS: 0,
|
|
2917
|
+
ERROR: 1,
|
|
2918
|
+
USAGE: 64,
|
|
2919
|
+
DATAERR: 65,
|
|
2920
|
+
NOINPUT: 66,
|
|
2921
|
+
NOUSER: 67,
|
|
2922
|
+
NOHOST: 68,
|
|
2923
|
+
UNAVAILABLE: 69,
|
|
2924
|
+
SOFTWARE: 70,
|
|
2925
|
+
OSERR: 71,
|
|
2926
|
+
OSFILE: 72,
|
|
2927
|
+
CANTCREAT: 73,
|
|
2928
|
+
IOERR: 74,
|
|
2929
|
+
TEMPFAIL: 75,
|
|
2930
|
+
PROTOCOL: 76,
|
|
2931
|
+
NOPERM: 77,
|
|
2932
|
+
CONFIG: 78
|
|
2933
|
+
};
|
|
2934
|
+
// package.json
|
|
2935
|
+
var package_default = {
|
|
2936
|
+
name: "lldap-cli",
|
|
2937
|
+
version: "1.0.3",
|
|
2938
|
+
description: "CLI tool for managing LLDAP (Lightweight LDAP) users, groups, and schema",
|
|
2939
|
+
type: "module",
|
|
2940
|
+
bin: {
|
|
2941
|
+
"lldap-cli": "./dist/cli.js"
|
|
2942
|
+
},
|
|
2943
|
+
files: [
|
|
2944
|
+
"dist",
|
|
2945
|
+
"README.md",
|
|
2946
|
+
"LICENSE"
|
|
2947
|
+
],
|
|
2948
|
+
scripts: {
|
|
2949
|
+
build: "bun build ./src/cli.ts --outdir ./dist --target node",
|
|
2950
|
+
dev: "bun run ./src/cli.ts",
|
|
2951
|
+
lint: "eslint src tests",
|
|
2952
|
+
"lint:fix": "eslint src tests --fix",
|
|
2953
|
+
test: "bun test",
|
|
2954
|
+
"test:watch": "bun test --watch",
|
|
2955
|
+
typecheck: "tsc --noEmit",
|
|
2956
|
+
prepublishOnly: "bun run build"
|
|
2957
|
+
},
|
|
2958
|
+
dependencies: {
|
|
2959
|
+
commander: "^12.1.0"
|
|
2960
|
+
},
|
|
2961
|
+
devDependencies: {
|
|
2962
|
+
"@eslint/js": "^9.0.0",
|
|
2963
|
+
"@types/bun": "^1.1.14",
|
|
2964
|
+
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
2965
|
+
"@typescript-eslint/parser": "^8.0.0",
|
|
2966
|
+
eslint: "^9.0.0",
|
|
2967
|
+
typescript: "^5.7.2",
|
|
2968
|
+
"typescript-eslint": "^8.0.0"
|
|
2969
|
+
},
|
|
2970
|
+
keywords: [
|
|
2971
|
+
"lldap",
|
|
2972
|
+
"ldap",
|
|
2973
|
+
"cli",
|
|
2974
|
+
"user-management",
|
|
2975
|
+
"graphql",
|
|
2976
|
+
"bun",
|
|
2977
|
+
"typescript"
|
|
2978
|
+
],
|
|
2979
|
+
author: "Stephen Eaton",
|
|
2980
|
+
license: "MIT",
|
|
2981
|
+
repository: {
|
|
2982
|
+
type: "git",
|
|
2983
|
+
url: "git+https://github.com/madeinoz67/lldap-cli.git"
|
|
2984
|
+
},
|
|
2985
|
+
homepage: "https://github.com/madeinoz67/lldap-cli#readme",
|
|
2986
|
+
bugs: {
|
|
2987
|
+
url: "https://github.com/madeinoz67/lldap-cli/issues"
|
|
2988
|
+
},
|
|
2989
|
+
engines: {
|
|
2990
|
+
node: ">=18.0.0"
|
|
2991
|
+
}
|
|
2992
|
+
};
|
|
2993
|
+
|
|
2994
|
+
// src/cli.ts
|
|
2995
|
+
function loadEnvFile() {
|
|
2996
|
+
const envPath = ".env";
|
|
2997
|
+
if (existsSync3(envPath)) {
|
|
2998
|
+
const content = readFileSync3(envPath, "utf-8");
|
|
2999
|
+
for (const line of content.split(`
|
|
3000
|
+
`)) {
|
|
3001
|
+
const trimmed = line.trim();
|
|
3002
|
+
if (!trimmed || trimmed.startsWith("#"))
|
|
3003
|
+
continue;
|
|
3004
|
+
const match = trimmed.match(/^([^=]+)=(.*)$/);
|
|
3005
|
+
if (match) {
|
|
3006
|
+
const [, key, value] = match;
|
|
3007
|
+
if (!process.env[key.trim()]) {
|
|
3008
|
+
const cleanValue = value.trim().replace(/^["']|["']$/g, "");
|
|
3009
|
+
process.env[key.trim()] = cleanValue;
|
|
3010
|
+
}
|
|
3011
|
+
}
|
|
3012
|
+
}
|
|
3013
|
+
}
|
|
3014
|
+
}
|
|
3015
|
+
loadEnvFile();
|
|
3016
|
+
var program2 = new Command;
|
|
3017
|
+
var APP_NAME = package_default.name;
|
|
3018
|
+
var APP_VERSION = package_default.version;
|
|
3019
|
+
var APP_AUTHOR = package_default.author;
|
|
3020
|
+
var globalOptions = {};
|
|
3021
|
+
var quietMode = false;
|
|
3022
|
+
function printHeader() {
|
|
3023
|
+
if (!quietMode) {
|
|
3024
|
+
console.error(`${APP_NAME} v${APP_VERSION} - ${APP_AUTHOR}`);
|
|
3025
|
+
console.error("");
|
|
3026
|
+
}
|
|
3027
|
+
}
|
|
3028
|
+
program2.name(APP_NAME).description("CLI tool for managing LLDAP (Lightweight LDAP) users, groups, and schema").version(APP_VERSION, "-V, --version", "Output the version number").addHelpText("beforeAll", `${APP_NAME} v${APP_VERSION} - ${APP_AUTHOR}
|
|
3029
|
+
`).option("-H, --http-url <url>", "HTTP base URL of the LLDAP management interface").option("-u, --username <username>", "Username of the admin account").option("-t, --token <token>", "Authentication token (prefer LLDAP_TOKEN env var)").option("-r, --refresh-token <token>", "Refresh token (prefer LLDAP_REFRESHTOKEN env var)").option("-q, --quiet", "Suppress header and non-essential output").option("--debug", "Enable debug output (WARNING: may expose sensitive info in logs)").hook("preAction", async (thisCommand) => {
|
|
3030
|
+
const opts = thisCommand.opts();
|
|
3031
|
+
quietMode = opts.quiet || false;
|
|
3032
|
+
printHeader();
|
|
3033
|
+
globalOptions = {
|
|
3034
|
+
httpUrl: opts.httpUrl,
|
|
3035
|
+
username: opts.username,
|
|
3036
|
+
token: opts.token,
|
|
3037
|
+
refreshToken: opts.refreshToken
|
|
3038
|
+
};
|
|
3039
|
+
if (opts.debug) {
|
|
3040
|
+
console.error("WARNING: Debug mode enabled. Output may contain sensitive information.");
|
|
3041
|
+
console.error("Do not use in production or share debug output publicly.");
|
|
3042
|
+
LldapClient.setDebugEnabled(true);
|
|
3043
|
+
}
|
|
3044
|
+
});
|
|
3045
|
+
program2.command("login").description("Authenticate and print tokens for subsequent commands").option("-p, --prompt-password", "Prompt for password (recommended)").option("-w, --password <password>", "Password (less secure than -p)").option("-o, --output <file>", "Write tokens to file instead of stdout (more secure)").option("-q, --quiet", "Suppress security warnings").action(async (opts) => {
|
|
3046
|
+
try {
|
|
3047
|
+
if (opts.promptPassword) {
|
|
3048
|
+
process.stderr.write("Login password: ");
|
|
3049
|
+
const pwd = await readPassword();
|
|
3050
|
+
if (!pwd) {
|
|
3051
|
+
console.error("ERROR: No password provided");
|
|
3052
|
+
process.exit(ExitCode2.USAGE);
|
|
3053
|
+
}
|
|
3054
|
+
globalOptions.password = pwd;
|
|
3055
|
+
} else if (opts.password) {
|
|
3056
|
+
globalOptions.password = opts.password;
|
|
3057
|
+
}
|
|
3058
|
+
const config = buildConfig(globalOptions);
|
|
3059
|
+
const client = new LldapClient(config);
|
|
3060
|
+
let token;
|
|
3061
|
+
let refreshToken;
|
|
3062
|
+
if (config.refreshToken && !config.password) {
|
|
3063
|
+
token = await client.refresh();
|
|
3064
|
+
refreshToken = config.refreshToken;
|
|
3065
|
+
} else {
|
|
3066
|
+
const tokens = await client.login();
|
|
3067
|
+
token = tokens.token;
|
|
3068
|
+
refreshToken = tokens.refreshToken;
|
|
3069
|
+
}
|
|
3070
|
+
const output = `export LLDAP_TOKEN=${token}
|
|
3071
|
+
export LLDAP_REFRESHTOKEN=${refreshToken}
|
|
3072
|
+
`;
|
|
3073
|
+
if (opts.output) {
|
|
3074
|
+
const fs = await import("fs");
|
|
3075
|
+
fs.writeFileSync(opts.output, output, { mode: 384 });
|
|
3076
|
+
console.error(`Tokens written to ${opts.output} (mode 600)`);
|
|
3077
|
+
console.error(`Source with: source ${opts.output}`);
|
|
3078
|
+
} else {
|
|
3079
|
+
if (!opts.quiet) {
|
|
3080
|
+
console.error("WARNING: Tokens will be displayed. Consider using -o <file> for better security.");
|
|
3081
|
+
console.error("Suppress this warning with -q or --quiet");
|
|
3082
|
+
}
|
|
3083
|
+
console.log(output.trim());
|
|
3084
|
+
}
|
|
3085
|
+
} catch (error) {
|
|
3086
|
+
handleError(error);
|
|
3087
|
+
}
|
|
3088
|
+
});
|
|
3089
|
+
program2.command("logout").description("Invalidate refresh token and associated tokens").action(async () => {
|
|
3090
|
+
try {
|
|
3091
|
+
const config = buildConfig(globalOptions);
|
|
3092
|
+
if (!config.refreshToken) {
|
|
3093
|
+
console.error("ERROR: A refresh token is not available for logout.");
|
|
3094
|
+
process.exit(ExitCode2.CONFIG);
|
|
3095
|
+
}
|
|
3096
|
+
const client = new LldapClient(config);
|
|
3097
|
+
await client.logout();
|
|
3098
|
+
console.log("unset LLDAP_TOKEN");
|
|
3099
|
+
console.log("unset LLDAP_REFRESHTOKEN");
|
|
3100
|
+
console.error("Refresh token and any associated tokens are invalidated.");
|
|
3101
|
+
} catch (error) {
|
|
3102
|
+
handleError(error);
|
|
3103
|
+
}
|
|
3104
|
+
});
|
|
3105
|
+
var userCommand = program2.command("user").description("User management commands");
|
|
3106
|
+
userCommand.command("list [field]").description("List users (field: uid, email, or all)").option("-g, --group <name>", "Filter by group membership").action(async (field = "uid", opts) => {
|
|
3107
|
+
try {
|
|
3108
|
+
const config = buildConfig(globalOptions);
|
|
3109
|
+
const client = new LldapClient(config);
|
|
3110
|
+
const userService = new UserService(client);
|
|
3111
|
+
let users = opts.group ? await userService.getUsersByGroup(opts.group) : await userService.getUsers();
|
|
3112
|
+
let output;
|
|
3113
|
+
if (field === "uid") {
|
|
3114
|
+
output = formatList(users.map((u) => u.id));
|
|
3115
|
+
} else if (field === "email") {
|
|
3116
|
+
output = formatList(users.map((u) => u.email).sort());
|
|
3117
|
+
} else if (field === "all") {
|
|
3118
|
+
output = formatUsersTable(users);
|
|
3119
|
+
} else {
|
|
3120
|
+
console.error(`ERROR: Unknown field '${field}'. Use: uid, email, or all`);
|
|
3121
|
+
process.exit(ExitCode2.USAGE);
|
|
3122
|
+
}
|
|
3123
|
+
console.log(output);
|
|
3124
|
+
await client.cleanup();
|
|
3125
|
+
} catch (error) {
|
|
3126
|
+
handleError(error);
|
|
3127
|
+
}
|
|
3128
|
+
});
|
|
3129
|
+
userCommand.command("search <pattern>").description("Search users by pattern (uid, email, or display name). Supports * and ? wildcards").action(async (pattern) => {
|
|
3130
|
+
try {
|
|
3131
|
+
const config = buildConfig(globalOptions);
|
|
3132
|
+
const client = new LldapClient(config);
|
|
3133
|
+
const userService = new UserService(client);
|
|
3134
|
+
const users = await userService.searchUsers(pattern);
|
|
3135
|
+
if (users.length === 0) {
|
|
3136
|
+
console.error(`No users found matching: ${pattern}`);
|
|
3137
|
+
process.exit(ExitCode2.SUCCESS);
|
|
3138
|
+
}
|
|
3139
|
+
console.log(formatUsersTable(users));
|
|
3140
|
+
await client.cleanup();
|
|
3141
|
+
} catch (error) {
|
|
3142
|
+
handleError(error);
|
|
3143
|
+
}
|
|
3144
|
+
});
|
|
3145
|
+
userCommand.command("add <uid> <email>").description("Create a new user").option("-P, --prompt-password", "Prompt for user password (recommended)").option("-d, --display-name <name>", "Display name").option("-f, --first-name <name>", "First name").option("-l, --last-name <name>", "Last name").option("-a, --avatar <file>", "Avatar image file").action(async (uid, email, opts) => {
|
|
3146
|
+
try {
|
|
3147
|
+
const config = buildConfig(globalOptions);
|
|
3148
|
+
const client = new LldapClient(config);
|
|
3149
|
+
const userService = new UserService(client);
|
|
3150
|
+
let userPassword;
|
|
3151
|
+
if (opts.promptPassword) {
|
|
3152
|
+
process.stdout.write("New user password: ");
|
|
3153
|
+
userPassword = await readPassword();
|
|
3154
|
+
}
|
|
3155
|
+
await userService.createUser(uid, email, {
|
|
3156
|
+
displayName: opts.displayName,
|
|
3157
|
+
firstName: opts.firstName,
|
|
3158
|
+
lastName: opts.lastName,
|
|
3159
|
+
avatar: opts.avatar
|
|
3160
|
+
});
|
|
3161
|
+
if (userPassword) {
|
|
3162
|
+
const token = client.getToken();
|
|
3163
|
+
if (token) {
|
|
3164
|
+
await userService.setPassword(uid, userPassword, config.httpUrl, token);
|
|
3165
|
+
}
|
|
3166
|
+
}
|
|
3167
|
+
await client.cleanup();
|
|
3168
|
+
} catch (error) {
|
|
3169
|
+
handleError(error);
|
|
3170
|
+
}
|
|
3171
|
+
});
|
|
3172
|
+
userCommand.command("del <uid>").description("Delete a user").action(async (uid) => {
|
|
3173
|
+
try {
|
|
3174
|
+
const config = buildConfig(globalOptions);
|
|
3175
|
+
const client = new LldapClient(config);
|
|
3176
|
+
const userService = new UserService(client);
|
|
3177
|
+
const resolvedUid = await userService.resolveUserId(uid);
|
|
3178
|
+
await userService.deleteUser(resolvedUid);
|
|
3179
|
+
await client.cleanup();
|
|
3180
|
+
} catch (error) {
|
|
3181
|
+
handleError(error);
|
|
3182
|
+
}
|
|
3183
|
+
});
|
|
3184
|
+
userCommand.command("update <mutation> <uid> <attribute> [value]").description("Update a user attribute (mutation: set, clear, add, del)").action(async (mutation, uid, attribute, value) => {
|
|
3185
|
+
try {
|
|
3186
|
+
const config = buildConfig(globalOptions);
|
|
3187
|
+
const client = new LldapClient(config);
|
|
3188
|
+
const userService = new UserService(client);
|
|
3189
|
+
const resolvedUid = await userService.resolveUserId(uid);
|
|
3190
|
+
if (attribute === "password") {
|
|
3191
|
+
if (mutation !== "set") {
|
|
3192
|
+
console.error(`ERROR: Mutation ${mutation} not supported for attribute password. Use set instead.`);
|
|
3193
|
+
process.exit(ExitCode2.USAGE);
|
|
3194
|
+
}
|
|
3195
|
+
let password = value;
|
|
3196
|
+
if (!password) {
|
|
3197
|
+
process.stdout.write("New user password: ");
|
|
3198
|
+
password = await readPassword();
|
|
3199
|
+
}
|
|
3200
|
+
const token = client.getToken();
|
|
3201
|
+
if (token) {
|
|
3202
|
+
await userService.setPassword(resolvedUid, password, config.httpUrl, token);
|
|
3203
|
+
}
|
|
3204
|
+
} else {
|
|
3205
|
+
await userService.updateUserAttribute(mutation, resolvedUid, attribute, value);
|
|
3206
|
+
}
|
|
3207
|
+
await client.cleanup();
|
|
3208
|
+
} catch (error) {
|
|
3209
|
+
handleError(error);
|
|
3210
|
+
}
|
|
3211
|
+
});
|
|
3212
|
+
userCommand.command("info [uid]").description("Show user information").action(async (uid) => {
|
|
3213
|
+
try {
|
|
3214
|
+
const config = buildConfig(globalOptions);
|
|
3215
|
+
const client = new LldapClient(config);
|
|
3216
|
+
const userService = new UserService(client);
|
|
3217
|
+
const users = await userService.getUsers();
|
|
3218
|
+
if (uid) {
|
|
3219
|
+
const resolvedUid = await userService.resolveUserId(uid);
|
|
3220
|
+
const filteredUsers = users.filter((u) => u.id === resolvedUid);
|
|
3221
|
+
console.log(formatUsersTable(filteredUsers));
|
|
3222
|
+
} else {
|
|
3223
|
+
console.log(formatUsersTable(users));
|
|
3224
|
+
}
|
|
3225
|
+
await client.cleanup();
|
|
3226
|
+
} catch (error) {
|
|
3227
|
+
handleError(error);
|
|
3228
|
+
}
|
|
3229
|
+
});
|
|
3230
|
+
var userAttributeCommand = userCommand.command("attribute").description("User attribute commands");
|
|
3231
|
+
userAttributeCommand.command("list <uid>").description("List attributes for a user").action(async (uid) => {
|
|
3232
|
+
try {
|
|
3233
|
+
const config = buildConfig(globalOptions);
|
|
3234
|
+
const client = new LldapClient(config);
|
|
3235
|
+
const userService = new UserService(client);
|
|
3236
|
+
const resolvedUid = await userService.resolveUserId(uid);
|
|
3237
|
+
const attributes = await userService.listUserAttributes(resolvedUid);
|
|
3238
|
+
console.log(formatList(attributes));
|
|
3239
|
+
await client.cleanup();
|
|
3240
|
+
} catch (error) {
|
|
3241
|
+
handleError(error);
|
|
3242
|
+
}
|
|
3243
|
+
});
|
|
3244
|
+
userAttributeCommand.command("values <uid> <attribute>").description("Get values for a user attribute").action(async (uid, attribute) => {
|
|
3245
|
+
try {
|
|
3246
|
+
const config = buildConfig(globalOptions);
|
|
3247
|
+
const client = new LldapClient(config);
|
|
3248
|
+
const userService = new UserService(client);
|
|
3249
|
+
const resolvedUid = await userService.resolveUserId(uid);
|
|
3250
|
+
const values = await userService.getUserAttributeValues(resolvedUid, attribute);
|
|
3251
|
+
console.log(formatList(values));
|
|
3252
|
+
await client.cleanup();
|
|
3253
|
+
} catch (error) {
|
|
3254
|
+
handleError(error);
|
|
3255
|
+
}
|
|
3256
|
+
});
|
|
3257
|
+
var userGroupCommand = userCommand.command("group").description("User group membership commands");
|
|
3258
|
+
userGroupCommand.command("add <uid> <groupName>").description("Add user to a group").action(async (uid, groupName) => {
|
|
3259
|
+
try {
|
|
3260
|
+
const config = buildConfig(globalOptions);
|
|
3261
|
+
const client = new LldapClient(config);
|
|
3262
|
+
const userService = new UserService(client);
|
|
3263
|
+
const groupService = new GroupService(client);
|
|
3264
|
+
const resolvedUid = await userService.resolveUserId(uid);
|
|
3265
|
+
const groupId = await groupService.getGroupId(groupName);
|
|
3266
|
+
await userService.addToGroup(resolvedUid, groupId);
|
|
3267
|
+
console.log(`Added user ${resolvedUid} to group ${groupName}`);
|
|
3268
|
+
await client.cleanup();
|
|
3269
|
+
} catch (error) {
|
|
3270
|
+
handleError(error);
|
|
3271
|
+
}
|
|
3272
|
+
});
|
|
3273
|
+
userGroupCommand.command("del <uid> <groupName>").description("Remove user from a group").action(async (uid, groupName) => {
|
|
3274
|
+
try {
|
|
3275
|
+
const config = buildConfig(globalOptions);
|
|
3276
|
+
const client = new LldapClient(config);
|
|
3277
|
+
const userService = new UserService(client);
|
|
3278
|
+
const groupService = new GroupService(client);
|
|
3279
|
+
const resolvedUid = await userService.resolveUserId(uid);
|
|
3280
|
+
const groupId = await groupService.getGroupId(groupName);
|
|
3281
|
+
await userService.removeFromGroup(resolvedUid, groupId);
|
|
3282
|
+
console.log(`Removed user ${resolvedUid} from group ${groupName}`);
|
|
3283
|
+
await client.cleanup();
|
|
3284
|
+
} catch (error) {
|
|
3285
|
+
handleError(error);
|
|
3286
|
+
}
|
|
3287
|
+
});
|
|
3288
|
+
userGroupCommand.command("list <uid>").description("List groups for a user").action(async (uid) => {
|
|
3289
|
+
try {
|
|
3290
|
+
const config = buildConfig(globalOptions);
|
|
3291
|
+
const client = new LldapClient(config);
|
|
3292
|
+
const userService = new UserService(client);
|
|
3293
|
+
const resolvedUid = await userService.resolveUserId(uid);
|
|
3294
|
+
const groups = await userService.getUserGroups(resolvedUid);
|
|
3295
|
+
console.log(formatList(groups));
|
|
3296
|
+
await client.cleanup();
|
|
3297
|
+
} catch (error) {
|
|
3298
|
+
handleError(error);
|
|
3299
|
+
}
|
|
3300
|
+
});
|
|
3301
|
+
var groupCommand = program2.command("group").description("Group management commands");
|
|
3302
|
+
groupCommand.command("list").description("List all groups").action(async () => {
|
|
3303
|
+
try {
|
|
3304
|
+
const config = buildConfig(globalOptions);
|
|
3305
|
+
const client = new LldapClient(config);
|
|
3306
|
+
const groupService = new GroupService(client);
|
|
3307
|
+
const groups = await groupService.getGroups();
|
|
3308
|
+
console.log(formatGroupsTable(groups));
|
|
3309
|
+
await client.cleanup();
|
|
3310
|
+
} catch (error) {
|
|
3311
|
+
handleError(error);
|
|
3312
|
+
}
|
|
3313
|
+
});
|
|
3314
|
+
groupCommand.command("search <pattern>").description("Search groups by pattern. Supports * and ? wildcards").action(async (pattern) => {
|
|
3315
|
+
try {
|
|
3316
|
+
const config = buildConfig(globalOptions);
|
|
3317
|
+
const client = new LldapClient(config);
|
|
3318
|
+
const groupService = new GroupService(client);
|
|
3319
|
+
const groups = await groupService.searchGroups(pattern);
|
|
3320
|
+
if (groups.length === 0) {
|
|
3321
|
+
console.error(`No groups found matching: ${pattern}`);
|
|
3322
|
+
process.exit(ExitCode2.SUCCESS);
|
|
3323
|
+
}
|
|
3324
|
+
console.log(formatGroupsTable(groups));
|
|
3325
|
+
await client.cleanup();
|
|
3326
|
+
} catch (error) {
|
|
3327
|
+
handleError(error);
|
|
3328
|
+
}
|
|
3329
|
+
});
|
|
3330
|
+
groupCommand.command("add <name>").description("Create a new group").action(async (name) => {
|
|
3331
|
+
try {
|
|
3332
|
+
const config = buildConfig(globalOptions);
|
|
3333
|
+
const client = new LldapClient(config);
|
|
3334
|
+
const groupService = new GroupService(client);
|
|
3335
|
+
await groupService.createGroup(name);
|
|
3336
|
+
await client.cleanup();
|
|
3337
|
+
} catch (error) {
|
|
3338
|
+
handleError(error);
|
|
3339
|
+
}
|
|
3340
|
+
});
|
|
3341
|
+
groupCommand.command("del <name>").description("Delete a group").action(async (name) => {
|
|
3342
|
+
try {
|
|
3343
|
+
const config = buildConfig(globalOptions);
|
|
3344
|
+
const client = new LldapClient(config);
|
|
3345
|
+
const groupService = new GroupService(client);
|
|
3346
|
+
await groupService.deleteGroup(name);
|
|
3347
|
+
await client.cleanup();
|
|
3348
|
+
} catch (error) {
|
|
3349
|
+
handleError(error);
|
|
3350
|
+
}
|
|
3351
|
+
});
|
|
3352
|
+
groupCommand.command("update <mutation> <name> <attribute> [value]").description("Update a group attribute (mutation: set, clear, add, del)").action(async (mutation, name, attribute, value) => {
|
|
3353
|
+
try {
|
|
3354
|
+
const config = buildConfig(globalOptions);
|
|
3355
|
+
const client = new LldapClient(config);
|
|
3356
|
+
const groupService = new GroupService(client);
|
|
3357
|
+
await groupService.updateGroupAttribute(mutation, name, attribute, value);
|
|
3358
|
+
await client.cleanup();
|
|
3359
|
+
} catch (error) {
|
|
3360
|
+
handleError(error);
|
|
3361
|
+
}
|
|
3362
|
+
});
|
|
3363
|
+
groupCommand.command("info <name>").description("List users in a group").action(async (name) => {
|
|
3364
|
+
try {
|
|
3365
|
+
const config = buildConfig(globalOptions);
|
|
3366
|
+
const client = new LldapClient(config);
|
|
3367
|
+
const groupService = new GroupService(client);
|
|
3368
|
+
const userIds = await groupService.listUserIdsByGroupName(name);
|
|
3369
|
+
console.log(formatList(userIds));
|
|
3370
|
+
await client.cleanup();
|
|
3371
|
+
} catch (error) {
|
|
3372
|
+
handleError(error);
|
|
3373
|
+
}
|
|
3374
|
+
});
|
|
3375
|
+
var groupAttributeCommand = groupCommand.command("attribute").description("Group attribute commands");
|
|
3376
|
+
groupAttributeCommand.command("list <name>").description("List attributes for a group").action(async (name) => {
|
|
3377
|
+
try {
|
|
3378
|
+
const config = buildConfig(globalOptions);
|
|
3379
|
+
const client = new LldapClient(config);
|
|
3380
|
+
const groupService = new GroupService(client);
|
|
3381
|
+
const groupId = await groupService.getGroupId(name);
|
|
3382
|
+
const attributes = await groupService.listGroupAttributes(groupId);
|
|
3383
|
+
console.log(formatList(attributes));
|
|
3384
|
+
await client.cleanup();
|
|
3385
|
+
} catch (error) {
|
|
3386
|
+
handleError(error);
|
|
3387
|
+
}
|
|
3388
|
+
});
|
|
3389
|
+
groupAttributeCommand.command("values <name> <attribute>").description("Get values for a group attribute").action(async (name, attribute) => {
|
|
3390
|
+
try {
|
|
3391
|
+
const config = buildConfig(globalOptions);
|
|
3392
|
+
const client = new LldapClient(config);
|
|
3393
|
+
const groupService = new GroupService(client);
|
|
3394
|
+
const groupId = await groupService.getGroupId(name);
|
|
3395
|
+
const values = await groupService.getGroupAttributeValues(groupId, attribute);
|
|
3396
|
+
console.log(formatList(values));
|
|
3397
|
+
await client.cleanup();
|
|
3398
|
+
} catch (error) {
|
|
3399
|
+
handleError(error);
|
|
3400
|
+
}
|
|
3401
|
+
});
|
|
3402
|
+
var schemaCommand = program2.command("schema").description("Schema management commands");
|
|
3403
|
+
var schemaAttributeCommand = schemaCommand.command("attribute").description("Schema attribute commands");
|
|
3404
|
+
var schemaUserCommand = schemaAttributeCommand.command("user").description("User schema attribute commands");
|
|
3405
|
+
schemaUserCommand.command("list").description("List user schema attributes").action(async () => {
|
|
3406
|
+
try {
|
|
3407
|
+
const config = buildConfig(globalOptions);
|
|
3408
|
+
const client = new LldapClient(config);
|
|
3409
|
+
const schemaService = new SchemaService(client);
|
|
3410
|
+
const attributes = await schemaService.getUserAttributes();
|
|
3411
|
+
console.log(formatSchemaAttributesTable(attributes));
|
|
3412
|
+
await client.cleanup();
|
|
3413
|
+
} catch (error) {
|
|
3414
|
+
handleError(error);
|
|
3415
|
+
}
|
|
3416
|
+
});
|
|
3417
|
+
schemaUserCommand.command("add <name> <type>").description("Add a user schema attribute (type: string, integer, date_time, jpeg_photo)").option("-l, --list", "Attribute is a list").option("-v, --visible", "Attribute is visible").option("-e, --editable", "Attribute is editable").action(async (name, type, opts) => {
|
|
3418
|
+
try {
|
|
3419
|
+
const config = buildConfig(globalOptions);
|
|
3420
|
+
const client = new LldapClient(config);
|
|
3421
|
+
const schemaService = new SchemaService(client);
|
|
3422
|
+
await schemaService.addUserAttribute(name, type.toUpperCase(), {
|
|
3423
|
+
isList: opts.list,
|
|
3424
|
+
isVisible: opts.visible,
|
|
3425
|
+
isEditable: opts.editable
|
|
3426
|
+
});
|
|
3427
|
+
await client.cleanup();
|
|
3428
|
+
} catch (error) {
|
|
3429
|
+
handleError(error);
|
|
3430
|
+
}
|
|
3431
|
+
});
|
|
3432
|
+
schemaUserCommand.command("del <name>").description("Delete a user schema attribute").action(async (name) => {
|
|
3433
|
+
try {
|
|
3434
|
+
const config = buildConfig(globalOptions);
|
|
3435
|
+
const client = new LldapClient(config);
|
|
3436
|
+
const schemaService = new SchemaService(client);
|
|
3437
|
+
await schemaService.deleteUserAttribute(name);
|
|
3438
|
+
await client.cleanup();
|
|
3439
|
+
} catch (error) {
|
|
3440
|
+
handleError(error);
|
|
3441
|
+
}
|
|
3442
|
+
});
|
|
3443
|
+
var schemaGroupCommand = schemaAttributeCommand.command("group").description("Group schema attribute commands");
|
|
3444
|
+
schemaGroupCommand.command("list").description("List group schema attributes").action(async () => {
|
|
3445
|
+
try {
|
|
3446
|
+
const config = buildConfig(globalOptions);
|
|
3447
|
+
const client = new LldapClient(config);
|
|
3448
|
+
const schemaService = new SchemaService(client);
|
|
3449
|
+
const attributes = await schemaService.getGroupAttributes();
|
|
3450
|
+
console.log(formatSchemaAttributesTable(attributes));
|
|
3451
|
+
await client.cleanup();
|
|
3452
|
+
} catch (error) {
|
|
3453
|
+
handleError(error);
|
|
3454
|
+
}
|
|
3455
|
+
});
|
|
3456
|
+
schemaGroupCommand.command("add <name> <type>").description("Add a group schema attribute (type: string, integer, date_time, jpeg_photo)").option("-l, --list", "Attribute is a list").option("-v, --visible", "Attribute is visible").option("-e, --editable", "Attribute is editable").action(async (name, type, opts) => {
|
|
3457
|
+
try {
|
|
3458
|
+
const config = buildConfig(globalOptions);
|
|
3459
|
+
const client = new LldapClient(config);
|
|
3460
|
+
const schemaService = new SchemaService(client);
|
|
3461
|
+
await schemaService.addGroupAttribute(name, type.toUpperCase(), {
|
|
3462
|
+
isList: opts.list,
|
|
3463
|
+
isVisible: opts.visible,
|
|
3464
|
+
isEditable: opts.editable
|
|
3465
|
+
});
|
|
3466
|
+
await client.cleanup();
|
|
3467
|
+
} catch (error) {
|
|
3468
|
+
handleError(error);
|
|
3469
|
+
}
|
|
3470
|
+
});
|
|
3471
|
+
schemaGroupCommand.command("del <name>").description("Delete a group schema attribute").action(async (name) => {
|
|
3472
|
+
try {
|
|
3473
|
+
const config = buildConfig(globalOptions);
|
|
3474
|
+
const client = new LldapClient(config);
|
|
3475
|
+
const schemaService = new SchemaService(client);
|
|
3476
|
+
await schemaService.deleteGroupAttribute(name);
|
|
3477
|
+
await client.cleanup();
|
|
3478
|
+
} catch (error) {
|
|
3479
|
+
handleError(error);
|
|
3480
|
+
}
|
|
3481
|
+
});
|
|
3482
|
+
var schemaObjectClassCommand = schemaCommand.command("objectclass").description("Schema object class commands");
|
|
3483
|
+
var objectClassUserCommand = schemaObjectClassCommand.command("user").description("User object class commands");
|
|
3484
|
+
objectClassUserCommand.command("list").description("List user object classes").action(async () => {
|
|
3485
|
+
try {
|
|
3486
|
+
const config = buildConfig(globalOptions);
|
|
3487
|
+
const client = new LldapClient(config);
|
|
3488
|
+
const schemaService = new SchemaService(client);
|
|
3489
|
+
const classes = await schemaService.listUserObjectClasses();
|
|
3490
|
+
console.log(formatList(classes));
|
|
3491
|
+
await client.cleanup();
|
|
3492
|
+
} catch (error) {
|
|
3493
|
+
handleError(error);
|
|
3494
|
+
}
|
|
3495
|
+
});
|
|
3496
|
+
objectClassUserCommand.command("add <name>").description("Add a user object class").action(async (name) => {
|
|
3497
|
+
try {
|
|
3498
|
+
const config = buildConfig(globalOptions);
|
|
3499
|
+
const client = new LldapClient(config);
|
|
3500
|
+
const schemaService = new SchemaService(client);
|
|
3501
|
+
await schemaService.addUserObjectClass(name);
|
|
3502
|
+
await client.cleanup();
|
|
3503
|
+
} catch (error) {
|
|
3504
|
+
handleError(error);
|
|
3505
|
+
}
|
|
3506
|
+
});
|
|
3507
|
+
objectClassUserCommand.command("del <name>").description("Delete a user object class").action(async (name) => {
|
|
3508
|
+
try {
|
|
3509
|
+
const config = buildConfig(globalOptions);
|
|
3510
|
+
const client = new LldapClient(config);
|
|
3511
|
+
const schemaService = new SchemaService(client);
|
|
3512
|
+
await schemaService.deleteUserObjectClass(name);
|
|
3513
|
+
await client.cleanup();
|
|
3514
|
+
} catch (error) {
|
|
3515
|
+
handleError(error);
|
|
3516
|
+
}
|
|
3517
|
+
});
|
|
3518
|
+
var objectClassGroupCommand = schemaObjectClassCommand.command("group").description("Group object class commands");
|
|
3519
|
+
objectClassGroupCommand.command("list").description("List group object classes").action(async () => {
|
|
3520
|
+
try {
|
|
3521
|
+
const config = buildConfig(globalOptions);
|
|
3522
|
+
const client = new LldapClient(config);
|
|
3523
|
+
const schemaService = new SchemaService(client);
|
|
3524
|
+
const classes = await schemaService.listGroupObjectClasses();
|
|
3525
|
+
console.log(formatList(classes));
|
|
3526
|
+
await client.cleanup();
|
|
3527
|
+
} catch (error) {
|
|
3528
|
+
handleError(error);
|
|
3529
|
+
}
|
|
3530
|
+
});
|
|
3531
|
+
objectClassGroupCommand.command("add <name>").description("Add a group object class").action(async (name) => {
|
|
3532
|
+
try {
|
|
3533
|
+
const config = buildConfig(globalOptions);
|
|
3534
|
+
const client = new LldapClient(config);
|
|
3535
|
+
const schemaService = new SchemaService(client);
|
|
3536
|
+
await schemaService.addGroupObjectClass(name);
|
|
3537
|
+
await client.cleanup();
|
|
3538
|
+
} catch (error) {
|
|
3539
|
+
handleError(error);
|
|
3540
|
+
}
|
|
3541
|
+
});
|
|
3542
|
+
objectClassGroupCommand.command("del <name>").description("Delete a group object class").action(async (name) => {
|
|
3543
|
+
try {
|
|
3544
|
+
const config = buildConfig(globalOptions);
|
|
3545
|
+
const client = new LldapClient(config);
|
|
3546
|
+
const schemaService = new SchemaService(client);
|
|
3547
|
+
await schemaService.deleteGroupObjectClass(name);
|
|
3548
|
+
await client.cleanup();
|
|
3549
|
+
} catch (error) {
|
|
3550
|
+
handleError(error);
|
|
3551
|
+
}
|
|
3552
|
+
});
|
|
3553
|
+
async function readPassword() {
|
|
3554
|
+
const { execSync } = await import("child_process");
|
|
3555
|
+
const fs = await import("fs");
|
|
3556
|
+
try {
|
|
3557
|
+
if (fs.existsSync("/dev/tty")) {
|
|
3558
|
+
const ttyFd = fs.openSync("/dev/tty", "r");
|
|
3559
|
+
const ttyStream = fs.createReadStream("", { fd: ttyFd, autoClose: true });
|
|
3560
|
+
execSync("stty -echo < /dev/tty", { stdio: "pipe" });
|
|
3561
|
+
const readline = await import("readline");
|
|
3562
|
+
const rl = readline.createInterface({
|
|
3563
|
+
input: ttyStream,
|
|
3564
|
+
output: process.stderr,
|
|
3565
|
+
terminal: false
|
|
3566
|
+
});
|
|
3567
|
+
const password = await new Promise((resolve2) => {
|
|
3568
|
+
rl.once("line", (line) => {
|
|
3569
|
+
rl.close();
|
|
3570
|
+
resolve2(line);
|
|
3571
|
+
});
|
|
3572
|
+
});
|
|
3573
|
+
execSync("stty echo < /dev/tty", { stdio: "pipe" });
|
|
3574
|
+
process.stderr.write(`
|
|
3575
|
+
`);
|
|
3576
|
+
ttyStream.destroy();
|
|
3577
|
+
return password.trim();
|
|
3578
|
+
}
|
|
3579
|
+
} catch {
|
|
3580
|
+
try {
|
|
3581
|
+
execSync("stty echo < /dev/tty", { stdio: "pipe" });
|
|
3582
|
+
} catch {}
|
|
3583
|
+
}
|
|
3584
|
+
if (process.stdin.isTTY) {
|
|
3585
|
+
try {
|
|
3586
|
+
execSync("stty -echo", { stdio: "inherit" });
|
|
3587
|
+
const readline = await import("readline");
|
|
3588
|
+
const rl = readline.createInterface({
|
|
3589
|
+
input: process.stdin,
|
|
3590
|
+
output: process.stdout,
|
|
3591
|
+
terminal: false
|
|
3592
|
+
});
|
|
3593
|
+
const password = await new Promise((resolve2) => {
|
|
3594
|
+
rl.once("line", (line) => {
|
|
3595
|
+
rl.close();
|
|
3596
|
+
resolve2(line);
|
|
3597
|
+
});
|
|
3598
|
+
});
|
|
3599
|
+
execSync("stty echo", { stdio: "inherit" });
|
|
3600
|
+
process.stdout.write(`
|
|
3601
|
+
`);
|
|
3602
|
+
return password.trim();
|
|
3603
|
+
} catch {
|
|
3604
|
+
try {
|
|
3605
|
+
execSync("stty echo", { stdio: "inherit" });
|
|
3606
|
+
} catch {}
|
|
3607
|
+
throw new Error("Failed to read password securely");
|
|
3608
|
+
}
|
|
3609
|
+
}
|
|
3610
|
+
const text = await Bun.stdin.text();
|
|
3611
|
+
const firstLine = text.split(`
|
|
3612
|
+
`)[0] || "";
|
|
3613
|
+
return firstLine.trim();
|
|
3614
|
+
}
|
|
3615
|
+
function handleError(error) {
|
|
3616
|
+
if (error instanceof CliError2) {
|
|
3617
|
+
console.error(`ERROR: ${error.message}`);
|
|
3618
|
+
process.exit(error.exitCode);
|
|
3619
|
+
} else if (error instanceof Error) {
|
|
3620
|
+
console.error(`ERROR: ${error.message}`);
|
|
3621
|
+
} else {
|
|
3622
|
+
console.error("ERROR: An unknown error occurred");
|
|
3623
|
+
}
|
|
3624
|
+
process.exit(ExitCode2.ERROR);
|
|
3625
|
+
}
|
|
3626
|
+
program2.parse();
|