diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..bcdf28b --- /dev/null +++ b/deno.json @@ -0,0 +1,34 @@ +{ + "name": "@serve.zone/nupst", + "version": "4.0.0", + "exports": "./mod.ts", + "tasks": { + "dev": "deno run --allow-all mod.ts", + "compile": "deno task compile:all", + "compile:all": "bash scripts/compile-all.sh", + "test": "deno test --allow-all tests/", + "check": "deno check mod.ts", + "fmt": "deno fmt", + "lint": "deno lint" + }, + "lint": { + "rules": { + "tags": ["recommended"] + } + }, + "fmt": { + "useTabs": false, + "lineWidth": 100, + "indentWidth": 2, + "semiColons": true + }, + "compilerOptions": { + "lib": ["deno.window"], + "strict": true + }, + "imports": { + "@std/cli": "jsr:@std/cli@^1.0.0", + "@std/fmt": "jsr:@std/fmt@^1.0.0", + "@std/path": "jsr:@std/path@^1.0.0" + } +} diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..8259be1 --- /dev/null +++ b/mod.ts @@ -0,0 +1,44 @@ +#!/usr/bin/env -S deno run --allow-all + +/** + * NUPST - UPS Shutdown Tool + * + * A command-line tool for monitoring SNMP-enabled UPS devices and + * initiating system shutdown when power conditions are critical. + * + * Required Permissions: + * - --allow-net: SNMP communication with UPS devices + * - --allow-read: Read configuration files (/etc/nupst/config.json) + * - --allow-write: Write configuration files + * - --allow-run: Execute system commands (systemctl, shutdown, git, bash) + * - --allow-sys: Access system information (hostname, OS details) + * - --allow-env: Read environment variables + * + * @module + */ + +import { NupstCli } from './ts/cli.ts'; + +/** + * Main entry point for the NUPST application + * Parses command-line arguments and executes the requested command + */ +async function main(): Promise { + const cli = new NupstCli(); + + // Deno.args is already 0-indexed (unlike Node's process.argv which starts at index 2) + // We need to prepend placeholder args to match the existing CLI parser expectations + const args = ['deno', 'mod.ts', ...Deno.args]; + + await cli.parseAndExecute(args); +} + +// Execute main and handle errors +if (import.meta.main) { + try { + await main(); + } catch (error) { + console.error(`Error: ${error instanceof Error ? error.message : String(error)}`); + Deno.exit(1); + } +} diff --git a/ts/cli.ts b/ts/cli.ts index 1692985..b8c481e 100644 --- a/ts/cli.ts +++ b/ts/cli.ts @@ -1,6 +1,6 @@ -import { execSync } from 'child_process'; -import { Nupst } from './nupst.js'; -import { logger } from './logger.js'; +import { execSync } from "node:child_process"; +import { Nupst } from './nupst.ts'; +import { logger } from './logger.ts'; /** * Class for handling CLI commands diff --git a/ts/cli/group-handler.ts b/ts/cli/group-handler.ts index 481043d..9790c41 100644 --- a/ts/cli/group-handler.ts +++ b/ts/cli/group-handler.ts @@ -1,7 +1,7 @@ -import { Nupst } from '../nupst.js'; -import { logger } from '../logger.js'; -import * as helpers from '../helpers/index.js'; -import { type IGroupConfig } from '../daemon.js'; +import { Nupst } from '../nupst.ts'; +import { logger } from '../logger.ts'; +import * as helpers from '../helpers/index.ts'; +import { type IGroupConfig } from '../daemon.ts'; /** * Class for handling group-related CLI commands diff --git a/ts/cli/service-handler.ts b/ts/cli/service-handler.ts index 32c3512..398521b 100644 --- a/ts/cli/service-handler.ts +++ b/ts/cli/service-handler.ts @@ -1,6 +1,6 @@ -import { execSync } from 'child_process'; -import { Nupst } from '../nupst.js'; -import { logger } from '../logger.js'; +import { execSync } from "node:child_process"; +import { Nupst } from '../nupst.ts'; +import { logger } from '../logger.ts'; /** * Class for handling service-related CLI commands diff --git a/ts/cli/ups-handler.ts b/ts/cli/ups-handler.ts index 70c4754..cd6580c 100644 --- a/ts/cli/ups-handler.ts +++ b/ts/cli/ups-handler.ts @@ -1,7 +1,7 @@ -import { execSync } from 'child_process'; -import { Nupst } from '../nupst.js'; -import { logger } from '../logger.js'; -import * as helpers from '../helpers/index.js'; +import { execSync } from "node:child_process"; +import { Nupst } from '../nupst.ts'; +import { logger } from '../logger.ts'; +import * as helpers from '../helpers/index.ts'; /** * Class for handling UPS-related CLI commands diff --git a/ts/daemon.ts b/ts/daemon.ts index 697ef04..2413d17 100644 --- a/ts/daemon.ts +++ b/ts/daemon.ts @@ -1,10 +1,10 @@ -import * as fs from 'fs'; -import * as path from 'path'; -import { exec, execFile } from 'child_process'; -import { promisify } from 'util'; -import { NupstSnmp } from './snmp/manager.js'; -import type { ISnmpConfig } from './snmp/types.js'; -import { logger } from './logger.js'; +import * as fs from "node:fs"; +import * as path from "node:path"; +import { exec, execFile } from "node:child_process"; +import { promisify } from "node:util"; +import { NupstSnmp } from './snmp/manager.ts'; +import type { ISnmpConfig } from './snmp/types.ts'; +import { logger } from './logger.ts'; const execAsync = promisify(exec); const execFileAsync = promisify(execFile); diff --git a/ts/helpers/index.ts b/ts/helpers/index.ts index 61ed984..a881777 100644 --- a/ts/helpers/index.ts +++ b/ts/helpers/index.ts @@ -1 +1 @@ -export * from './shortid.js'; \ No newline at end of file +export * from './shortid.ts'; \ No newline at end of file diff --git a/ts/index.ts b/ts/index.ts index 30824a2..a1d0ac2 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1,7 +1,7 @@ #!/usr/bin/env node -import { NupstCli } from './cli.js'; -import { logger } from './logger.js'; +import { NupstCli } from './cli.ts'; +import { logger } from './logger.ts'; /** * Main entry point for NUPST diff --git a/ts/nupst.ts b/ts/nupst.ts index 3d80cdd..741eba2 100644 --- a/ts/nupst.ts +++ b/ts/nupst.ts @@ -1,12 +1,12 @@ -import { NupstSnmp } from './snmp/manager.js'; -import { NupstDaemon } from './daemon.js'; -import { NupstSystemd } from './systemd.js'; -import { commitinfo } from './00_commitinfo_data.js'; -import { logger } from './logger.js'; -import { UpsHandler } from './cli/ups-handler.js'; -import { GroupHandler } from './cli/group-handler.js'; -import { ServiceHandler } from './cli/service-handler.js'; -import * as https from 'https'; +import { NupstSnmp } from './snmp/manager.ts'; +import { NupstDaemon } from './daemon.ts'; +import { NupstSystemd } from './systemd.ts'; +import { commitinfo } from './00_commitinfo_data.ts'; +import { logger } from './logger.ts'; +import { UpsHandler } from './cli/ups-handler.ts'; +import { GroupHandler } from './cli/group-handler.ts'; +import { ServiceHandler } from './cli/service-handler.ts'; +import * as https from "node:https"; /** * Main Nupst class that coordinates all components diff --git a/ts/snmp/index.ts b/ts/snmp/index.ts index e42119a..41d35b5 100644 --- a/ts/snmp/index.ts +++ b/ts/snmp/index.ts @@ -4,7 +4,7 @@ */ // Re-export all public types -export type { IUpsStatus, IOidSet, TUpsModel, ISnmpConfig } from './types.js'; +export type { IUpsStatus, IOidSet, TUpsModel, ISnmpConfig } from './types.ts'; // Re-export the SNMP manager class -export { NupstSnmp } from './manager.js'; \ No newline at end of file +export { NupstSnmp } from './manager.ts'; \ No newline at end of file diff --git a/ts/snmp/manager.ts b/ts/snmp/manager.ts index 22a607c..1d9ae84 100644 --- a/ts/snmp/manager.ts +++ b/ts/snmp/manager.ts @@ -1,6 +1,6 @@ -import * as snmp from 'net-snmp'; -import type { IOidSet, ISnmpConfig, TUpsModel, IUpsStatus } from './types.js'; -import { UpsOidSets } from './oid-sets.js'; +import * as snmp from "npm:net-snmp@3.20.0"; +import type { IOidSet, ISnmpConfig, TUpsModel, IUpsStatus } from './types.ts'; +import { UpsOidSets } from './oid-sets.ts'; /** * Class for SNMP communication with UPS devices diff --git a/ts/snmp/oid-sets.ts b/ts/snmp/oid-sets.ts index cb6174a..86d07e8 100644 --- a/ts/snmp/oid-sets.ts +++ b/ts/snmp/oid-sets.ts @@ -1,4 +1,4 @@ -import type { IOidSet, TUpsModel } from './types.js'; +import type { IOidSet, TUpsModel } from './types.ts'; /** * OID sets for different UPS models diff --git a/ts/systemd.ts b/ts/systemd.ts index 88c7813..574dd00 100644 --- a/ts/systemd.ts +++ b/ts/systemd.ts @@ -1,7 +1,7 @@ -import { promises as fs } from 'fs'; -import { execSync } from 'child_process'; -import { NupstDaemon } from './daemon.js'; -import { logger } from './logger.js'; +import { promises as fs } from "node:fs"; +import { execSync } from "node:child_process"; +import { NupstDaemon } from './daemon.ts'; +import { logger } from './logger.ts'; /** * Class for managing systemd service