Add some useful CLI options
This commit is contained in:
@@ -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 <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()
|
||||
.opts<CLIOptions>();
|
||||
|
||||
@@ -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}`))
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Config, ImportOptions } from "@types";
|
||||
import { Config, ImportOptions, Thing } from "@types";
|
||||
import { adapters } from "../adapters";
|
||||
import { DummyOpenHAB, OpenHAB } from "../openhab";
|
||||
|
||||
@@ -27,7 +27,15 @@ 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 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;
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
export type ImportOptions = {
|
||||
force: boolean;
|
||||
dryRun: boolean;
|
||||
print: boolean;
|
||||
sources?: string[];
|
||||
where?: string;
|
||||
};
|
||||
Reference in New Issue
Block a user