Add some useful CLI options

This commit is contained in:
2024-11-15 19:45:42 +01:00
parent c29c9efe95
commit 70b78c6085
3 changed files with 19 additions and 3 deletions

View File

@@ -13,6 +13,8 @@ const getOptions = () => program
.option("-f, --force", "overrides the existing things with new schemas", false) .option("-f, --force", "overrides the existing things with new schemas", false)
.option("-n, --dry-run", "disables all mutations against OpenHAB (data creating, updating or deletion), only data retrieval will be allowed", false) .option("-n, --dry-run", "disables all mutations against OpenHAB (data creating, updating or deletion), only data retrieval will be allowed", false)
.option("-j, --json", "prints things to be imported as a JSON", false) .option("-j, --json", "prints things to be imported as a JSON", false)
.option("-w, --where <condition>", "filters the things using JS query with the special '$' variable representing a single thing to be imported")
.option("-p, --print", "prints the UIDs and thing names", false)
.parse() .parse()
.opts<CLIOptions>(); .opts<CLIOptions>();
@@ -44,4 +46,8 @@ export const run = async () => {
if(options.json) { if(options.json) {
console.log(JSON.stringify(things, undefined, 3)); console.log(JSON.stringify(things, undefined, 3));
} }
if(options.print) {
things.forEach(t => console.log(`${t.UID} ==> ${t.label}`))
}
} }

View File

@@ -1,4 +1,4 @@
import { Config, ImportOptions } from "@types"; import { Config, ImportOptions, Thing } from "@types";
import { adapters } from "../adapters"; import { adapters } from "../adapters";
import { DummyOpenHAB, OpenHAB } from "../openhab"; import { DummyOpenHAB, OpenHAB } from "../openhab";
@@ -26,8 +26,16 @@ export const importThings = async (config: Config, options: ImportOptions) => {
} }
const existingThings = await openhab.getThings(); const existingThings = await openhab.getThings();
const existingThingsUIDs = existingThings.map(t => t.UID); const existingThingsUIDs = existingThings.map(t => t.UID);
const thingsToImport = things.filter(t => !existingThingsUIDs.includes(t.UID));
const filter: (thing: Thing) => boolean = options.where
? (new Function("$", `return ${options.where}`)) as ((_: Thing) => true)
: (_: Thing) => true;
const thingsToImport = things
.filter(t => !existingThingsUIDs.includes(t.UID))
.filter(filter);
return thingsToImport; return thingsToImport;
} }

View File

@@ -1,5 +1,7 @@
export type ImportOptions = { export type ImportOptions = {
force: boolean; force: boolean;
dryRun: boolean; dryRun: boolean;
print: boolean;
sources?: string[]; sources?: string[];
where?: string;
}; };