Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
ca1039408d | |||
df3e0b9424 | |||
c8e5960abd | |||
7304a62357 | |||
a5a88e53ba | |||
73bc271c59 | |||
1e98181e71 | |||
eb5a8185ae | |||
ef3d3f3fa3 | |||
34e6e850ad | |||
992a776fd2 | |||
3e15a2d52f |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@serve.zone/nupst",
|
"name": "@serve.zone/nupst",
|
||||||
"version": "4.3.1",
|
"version": "5.0.2",
|
||||||
"exports": "./mod.ts",
|
"exports": "./mod.ts",
|
||||||
"tasks": {
|
"tasks": {
|
||||||
"dev": "deno run --allow-all mod.ts",
|
"dev": "deno run --allow-all mod.ts",
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
import * as path from 'node:path';
|
import * as path from 'node:path';
|
||||||
import * as fs from 'node:fs';
|
import * as fs from 'node:fs';
|
||||||
|
import process from 'node:process';
|
||||||
import { exec } from 'node:child_process';
|
import { exec } from 'node:child_process';
|
||||||
import { promisify } from 'node:util';
|
import { promisify } from 'node:util';
|
||||||
import { Action, type IActionConfig, type IActionContext } from './base-action.ts';
|
import { Action, type IActionConfig, type IActionContext } from './base-action.ts';
|
||||||
|
82
ts/cli.ts
82
ts/cli.ts
@@ -127,8 +127,7 @@ export class NupstCli {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'remove':
|
case 'remove':
|
||||||
case 'rm': // Alias
|
case 'rm': {
|
||||||
case 'delete': { // Backward compatibility
|
|
||||||
const upsIdToRemove = subcommandArgs[0];
|
const upsIdToRemove = subcommandArgs[0];
|
||||||
if (!upsIdToRemove) {
|
if (!upsIdToRemove) {
|
||||||
logger.error('UPS ID is required for remove command');
|
logger.error('UPS ID is required for remove command');
|
||||||
@@ -172,8 +171,7 @@ export class NupstCli {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'remove':
|
case 'remove':
|
||||||
case 'rm': // Alias
|
case 'rm': {
|
||||||
case 'delete': { // Backward compatibility
|
|
||||||
const groupIdToRemove = subcommandArgs[0];
|
const groupIdToRemove = subcommandArgs[0];
|
||||||
if (!groupIdToRemove) {
|
if (!groupIdToRemove) {
|
||||||
logger.error('Group ID is required for remove command');
|
logger.error('Group ID is required for remove command');
|
||||||
@@ -206,8 +204,7 @@ export class NupstCli {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'remove':
|
case 'remove':
|
||||||
case 'rm': // Alias
|
case 'rm': {
|
||||||
case 'delete': { // Backward compatibility
|
|
||||||
const upsId = subcommandArgs[0];
|
const upsId = subcommandArgs[0];
|
||||||
const actionIndex = subcommandArgs[1];
|
const actionIndex = subcommandArgs[1];
|
||||||
await actionHandler.remove(upsId, actionIndex);
|
await actionHandler.remove(upsId, actionIndex);
|
||||||
@@ -242,72 +239,8 @@ export class NupstCli {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle top-level commands and backward compatibility
|
// Handle top-level commands
|
||||||
switch (command) {
|
switch (command) {
|
||||||
// Backward compatibility - old UPS commands
|
|
||||||
case 'add':
|
|
||||||
logger.log("Note: 'nupst add' is deprecated. Use 'nupst ups add' instead.");
|
|
||||||
await upsHandler.add();
|
|
||||||
break;
|
|
||||||
case 'edit':
|
|
||||||
logger.log("Note: 'nupst edit' is deprecated. Use 'nupst ups edit' instead.");
|
|
||||||
await upsHandler.edit(commandArgs[0]);
|
|
||||||
break;
|
|
||||||
case 'delete':
|
|
||||||
logger.log("Note: 'nupst delete' is deprecated. Use 'nupst ups remove' instead.");
|
|
||||||
if (!commandArgs[0]) {
|
|
||||||
logger.error('UPS ID is required for delete command');
|
|
||||||
this.showHelp();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await upsHandler.remove(commandArgs[0]);
|
|
||||||
break;
|
|
||||||
case 'list':
|
|
||||||
logger.log("Note: 'nupst list' is deprecated. Use 'nupst ups list' instead.");
|
|
||||||
await upsHandler.list();
|
|
||||||
break;
|
|
||||||
case 'test':
|
|
||||||
logger.log("Note: 'nupst test' is deprecated. Use 'nupst ups test' instead.");
|
|
||||||
await upsHandler.test(debugMode);
|
|
||||||
break;
|
|
||||||
case 'setup':
|
|
||||||
logger.log("Note: 'nupst setup' is deprecated. Use 'nupst ups edit' instead.");
|
|
||||||
await upsHandler.edit(undefined);
|
|
||||||
break;
|
|
||||||
|
|
||||||
// Backward compatibility - old service commands
|
|
||||||
case 'enable':
|
|
||||||
logger.log("Note: 'nupst enable' is deprecated. Use 'nupst service enable' instead.");
|
|
||||||
await serviceHandler.enable();
|
|
||||||
break;
|
|
||||||
case 'disable':
|
|
||||||
logger.log("Note: 'nupst disable' is deprecated. Use 'nupst service disable' instead.");
|
|
||||||
await serviceHandler.disable();
|
|
||||||
break;
|
|
||||||
case 'start':
|
|
||||||
logger.log("Note: 'nupst start' is deprecated. Use 'nupst service start' instead.");
|
|
||||||
await serviceHandler.start();
|
|
||||||
break;
|
|
||||||
case 'stop':
|
|
||||||
logger.log("Note: 'nupst stop' is deprecated. Use 'nupst service stop' instead.");
|
|
||||||
await serviceHandler.stop();
|
|
||||||
break;
|
|
||||||
case 'status':
|
|
||||||
logger.log("Note: 'nupst status' is deprecated. Use 'nupst service status' instead.");
|
|
||||||
await serviceHandler.status();
|
|
||||||
break;
|
|
||||||
case 'logs':
|
|
||||||
logger.log("Note: 'nupst logs' is deprecated. Use 'nupst service logs' instead.");
|
|
||||||
await serviceHandler.logs();
|
|
||||||
break;
|
|
||||||
case 'daemon-start':
|
|
||||||
logger.log(
|
|
||||||
"Note: 'nupst daemon-start' is deprecated. Use 'nupst service start-daemon' instead.",
|
|
||||||
);
|
|
||||||
await serviceHandler.daemonStart(debugMode);
|
|
||||||
break;
|
|
||||||
|
|
||||||
// Top-level commands (no changes)
|
|
||||||
case 'update':
|
case 'update':
|
||||||
await serviceHandler.update();
|
await serviceHandler.update();
|
||||||
break;
|
break;
|
||||||
@@ -589,11 +522,6 @@ export class NupstCli {
|
|||||||
logger.dim(' nupst group list # Show all configured groups');
|
logger.dim(' nupst group list # Show all configured groups');
|
||||||
logger.dim(' nupst config # Display current configuration');
|
logger.dim(' nupst config # Display current configuration');
|
||||||
console.log('');
|
console.log('');
|
||||||
|
|
||||||
// Note about deprecated commands
|
|
||||||
logger.warn('Note: Old command format (e.g., \'nupst add\') still works but is deprecated.');
|
|
||||||
logger.dim(' Use the new format (e.g., \'nupst ups add\') going forward.');
|
|
||||||
console.log('');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -692,7 +620,7 @@ Usage:
|
|||||||
|
|
||||||
Subcommands:
|
Subcommands:
|
||||||
add <ups-id|group-id> - Add a new action to a UPS or group interactively
|
add <ups-id|group-id> - Add a new action to a UPS or group interactively
|
||||||
remove <ups-id|group-id> <index> - Remove an action by index (alias: rm, delete)
|
remove <ups-id|group-id> <index> - Remove an action by index (alias: rm)
|
||||||
list [ups-id|group-id] - List all actions (optionally for specific target) (alias: ls)
|
list [ups-id|group-id] - List all actions (optionally for specific target) (alias: ls)
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
@@ -152,10 +152,7 @@ export class ActionHandler {
|
|||||||
|
|
||||||
logger.log('');
|
logger.log('');
|
||||||
logger.success(`Action added to ${targetType} ${targetName}`);
|
logger.success(`Action added to ${targetType} ${targetName}`);
|
||||||
logger.log('');
|
logger.log(` ${theme.dim('Changes saved and will be applied automatically')}`);
|
||||||
logger.log(
|
|
||||||
` ${theme.dim('Restart service to apply changes:')} ${theme.command('nupst service restart')}`,
|
|
||||||
);
|
|
||||||
logger.log('');
|
logger.log('');
|
||||||
} finally {
|
} finally {
|
||||||
rl.close();
|
rl.close();
|
||||||
@@ -241,10 +238,7 @@ export class ActionHandler {
|
|||||||
` ${theme.dim('Thresholds:')} Battery: ${removedAction.thresholds.battery}%, Runtime: ${removedAction.thresholds.runtime}min`,
|
` ${theme.dim('Thresholds:')} Battery: ${removedAction.thresholds.battery}%, Runtime: ${removedAction.thresholds.runtime}min`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
logger.log('');
|
logger.log(` ${theme.dim('Changes saved and will be applied automatically')}`);
|
||||||
logger.log(
|
|
||||||
` ${theme.dim('Restart service to apply changes:')} ${theme.command('nupst service restart')}`,
|
|
||||||
);
|
|
||||||
logger.log('');
|
logger.log('');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(
|
logger.error(
|
||||||
|
@@ -277,6 +277,9 @@ WantedBy=multi-user.target
|
|||||||
for (const ups of config.upsDevices) {
|
for (const ups of config.upsDevices) {
|
||||||
await this.displaySingleUpsStatus(ups, snmp);
|
await this.displaySingleUpsStatus(ups, snmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Display groups after UPS devices
|
||||||
|
this.displayGroupsStatus();
|
||||||
} else if (config.snmp) {
|
} else if (config.snmp) {
|
||||||
// Legacy single UPS configuration (v1/v2 format)
|
// Legacy single UPS configuration (v1/v2 format)
|
||||||
logger.info('UPS Devices (1):');
|
logger.info('UPS Devices (1):');
|
||||||
@@ -365,6 +368,27 @@ WantedBy=multi-user.target
|
|||||||
logger.log(` ${theme.dim(`Groups: ${groupNames.join(', ')}`)}`);
|
logger.log(` ${theme.dim(`Groups: ${groupNames.join(', ')}`)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Display actions if any
|
||||||
|
if (ups.actions && ups.actions.length > 0) {
|
||||||
|
for (const action of ups.actions) {
|
||||||
|
let actionDesc = `${action.type}`;
|
||||||
|
if (action.thresholds) {
|
||||||
|
actionDesc += ` (${action.triggerMode || 'onlyThresholds'}: battery<${action.thresholds.battery}%, runtime<${action.thresholds.runtime}min`;
|
||||||
|
if (action.shutdownDelay) {
|
||||||
|
actionDesc += `, delay=${action.shutdownDelay}s`;
|
||||||
|
}
|
||||||
|
actionDesc += ')';
|
||||||
|
} else {
|
||||||
|
actionDesc += ` (${action.triggerMode || 'onlyPowerChanges'}`;
|
||||||
|
if (action.shutdownDelay) {
|
||||||
|
actionDesc += `, delay=${action.shutdownDelay}s`;
|
||||||
|
}
|
||||||
|
actionDesc += ')';
|
||||||
|
}
|
||||||
|
logger.log(` ${theme.dim('Action:')} ${theme.info(actionDesc)}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
logger.log('');
|
logger.log('');
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -376,6 +400,69 @@ WantedBy=multi-user.target
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display status of all groups
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
private displayGroupsStatus(): void {
|
||||||
|
const config = this.daemon.getConfig();
|
||||||
|
|
||||||
|
if (!config.groups || config.groups.length === 0) {
|
||||||
|
return; // No groups to display
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.log('');
|
||||||
|
logger.info(`Groups (${config.groups.length}):`);
|
||||||
|
|
||||||
|
for (const group of config.groups) {
|
||||||
|
// Display group name and mode
|
||||||
|
const modeColor = group.mode === 'redundant' ? theme.success : theme.warning;
|
||||||
|
logger.log(
|
||||||
|
` ${symbols.info} ${theme.highlight(group.name)} ${theme.dim(`(${modeColor(group.mode)})`)}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Display description if present
|
||||||
|
if (group.description) {
|
||||||
|
logger.log(` ${theme.dim(group.description)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Display UPS devices in this group
|
||||||
|
const upsInGroup = config.upsDevices.filter((ups) =>
|
||||||
|
ups.groups && ups.groups.includes(group.id)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (upsInGroup.length > 0) {
|
||||||
|
const upsNames = upsInGroup.map((ups) => ups.name).join(', ');
|
||||||
|
logger.log(` ${theme.dim(`UPS Devices (${upsInGroup.length}):`)} ${upsNames}`);
|
||||||
|
} else {
|
||||||
|
logger.log(` ${theme.dim('UPS Devices: None')}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Display actions if any
|
||||||
|
if (group.actions && group.actions.length > 0) {
|
||||||
|
for (const action of group.actions) {
|
||||||
|
let actionDesc = `${action.type}`;
|
||||||
|
if (action.thresholds) {
|
||||||
|
actionDesc += ` (${action.triggerMode || 'onlyThresholds'}: battery<${action.thresholds.battery}%, runtime<${action.thresholds.runtime}min`;
|
||||||
|
if (action.shutdownDelay) {
|
||||||
|
actionDesc += `, delay=${action.shutdownDelay}s`;
|
||||||
|
}
|
||||||
|
actionDesc += ')';
|
||||||
|
} else {
|
||||||
|
actionDesc += ` (${action.triggerMode || 'onlyPowerChanges'}`;
|
||||||
|
if (action.shutdownDelay) {
|
||||||
|
actionDesc += `, delay=${action.shutdownDelay}s`;
|
||||||
|
}
|
||||||
|
actionDesc += ')';
|
||||||
|
}
|
||||||
|
logger.log(` ${theme.dim('Action:')} ${theme.info(actionDesc)}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.log('');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disable and uninstall the systemd service
|
* Disable and uninstall the systemd service
|
||||||
* @throws Error if disabling fails
|
* @throws Error if disabling fails
|
||||||
|
Reference in New Issue
Block a user