ref: prompt mqtt usage

This commit is contained in:
leukipp
2024-12-17 11:01:42 +01:00
parent d7dd3616bf
commit a9b7820261
+31 -11
View File
@@ -71,8 +71,8 @@ const promptArgs = async (proc) => {
output: proc.stdout, output: proc.stdout,
}); });
// Array of arguments // Array of prompts
const arguments = [ const prompts = [
{ {
key: "web_url", key: "web_url",
question: "\nEnter WEB url", question: "\nEnter WEB url",
@@ -88,6 +88,11 @@ const promptArgs = async (proc) => {
question: "Enter WEB zoom level", question: "Enter WEB zoom level",
fallback: "1.25", fallback: "1.25",
}, },
{
key: "mqtt",
question: "\nConnect to MQTT Broker?",
fallback: "y/N",
},
{ {
key: "mqtt_url", key: "mqtt_url",
question: "\nEnter MQTT url", question: "\nEnter MQTT url",
@@ -111,24 +116,39 @@ const promptArgs = async (proc) => {
{ {
key: "check", key: "check",
question: "\nEverything looks good?", question: "\nEverything looks good?",
fallback: "yes", fallback: "Y/n",
}, },
]; ];
// Prompt each argument and wait for the answer // Prompt questions and wait for the answer
let args = {}; let args = {};
for (const { key, question, fallback } of arguments) { let ignore = [];
if (key === "check") { for (const { key, question, fallback } of prompts) {
if (key === "mqtt") {
const prompt = `${question} (${fallback}): `;
const answer = await read.question(prompt);
const value = (answer.trim() || fallback.match(/[YN]/)[0]).toLowerCase();
if (!["y", "yes"].includes(value)) {
ignore = ignore.concat([
"mqtt_url",
"mqtt_user",
"mqtt_password",
"mqtt_discovery",
]);
}
} else if (key === "check") {
const json = JSON.stringify(args, null, 2); const json = JSON.stringify(args, null, 2);
const prompt = `${question}\n${json}\n(${fallback}): `; const prompt = `${question}\n${json}\n(${fallback}): `;
const answer = (await read.question(prompt)).trim() || fallback; const answer = await read.question(prompt);
if (!["y", "yes"].includes(answer)) { const value = (answer.trim() || fallback.match(/[YN]/)[0]).toLowerCase();
if (!["y", "yes"].includes(value)) {
args = {}; args = {};
} }
} else { } else if (!ignore.includes(key)) {
const prompt = `${question} (${fallback}): `; const prompt = `${question} (${fallback}): `;
const answer = (await read.question(prompt)).trim() || fallback; const answer = await read.question(prompt);
args[key] = answer; const value = answer.trim() || fallback;
args[key] = value;
} }
} }
read.close(); read.close();