diff --git a/src/cli/index.ts b/src/cli/index.ts index 60b978e..98a6938 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -13,6 +13,8 @@ const getOptions = () => program .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("-j, --json", "prints things to be imported as a JSON", false) + .option("-w, --where ", "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() .opts(); @@ -44,4 +46,8 @@ export const run = async () => { if(options.json) { console.log(JSON.stringify(things, undefined, 3)); } + + if(options.print) { + things.forEach(t => console.log(`${t.UID} ==> ${t.label}`)) + } } \ No newline at end of file diff --git a/src/importer/index.ts b/src/importer/index.ts index a32991c..97725bf 100644 --- a/src/importer/index.ts +++ b/src/importer/index.ts @@ -1,4 +1,4 @@ -import { Config, ImportOptions } from "@types"; +import { Config, ImportOptions, Thing } from "@types"; import { adapters } from "../adapters"; import { DummyOpenHAB, OpenHAB } from "../openhab"; @@ -26,8 +26,16 @@ export const importThings = async (config: Config, options: ImportOptions) => { } const existingThings = await openhab.getThings(); - const existingThingsUIDs = existingThings.map(t => t.UID); - const thingsToImport = things.filter(t => !existingThingsUIDs.includes(t.UID)); + const existingThingsUIDs = existingThings.map(t => 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; } \ No newline at end of file diff --git a/src/types/importer.ts b/src/types/importer.ts index 159d78b..f480e81 100644 --- a/src/types/importer.ts +++ b/src/types/importer.ts @@ -1,5 +1,7 @@ export type ImportOptions = { force: boolean; dryRun: boolean; + print: boolean; sources?: string[]; + where?: string; }; \ No newline at end of file