From 4f5326c0e9922c57952134699c24c41a632a5fd1 Mon Sep 17 00:00:00 2001 From: ljacobsson Date: Wed, 4 Oct 2023 13:56:38 +0200 Subject: [PATCH] wildcard support --- package.json | 2 +- src/commands/shared/input-util.js | 97 +++++++++++++++++++++++++------ 2 files changed, 81 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 2468cf6..c95bcdc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mhlabs/evb-cli", - "version": "1.2.0", + "version": "1.2.1", "description": "A package for building EventBridge patterns", "main": "index.js", "scripts": { diff --git a/src/commands/shared/input-util.js b/src/commands/shared/input-util.js index b70d9fb..3f043f1 100644 --- a/src/commands/shared/input-util.js +++ b/src/commands/shared/input-util.js @@ -27,16 +27,71 @@ const DONE = "✔ done"; const backNavigation = [BACK, new inquirer.Separator("-------------")]; const doneNavigation = [DONE, UNDO, new inquirer.Separator("-------------")]; const filterRules = [ - "equals", - "equals-ignore-case", - "prefix", - "suffix", - "wildcard", - "anything-but", - "numeric", - "exists", - "null", -].sort(); + { + name: "equals", + value: { + name: "equals", + isArrayable: true, + }, + }, + { + name: "equals-ignore-case", + value: { + name: "equals-ignore-case", + isArrayable: true, + }, + }, + { + name: "prefix", + value: { + name: "prefix", + isArrayable: true, + }, + }, + { + name: "suffix", + value: { + name: "suffix", + isArrayable: true, + }, + }, + { + name: "wildcard", + value: { + name: "wildcard", + isArrayable: false, + }, + }, + { + name: "anything-but", + value: { + name: "anything-but", + isArrayable: true, + }, + }, + { + name: "numeric", + value: { + name: "numeric", + isArrayable: false, + }, + }, + { + name: "exists", + value: { + name: "exists", + isArrayable: false, + }, + }, + { + name: "null", + value: { + name: "null", + isArrayable: false, + }, + }, +].sort((a, b) => a.name.localeCompare(b.name)); + const numericOperators = [">", "<", "=", ">=", "<=", "!=", "range"]; @@ -52,15 +107,15 @@ async function text(message, def) { async function getStringValue(fieldName, type) { const rules = JSON.parse(JSON.stringify(filterRules)); - const rule = await prompt({ + const rule = (await prompt({ name: "id", type: "list", message: `Enter rule for ${fieldName} matching`, choices: [...rules], - }); + })).id; let val = undefined; - if (rule.id !== "exists" && rule.id !== "numeric") { + if (rule.isArrayable) { const value = await prompt({ name: "id", type: "input", @@ -69,9 +124,9 @@ async function getStringValue(fieldName, type) { val = value.id.includes(",") ? value.id.split(",").map((p) => p.trim()) : value.id; - } else if (rule.id === "exists") { + } else if (rule.name === "exists") { val = true; - } else if (rule.id === "numeric") { + } else if (rule.name === "numeric") { const operator = await prompt({ name: "id", type: "list", @@ -99,13 +154,21 @@ async function getStringValue(fieldName, type) { val = [operator.id, parseFloat(value.id)]; } + } else { + const value = await prompt({ + name: "id", + type: "input", + message: `Enter value for ${fieldName}`, + }); + + val = value.id; } let returnObj = {}; - let ruleObj = rule.id === "equals" ? val : undefined; + let ruleObj = rule.name === "equals" ? val : undefined; if (!ruleObj) { ruleObj = {}; - ruleObj[rule.id] = val; + ruleObj[rule.name] = val; } if (!Array.isArray(ruleObj)) { returnObj[fieldName] = [];