377 lines
8.8 KiB
TypeScript
377 lines
8.8 KiB
TypeScript
namespace _wiki_js_cli
|
|
{
|
|
|
|
/**
|
|
*/
|
|
function parse_boolean(
|
|
indicator : string
|
|
) : boolean
|
|
{
|
|
if (
|
|
(indicator === "1")
|
|
||
|
|
(indicator === "yes")
|
|
||
|
|
(indicator === "on")
|
|
||
|
|
(indicator === "true")
|
|
) {
|
|
return true;
|
|
}
|
|
else {
|
|
if (
|
|
(indicator === "0")
|
|
||
|
|
(indicator === "no")
|
|
||
|
|
(indicator === "off")
|
|
||
|
|
(indicator === "false")
|
|
) {
|
|
return false;
|
|
}
|
|
else {
|
|
throw (new Error("invalid boolean indicator: " + indicator));
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
export async function main(
|
|
args_raw : Array<string>
|
|
) : Promise<void>
|
|
{
|
|
// args
|
|
const args = _wiki_js_cli.helpers.args.parse(args_raw);
|
|
const override_url_base : (null | string) = (
|
|
(
|
|
("b" in args.volatile)
|
|
&&
|
|
(args.volatile["b"].length >= 0)
|
|
)
|
|
?
|
|
args.volatile["b"][0]
|
|
:
|
|
null
|
|
);
|
|
const override_username : (null | string) = (
|
|
(
|
|
("u" in args.volatile)
|
|
&&
|
|
(args.volatile["u"].length >= 0)
|
|
)
|
|
?
|
|
args.volatile["u"][0]
|
|
:
|
|
null
|
|
);
|
|
const override_password : (null | string) = (
|
|
(
|
|
("p" in args.volatile)
|
|
&&
|
|
(args.volatile["p"].length >= 0)
|
|
)
|
|
?
|
|
args.volatile["p"][0]
|
|
:
|
|
null
|
|
);
|
|
const override_log_level : (null | string) = (
|
|
(
|
|
("l" in args.volatile)
|
|
&&
|
|
(args.volatile["l"].length >= 0)
|
|
)
|
|
?
|
|
args.volatile["l"][0]
|
|
:
|
|
null
|
|
);
|
|
|
|
// conf
|
|
const conf_path : (null | string) = (
|
|
(
|
|
("c" in args.volatile)
|
|
&&
|
|
(args.volatile["c"].length >= 0)
|
|
)
|
|
?
|
|
args.volatile["c"][0]
|
|
:
|
|
null
|
|
);
|
|
await _wiki_js_cli.conf.load(conf_path);
|
|
_wiki_js_cli.helpers.log.write(
|
|
_wiki_js_cli.helpers.log.enum_level.debug,
|
|
"conf",
|
|
_wiki_js_cli.conf.get()
|
|
);
|
|
|
|
// init
|
|
await _wiki_js_cli.helpers.log.setup(
|
|
override_log_level ?? _wiki_js_cli.conf.get().log.threshold
|
|
);
|
|
await _wiki_js_cli.api.init(
|
|
(override_url_base ?? _wiki_js_cli.conf.get().api.url_base)
|
|
);
|
|
|
|
// exec
|
|
if (args.positional.length < 1) {
|
|
return Promise.reject("SYNTAX: [node] cli.js [-c <conf-path>] [-b <api-url-base>] [-u <login-username>] [-p <login-password>] <action> [<arg-1> [<arg-2> […]]]\n\n\t<action> = init | email-settings-set | locale-add | group-add | group-modify | auth-strat-add-oauth2 | theming-set");
|
|
}
|
|
else {
|
|
const action : string = args.positional[0];
|
|
switch (action) {
|
|
default: {
|
|
return Promise.reject("invalid action: " + action);
|
|
break;
|
|
}
|
|
case "init": {
|
|
if (args.positional.length <= 2) {
|
|
return Promise.reject("SYNTAX: … init <admin-email-address> <admin-password> [<site-url> [<allow-telemetry>]]");
|
|
}
|
|
else {
|
|
const admin_email_address : string = args.positional[1];
|
|
const admin_password : string = args.positional[2];
|
|
const site_url : (undefined | string) = (
|
|
(args.positional.length >= 4)
|
|
?
|
|
args.positional[3]
|
|
:
|
|
undefined
|
|
);
|
|
const allow_telemetry : (undefined | boolean) = (
|
|
(args.positional.length >= 5)
|
|
?
|
|
parse_boolean(args.positional[4])
|
|
:
|
|
undefined
|
|
);
|
|
await _wiki_js_cli.logic.initialize(
|
|
admin_email_address,
|
|
admin_password,
|
|
{
|
|
"site_url": site_url,
|
|
"allow_telemetry": allow_telemetry
|
|
}
|
|
);
|
|
return Promise.resolve(undefined);
|
|
}
|
|
break;
|
|
}
|
|
case "email-settings-set": {
|
|
if (args.positional.length <= 6) {
|
|
return Promise.reject("SYNTAX: … email-settings-set <smtp-host> <smtp-port> <smtp-username> <smtp-password> <sender-name> <sender-email-address>");
|
|
}
|
|
else {
|
|
const smtp_host : string = args.positional[1];
|
|
const smtp_port : int = parseInt(args.positional[2]);
|
|
const smtp_username : string = args.positional[3];
|
|
const smtp_password : string = args.positional[4];
|
|
const sender_name : string = args.positional[5];
|
|
const sender_email_address : string = args.positional[6];
|
|
await _wiki_js_cli.logic.login(
|
|
{
|
|
"username": override_username,
|
|
"password": override_password,
|
|
}
|
|
);
|
|
await _wiki_js_cli.logic.email_settings_set(
|
|
smtp_host,
|
|
smtp_port,
|
|
smtp_username,
|
|
smtp_password,
|
|
sender_name,
|
|
sender_email_address,
|
|
{
|
|
}
|
|
);
|
|
return Promise.resolve(undefined);
|
|
}
|
|
break;
|
|
}
|
|
case "locale-add": {
|
|
if (args.positional.length <= 1) {
|
|
return Promise.reject("SYNTAX: … locale-add <locale-code>");
|
|
}
|
|
else {
|
|
const locale_code : string = args.positional[1];
|
|
await _wiki_js_cli.logic.login(
|
|
{
|
|
"username": override_username,
|
|
"password": override_password,
|
|
}
|
|
);
|
|
await _wiki_js_cli.logic.locale_add(
|
|
locale_code
|
|
);
|
|
return Promise.resolve(undefined);
|
|
}
|
|
break;
|
|
}
|
|
case "group-add": {
|
|
if (args.positional.length <= 2) {
|
|
return Promise.reject("SYNTAX: … group-add <name> <permissions>");
|
|
}
|
|
else {
|
|
const name = args.positional[1];
|
|
const permissions = args.positional[2].split(",");
|
|
await _wiki_js_cli.logic.login(
|
|
{
|
|
"username": override_username,
|
|
"password": override_password,
|
|
}
|
|
);
|
|
const result = await _wiki_js_cli.logic.group_add(
|
|
name,
|
|
{
|
|
"permissions_general": permissions,
|
|
"permissions_specific": [
|
|
{
|
|
"id": "default",
|
|
"path": "",
|
|
"roles": permissions,
|
|
"match": "START",
|
|
"deny": false,
|
|
"locales": []
|
|
}
|
|
],
|
|
}
|
|
);
|
|
process.stdout.write(
|
|
JSON.stringify(
|
|
result,
|
|
undefined,
|
|
"\t"
|
|
)
|
|
+
|
|
"\n"
|
|
);
|
|
}
|
|
break;
|
|
}
|
|
case "group-modify": {
|
|
if (args.positional.length <= 2) {
|
|
return Promise.reject("SYNTAX: … group-modify <name> <permissions>");
|
|
}
|
|
else {
|
|
const name = args.positional[1];
|
|
const permissions = args.positional[2].split(",");
|
|
const id : int = await _wiki_js_cli.logic.group_identify(name);
|
|
await _wiki_js_cli.logic.login(
|
|
{
|
|
"username": override_username,
|
|
"password": override_password,
|
|
}
|
|
);
|
|
const result = await _wiki_js_cli.logic.group_modify(
|
|
id,
|
|
{
|
|
"permissions_general": permissions,
|
|
"permissions_specific": [
|
|
{
|
|
"id": "default",
|
|
"path": "",
|
|
"roles": permissions,
|
|
"match": "START",
|
|
"deny": false,
|
|
"locales": []
|
|
}
|
|
],
|
|
}
|
|
);
|
|
}
|
|
break;
|
|
}
|
|
case "auth-strat-add-oauth2": {
|
|
if (args.positional.length <= 8) {
|
|
return Promise.reject("SYNTAX: … auth-strat-add-oauth2 <strategy-key> <strategy-name> <strategy-client-id> <strategy-client-secret> <strategy-authorization-url> <strategy-token-url> <strategy-user-info-url> <group-assignments>");
|
|
}
|
|
else {
|
|
const strategy = {
|
|
"key": args.positional[1],
|
|
"name": args.positional[2],
|
|
"client_id": args.positional[3],
|
|
"client_secret": args.positional[4],
|
|
"authorization_url": args.positional[5],
|
|
"token_url": args.positional[6],
|
|
"user_info_url": args.positional[7],
|
|
"group_assignments": args.positional[8].split(","),
|
|
}
|
|
await _wiki_js_cli.logic.login(
|
|
{
|
|
"username": override_username,
|
|
"password": override_password,
|
|
}
|
|
);
|
|
await _wiki_js_cli.logic.authentication_strategy_add(
|
|
strategy
|
|
);
|
|
return Promise.resolve(undefined);
|
|
}
|
|
break;
|
|
}
|
|
case "theming-set": {
|
|
if (args.positional.length <= 1) {
|
|
return Promise.reject("SYNTAX: … theming-set <dark-mode> [<toc-position>]");
|
|
}
|
|
else {
|
|
const dark_mode : boolean = parse_boolean(args.positional[1]);
|
|
const toc_position_raw : string = (
|
|
(args.positional.length <= 2)
|
|
?
|
|
"left"
|
|
:
|
|
args.positional[2]
|
|
);
|
|
const toc_position : (null | ("left" | "right" | "off")) = (() => {
|
|
switch (toc_position_raw) {
|
|
case "left": return "left";
|
|
case "right": return "right";
|
|
case "hidden": return "off";
|
|
default: {return null;}
|
|
}
|
|
}) ();
|
|
if (toc_position === null) {
|
|
return Promise.reject("invalid toc-position: " + toc_position_raw + "; valid values are: left,right,hidden");
|
|
}
|
|
else {
|
|
await _wiki_js_cli.logic.login(
|
|
{
|
|
"username": override_username,
|
|
"password": override_password,
|
|
}
|
|
);
|
|
await _wiki_js_cli.logic.theming_set(
|
|
{
|
|
"dark_mode": dark_mode,
|
|
"toc_position": toc_position,
|
|
}
|
|
);
|
|
return Promise.resolve(undefined);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
(
|
|
_wiki_js_cli.main(process.argv.slice(2))
|
|
.then(
|
|
() => {
|
|
}
|
|
)
|
|
.catch(
|
|
(reason) => {
|
|
process.stderr.write("-- " + String(reason) + "\n");
|
|
}
|
|
)
|
|
);
|
|
|