diff --git a/CHANGELOG.md b/CHANGELOG.md index e7cf0a2..ce5442e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ ## Fixes and improvements - +# 2.0.0 +## Breaking changes +- Make `event` use a multiselect, because it is much easier, safer, and faster to use, and can give a more helpful error message. +## Fixes and improvements +- Allow '-' in text inputs +- Change update command to install with @latest, to allow major version updates + # 1.11.1 ## Fixes and improvements - Fix `stats` command with count < 1000 diff --git a/contexts.js b/contexts.js index 6ff038d..3a2a361 100644 --- a/contexts.js +++ b/contexts.js @@ -62,9 +62,15 @@ const NOT_SERVICE_CONTEXT = (cmd) => { return hint; }; const SERVICE_GROUP_CONTEXT = (cmd) => { - let serviceGroups = (0, utils_1.directoryNames)(new utils_1.Path(), ["event-catalogue", "public"]); + let struct = (0, utils_1.fetchOrgRaw)(); + let serviceGroups = (0, utils_1.directoryNames)(new utils_1.Path(struct.pathToRoot), [ + "event-catalogue", + "public", + ]); let hint = `You can only run '${cmd}' from inside a service group.`; - hint += `\nUse '${prompt_1.GREEN}cd ${serviceGroups[0]}${prompt_1.NORMAL_COLOR}' or one of these:`; + hint += `\nUse '${prompt_1.GREEN}cd ${path_1.default + .join(struct.pathToRoot, serviceGroups[0].name) + .replace(/\\/g, "\\\\")}${prompt_1.NORMAL_COLOR}' or one of these:`; return hint; }; const NOT_SERVICE_GROUP_CONTEXT = upOrg; diff --git a/contexts.ts b/contexts.ts index 76bffea..56d46ba 100644 --- a/contexts.ts +++ b/contexts.ts @@ -60,9 +60,15 @@ const NOT_SERVICE_CONTEXT = (cmd: string) => { return hint; }; const SERVICE_GROUP_CONTEXT = (cmd: string) => { - let serviceGroups = directoryNames(new Path(), ["event-catalogue", "public"]); + let struct = fetchOrgRaw(); + let serviceGroups = directoryNames(new Path(struct.pathToRoot!), [ + "event-catalogue", + "public", + ]); let hint = `You can only run '${cmd}' from inside a service group.`; - hint += `\nUse '${GREEN}cd ${serviceGroups[0]}${NORMAL_COLOR}' or one of these:`; + hint += `\nUse '${GREEN}cd ${path + .join(struct.pathToRoot!, serviceGroups[0].name) + .replace(/\\/g, "\\\\")}${NORMAL_COLOR}' or one of these:`; return hint; }; const NOT_SERVICE_GROUP_CONTEXT = upOrg; diff --git a/dist/windows.zip b/dist/windows.zip index ca1f712..721f896 100644 Binary files a/dist/windows.zip and b/dist/windows.zip differ diff --git a/executors.js b/executors.js index 722d5ac..4862aff 100644 --- a/executors.js +++ b/executors.js @@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.do_spending = exports.do_remove_auto_approve = exports.do_auto_approve = exports.do_attach_role = exports.do_join = exports.do_post = exports.do_help = exports.do_queue_time = exports.printTableHeader = exports.alignLeft = exports.alignRight = exports.do_cron = exports.do_event = exports.do_envvar = exports.do_key = exports.do_inspect = exports.do_build = exports.do_redeploy = exports.do_deploy = exports.generateNewKey = exports.useExistingKey = exports.do_register = exports.addKnownHost = exports.do_duplicate = exports.fetch_template = exports.createService = exports.createServiceGroup = exports.createOrganization = exports.do_clone = exports.do_fetch = void 0; +exports.do_event = exports.do_spending = exports.do_remove_auto_approve = exports.do_auto_approve = exports.do_attach_role = exports.do_join = exports.do_post = exports.do_help = exports.do_queue_time = exports.printTableHeader = exports.alignLeft = exports.alignRight = exports.do_cron = exports.do_envvar = exports.do_key = exports.do_inspect = exports.do_build = exports.do_redeploy = exports.do_deploy = exports.generateNewKey = exports.useExistingKey = exports.do_register = exports.addKnownHost = exports.do_duplicate = exports.fetch_template = exports.createService = exports.createServiceGroup = exports.createOrganization = exports.do_clone = exports.do_fetch = void 0; const fs_1 = __importDefault(require("fs")); const os_1 = __importDefault(require("os")); const utils_1 = require("./utils"); @@ -468,17 +468,6 @@ function do_envvar(org, group, overwrite, key, value, access, visibility) { }); } exports.do_envvar = do_envvar; -function do_event(org, key, event, create) { - return __awaiter(this, void 0, void 0, function* () { - try { - (0, utils_1.output2)(yield (0, utils_1.sshReq)(`event`, event, `--key`, key, ...(create ? [] : [`--delete`]))); - } - catch (e) { - throw e; - } - }); -} -exports.do_event = do_event; function do_cron(org, name, overwrite, event, expr) { return __awaiter(this, void 0, void 0, function* () { try { @@ -746,3 +735,15 @@ function do_spending(org) { }); } exports.do_spending = do_spending; +function do_event(key, events) { + return __awaiter(this, void 0, void 0, function* () { + try { + let selected = Object.keys(events).filter((x) => events[x]); + (0, utils_1.output2)(yield (0, utils_1.sshReq)(`event`, `--key`, key, selected.join(","))); + } + catch (e) { + throw e; + } + }); +} +exports.do_event = do_event; diff --git a/executors.ts b/executors.ts index af30d5f..ae03c25 100644 --- a/executors.ts +++ b/executors.ts @@ -517,27 +517,6 @@ export async function do_envvar( } } -export async function do_event( - org: string, - key: string, - event: string, - create: boolean -) { - try { - output2( - await sshReq( - `event`, - event, - `--key`, - key, - ...(create ? [] : [`--delete`]) - ) - ); - } catch (e) { - throw e; - } -} - export async function do_cron( org: string, name: string, @@ -847,3 +826,15 @@ export async function do_spending(org: string) { throw e; } } + +export async function do_event( + key: string, + events: { [event: string]: boolean } +) { + try { + let selected = Object.keys(events).filter((x) => events[x]); + output2(await sshReq(`event`, `--key`, key, selected.join(","))); + } catch (e) { + throw e; + } +} diff --git a/mm.js b/mm.js index 2d3b348..19d7995 100644 --- a/mm.js +++ b/mm.js @@ -4,5 +4,5 @@ Object.defineProperty(exports, "__esModule", { value: true }); process.env["COMMAND"] = "mm"; const prompt_1 = require("./prompt"); process.env["UPDATE_MESSAGE"] = `to update run the command: -${prompt_1.YELLOW}npm update -g @merrymake/cli${prompt_1.NORMAL_COLOR}`; +${prompt_1.YELLOW}npm install --global @merrymake/cli@latest${prompt_1.NORMAL_COLOR}`; require("./index"); diff --git a/mm.ts b/mm.ts index 3910f53..a405814 100644 --- a/mm.ts +++ b/mm.ts @@ -2,5 +2,5 @@ process.env["COMMAND"] = "mm"; import { YELLOW, NORMAL_COLOR } from "./prompt"; process.env["UPDATE_MESSAGE"] = `to update run the command: -${YELLOW}npm update -g @merrymake/cli${NORMAL_COLOR}`; +${YELLOW}npm install --global @merrymake/cli@latest${NORMAL_COLOR}`; import "./index"; diff --git a/mmk.js b/mmk.js index c627c49..7a9b4fe 100644 --- a/mmk.js +++ b/mmk.js @@ -4,5 +4,5 @@ Object.defineProperty(exports, "__esModule", { value: true }); process.env["COMMAND"] = "mmk"; const prompt_1 = require("./prompt"); process.env["UPDATE_MESSAGE"] = `to update run the command: -${prompt_1.YELLOW}npm update -g @merrymake/cli${prompt_1.NORMAL_COLOR}`; +${prompt_1.YELLOW}npm install --global @merrymake/cli@latest${prompt_1.NORMAL_COLOR}`; require("./index"); diff --git a/mmk.ts b/mmk.ts index 8842c23..b57bd50 100644 --- a/mmk.ts +++ b/mmk.ts @@ -2,5 +2,5 @@ process.env["COMMAND"] = "mmk"; import { YELLOW, NORMAL_COLOR } from "./prompt"; process.env["UPDATE_MESSAGE"] = `to update run the command: -${YELLOW}npm update -g @merrymake/cli${NORMAL_COLOR}`; +${YELLOW}npm install --global @merrymake/cli@latest${NORMAL_COLOR}`; import "./index"; diff --git a/package.json b/package.json index fb6688d..a922a00 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@merrymake/cli", - "version": "1.11.1", + "version": "2.0.0", "description": "", "main": "index.js", "scripts": { diff --git a/prompt.js b/prompt.js index cc4956f..84d1380 100644 --- a/prompt.js +++ b/prompt.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.exit = exports.shortText = exports.spinner_stop = exports.spinner_start = exports.choice = exports.output = exports.INVISIBLE = exports.YELLOW = exports.GREEN = exports.BLUE = exports.RED = exports.NORMAL_COLOR = exports.SHOW_CURSOR = exports.HIDE_CURSOR = exports.RIGHT = exports.LEFT = exports.DOWN = exports.UP = exports.ENTER = exports.DELETE = exports.ESCAPE = exports.BACKSPACE = exports.CTRL_C = void 0; +exports.exit = exports.shortText = exports.spinner_stop = exports.spinner_start = exports.multiSelect = exports.choice = exports.output = exports.INVISIBLE = exports.YELLOW = exports.GREEN = exports.BLUE = exports.RED = exports.NORMAL_COLOR = exports.SHOW_CURSOR = exports.HIDE_CURSOR = exports.RIGHT = exports.LEFT = exports.DOWN = exports.UP = exports.ENTER = exports.DELETE = exports.ESCAPE = exports.BACKSPACE = exports.CTRL_C = void 0; const node_process_1 = require("node:process"); const args_1 = require("./args"); const utils_1 = require("./utils"); @@ -78,14 +78,16 @@ function getCursorPosition() { return [xOffset, yOffset]; } let command = "$ " + process.env["COMMAND"]; -function makeSelectionInternal(option, extra) { +function makeSelectionSuperInternal(action, extra = () => { }) { moveToBottom(); cleanup(); - if (option.short !== "x") - extra(); + extra(); if (listener !== undefined) node_process_1.stdin.removeListener("data", listener); - return option.action(); + return action(); +} +function makeSelectionInternal(option, extra) { + return makeSelectionSuperInternal(() => option.action(), option.short !== "x" ? extra : () => { }); } function makeSelection(option) { return makeSelectionInternal(option, () => { @@ -208,6 +210,146 @@ function choice(options, invertedQuiet = { cmd: false, select: true }, def = 0) }); } exports.choice = choice; +const SELECTED_MARK = "✔"; +const NOT_SELECTED_MARK = "_"; +function multiSelect(selection, after, errorMessage) { + return new Promise((resolve) => { + // options.push({ + // short: "x", + // long: "x", + // text: "exit", + // action: () => abort(), + // }); + let keys = Object.keys(selection); + if (keys.length === 0) { + console.log(errorMessage); + process.exit(1); + } + if ((0, args_1.getArgs)().length > 0) { + let arg = (0, args_1.getArgs)()[0]; + let es = arg.split(","); + let result = {}; + keys.forEach((e) => (result[e] = false)); + let illegal = es.filter((e) => !keys.includes(e)); + if (illegal.length > 0) { + output(`Invalid arguments in the current context: ${illegal.join(", ")}\n`); + } + else { + es.forEach((e) => (result[e] = true)); + } + (0, args_1.getArgs)().splice(0, (0, args_1.getArgs)().length); + resolve(makeSelectionSuperInternal(() => after(selection))); + return; + } + let str = []; + for (let i = 0; i < keys.length; i++) { + str.push(" "); + str.push(selection[keys[i]] === true ? SELECTED_MARK : NOT_SELECTED_MARK); + str.push(" "); + str.push(keys[i]); + str.push("\n"); + } + // Add submit and exit + str.push(` [s] submit\n`); + str.push(` [x] exit\n`); + output(exports.HIDE_CURSOR); + output(str.join("")); + if (!node_process_1.stdin.isTTY || node_process_1.stdin.setRawMode === undefined) { + console.log("This console does not support TTY, please use the 'mmk'-command instead."); + process.exit(1); + } + let pos = 0; + output(exports.YELLOW); + moveCursor(0, -(keys.length + 2) + pos); + output(`>`); + moveCursor(-1, 0); + // on any data into stdin + node_process_1.stdin.on("data", (listener = (key) => { + let k = key.toString(); + // moveCursor(0, options.length - pos); + // //let l = JSON.stringify(key); + // //output(l); + // stdout.write("" + yOffset); + // moveCursor(-("" + yOffset).length, -options.length + pos); + if (k === exports.ENTER) { + if (pos === keys.length) { + // Submit + let selected = keys.filter((x) => selection[x] === true).join(","); + resolve(makeSelectionSuperInternal(() => after(selection), () => { + output("\n"); + output((command += + " " + + (selected.includes(" ") || selected.length === 0 + ? `'${selected}'` + : selected))); + output("\n"); + })); + } + else if (pos > keys.length) { + // Exit + resolve(makeSelectionQuietly({ + short: "x", + long: "x", + text: "exit", + action: () => (0, utils_1.abort)(), + })); + } + else { + let sel = (selection[keys[pos]] = !selection[keys[pos]]); + moveCursor(2, 0); + output(exports.NORMAL_COLOR); + output(sel ? SELECTED_MARK : NOT_SELECTED_MARK); + output(exports.YELLOW); + moveCursor(-3, 0); + } + return; + } + else if (k === exports.UP && pos <= 0) { + return; + } + else if (k === exports.UP) { + pos--; + output(` `); + moveCursor(-1, -1); + output(`>`); + moveCursor(-1, 0); + } + else if (k === exports.DOWN && pos >= keys.length + 2 - 1) { + return; + } + else if (k === exports.DOWN) { + pos++; + output(` `); + moveCursor(-1, 1); + output(`>`); + moveCursor(-1, 0); + } + else if (k === "x") { + makeSelectionQuietly({ + short: "x", + long: "x", + text: "exit", + action: () => (0, utils_1.abort)(), + }); + } + else if (k === "s") { + let selected = keys.filter((x) => selection[x] === true).join(","); + resolve(makeSelectionSuperInternal(() => after(selection), () => { + output("\n"); + output((command += + " " + + (selected.includes(" ") || selected.length === 0 + ? `'${selected}'` + : selected))); + output("\n"); + })); + } + // write the key to stdout all normal like + // output(key); + })); + }); +} +exports.multiSelect = multiSelect; let interval; let spinnerIndex = 0; const SPINNER = ["│", "/", "─", "\\"]; @@ -319,14 +461,15 @@ function shortText(prompt, description, defaultValueArg) { output(beforeCursor + afterCursor); moveCursor(-afterCursor.length, 0); } - else if (k === exports.BACKSPACE && beforeCursor.length > 0) { + else if ((k === exports.BACKSPACE || k.charCodeAt(0) === 8) && + beforeCursor.length > 0) { moveCursor(-beforeCursor.length, 0); beforeCursor = beforeCursor.substring(0, beforeCursor.length - 1); node_process_1.stdout.clearLine(1); output(beforeCursor + afterCursor); moveCursor(-afterCursor.length, 0); } - else if (/^[A-Za-z0-9@_, .-/:;#=&*?+]+$/.test(k)) { + else if (/^[A-Za-z0-9@_, .\-/:;#=&*?+]+$/.test(k)) { moveCursor(-beforeCursor.length, 0); beforeCursor += k; node_process_1.stdout.clearLine(1); diff --git a/prompt.ts b/prompt.ts index 1076644..eb8e8f3 100644 --- a/prompt.ts +++ b/prompt.ts @@ -85,12 +85,21 @@ function getCursorPosition() { let command = "$ " + process.env["COMMAND"]; -function makeSelectionInternal(option: Option, extra: () => void) { +function makeSelectionSuperInternal( + action: () => Promise, + extra: () => void = () => {} +) { moveToBottom(); cleanup(); - if (option.short !== "x") extra(); + extra(); if (listener !== undefined) stdin.removeListener("data", listener); - return option.action(); + return action(); +} +function makeSelectionInternal(option: Option, extra: () => void) { + return makeSelectionSuperInternal( + () => option.action(), + option.short !== "x" ? extra : () => {} + ); } function makeSelection(option: Option) { return makeSelectionInternal(option, () => { @@ -233,6 +242,170 @@ export function choice( }); } +const SELECTED_MARK = "✔"; +const NOT_SELECTED_MARK = "_"; + +export function multiSelect( + selection: { [key: string]: boolean }, + after: (selection: { [key: string]: boolean }) => Promise, + errorMessage?: string +) { + return new Promise((resolve) => { + // options.push({ + // short: "x", + // long: "x", + // text: "exit", + // action: () => abort(), + // }); + let keys = Object.keys(selection); + if (keys.length === 0) { + console.log(errorMessage); + process.exit(1); + } + if (getArgs().length > 0) { + let arg = getArgs()[0]; + let es = arg.split(","); + let result: { [key: string]: boolean } = {}; + keys.forEach((e) => (result[e] = false)); + let illegal = es.filter((e) => !keys.includes(e)); + if (illegal.length > 0) { + output( + `Invalid arguments in the current context: ${illegal.join(", ")}\n` + ); + } else { + es.forEach((e) => (result[e] = true)); + } + getArgs().splice(0, getArgs().length); + resolve(makeSelectionSuperInternal(() => after(selection))); + return; + } + let str: string[] = []; + for (let i = 0; i < keys.length; i++) { + str.push(" "); + str.push(selection[keys[i]] === true ? SELECTED_MARK : NOT_SELECTED_MARK); + str.push(" "); + str.push(keys[i]); + str.push("\n"); + } + + // Add submit and exit + str.push(` [s] submit\n`); + str.push(` [x] exit\n`); + + output(HIDE_CURSOR); + output(str.join("")); + + if (!stdin.isTTY || stdin.setRawMode === undefined) { + console.log( + "This console does not support TTY, please use the 'mmk'-command instead." + ); + process.exit(1); + } + + let pos = 0; + output(YELLOW); + moveCursor(0, -(keys.length + 2) + pos); + output(`>`); + moveCursor(-1, 0); + + // on any data into stdin + stdin.on( + "data", + (listener = (key) => { + let k = key.toString(); + // moveCursor(0, options.length - pos); + // //let l = JSON.stringify(key); + // //output(l); + // stdout.write("" + yOffset); + // moveCursor(-("" + yOffset).length, -options.length + pos); + if (k === ENTER) { + if (pos === keys.length) { + // Submit + let selected = keys.filter((x) => selection[x] === true).join(","); + resolve( + makeSelectionSuperInternal( + () => after(selection), + () => { + output("\n"); + output( + (command += + " " + + (selected.includes(" ") || selected.length === 0 + ? `'${selected}'` + : selected)) + ); + output("\n"); + } + ) + ); + } else if (pos > keys.length) { + // Exit + resolve( + makeSelectionQuietly({ + short: "x", + long: "x", + text: "exit", + action: () => abort(), + }) + ); + } else { + let sel = (selection[keys[pos]] = !selection[keys[pos]]); + moveCursor(2, 0); + output(NORMAL_COLOR); + output(sel ? SELECTED_MARK : NOT_SELECTED_MARK); + output(YELLOW); + moveCursor(-3, 0); + } + return; + } else if (k === UP && pos <= 0) { + return; + } else if (k === UP) { + pos--; + output(` `); + moveCursor(-1, -1); + output(`>`); + moveCursor(-1, 0); + } else if (k === DOWN && pos >= keys.length + 2 - 1) { + return; + } else if (k === DOWN) { + pos++; + output(` `); + moveCursor(-1, 1); + output(`>`); + moveCursor(-1, 0); + } else if (k === "x") { + makeSelectionQuietly({ + short: "x", + long: "x", + text: "exit", + action: () => abort(), + }); + } else if (k === "s") { + let selected = keys.filter((x) => selection[x] === true).join(","); + resolve( + makeSelectionSuperInternal( + () => after(selection), + () => { + output("\n"); + output( + (command += + " " + + (selected.includes(" ") || selected.length === 0 + ? `'${selected}'` + : selected)) + ); + output("\n"); + } + ) + ); + } + // write the key to stdout all normal like + // output(key); + }) + ); + }); +} + let interval: NodeJS.Timer | undefined; let spinnerIndex = 0; const SPINNER = ["│", "/", "─", "\\"]; @@ -347,13 +520,16 @@ export function shortText( stdout.clearLine(1); output(beforeCursor + afterCursor); moveCursor(-afterCursor.length, 0); - } else if (k === BACKSPACE && beforeCursor.length > 0) { + } else if ( + (k === BACKSPACE || k.charCodeAt(0) === 8) && + beforeCursor.length > 0 + ) { moveCursor(-beforeCursor.length, 0); beforeCursor = beforeCursor.substring(0, beforeCursor.length - 1); stdout.clearLine(1); output(beforeCursor + afterCursor); moveCursor(-afterCursor.length, 0); - } else if (/^[A-Za-z0-9@_, .-/:;#=&*?+]+$/.test(k)) { + } else if (/^[A-Za-z0-9@_, .\-/:;#=&*?+]+$/.test(k)) { moveCursor(-beforeCursor.length, 0); beforeCursor += k; stdout.clearLine(1); diff --git a/questions.js b/questions.js index 1af146d..fc098fe 100644 --- a/questions.js +++ b/questions.js @@ -704,40 +704,16 @@ function post(org) { } }); } -function event_key_event(org, key, event, create) { - (0, utils_1.addToExecuteQueue)(() => (0, executors_1.do_event)(org, key, event, create)); +function event_key_events(key, events) { + (0, utils_1.addToExecuteQueue)(() => (0, executors_1.do_event)(key, events)); return (0, utils_1.finish)(); } -function event_key_new(org, key) { +function event_key(key) { return __awaiter(this, void 0, void 0, function* () { try { - let eventType = yield (0, prompt_1.shortText)("Event type", "Event type to allow through key", "hello"); - return event_key_event(org, key, eventType, true); - } - catch (e) { - throw e; - } - }); -} -function event_key(org, key) { - return __awaiter(this, void 0, void 0, function* () { - try { - let resp = yield (0, utils_1.sshReq)(`list-events`, `--org`, org, `--key`, key); + let resp = yield (0, utils_1.sshReq)(`list-events`, `--key`, key); let events = JSON.parse(resp); - let options = events.map((x) => { - return { - long: x.event, - text: `disallow ${x.event}`, - action: () => event_key_event(org, key, x.event, false), - }; - }); - options.push({ - long: `new`, - short: `n`, - text: `allow a new event type`, - action: () => event_key_new(org, key), - }); - return yield (0, prompt_1.choice)(options).then((x) => x); + return yield (0, prompt_1.multiSelect)(events, (s) => event_key_events(key, s), "No events in event-catalogue. Make sure you have added events to the event-catalogue and deployed it."); } catch (e) { throw e; @@ -753,8 +729,8 @@ function event(org) { let n = x.name || ""; return { long: x.key, - text: `${x.key} │ ${(0, executors_1.alignLeft)(n, 12)}`, - action: () => event_key(org, x.key), + text: `${x.key} │ ${(0, executors_1.alignLeft)(n, process_1.stdout.getWindowSize()[0] - 36 - 9)}`, + action: () => event_key(x.key), }; }); options.push({ @@ -764,7 +740,7 @@ function event(org) { action: () => keys_key(org, null, ""), }); if (options.length > 1) - (0, executors_1.printTableHeader)(" ", { Key: 36, Name: 12 }); + (0, executors_1.printTableHeader)(" ", { Key: 36, Name: -12 }); return yield (0, prompt_1.choice)(options).then((x) => x); } catch (e) { diff --git a/questions.ts b/questions.ts index 5318c01..7260629 100644 --- a/questions.ts +++ b/questions.ts @@ -8,8 +8,10 @@ import { shortText, spinner_start, spinner_stop, + multiSelect, } from "./prompt"; import { + abort, addToExecuteQueue, directoryNames, execPromise, @@ -914,47 +916,20 @@ async function post(org: string) { } } -function event_key_event( - org: string, - key: string, - event: string, - create: boolean -) { - addToExecuteQueue(() => do_event(org, key, event, create)); +function event_key_events(key: string, events: { [event: string]: boolean }) { + addToExecuteQueue(() => do_event(key, events)); return finish(); } -async function event_key_new(org: string, key: string) { +async function event_key(key: string) { try { - let eventType = await shortText( - "Event type", - "Event type to allow through key", - "hello" + let resp = await sshReq(`list-events`, `--key`, key); + let events: { [key: string]: boolean } = JSON.parse(resp); + return await multiSelect( + events, + (s) => event_key_events(key, s), + "No events in event-catalogue. Make sure you have added events to the event-catalogue and deployed it." ); - return event_key_event(org, key, eventType, true); - } catch (e) { - throw e; - } -} - -async function event_key(org: string, key: string) { - try { - let resp = await sshReq(`list-events`, `--org`, org, `--key`, key); - let events: { event: string }[] = JSON.parse(resp); - let options: Option[] = events.map((x) => { - return { - long: x.event, - text: `disallow ${x.event}`, - action: () => event_key_event(org, key, x.event, false), - }; - }); - options.push({ - long: `new`, - short: `n`, - text: `allow a new event type`, - action: () => event_key_new(org, key), - }); - return await choice(options).then((x) => x); } catch (e) { throw e; } @@ -968,8 +943,8 @@ async function event(org: string) { let n = x.name || ""; return { long: x.key, - text: `${x.key} │ ${alignLeft(n, 12)}`, - action: () => event_key(org, x.key), + text: `${x.key} │ ${alignLeft(n, stdout.getWindowSize()[0] - 36 - 9)}`, + action: () => event_key(x.key), }; }); options.push({ @@ -978,7 +953,7 @@ async function event(org: string) { text: `add a new apikey`, action: () => keys_key(org, null, ""), }); - if (options.length > 1) printTableHeader(" ", { Key: 36, Name: 12 }); + if (options.length > 1) printTableHeader(" ", { Key: 36, Name: -12 }); return await choice(options).then((x) => x); } catch (e) { throw e;