fix(cli): Improve CLI logging consistency by replacing direct console output with unified logger calls.

This commit is contained in:
Philipp Kunz 2025-03-26 22:38:24 +00:00
parent 39bf3e2239
commit 45ee8208b5
3 changed files with 177 additions and 152 deletions

View File

@ -1,5 +1,12 @@
# Changelog # Changelog
## 2025-03-26 - 2.6.16 - fix(cli)
Improve CLI logging consistency by replacing direct console output with unified logger calls.
- Replaced console.log and console.error with logger.log and logger.error in CLI commands
- Standardized debug, error, and status messages using logger's logbox utilities
- Enhanced consistency of log output throughout the ts/cli.ts file
## 2025-03-26 - 2.6.15 - fix(logger) ## 2025-03-26 - 2.6.15 - fix(logger)
Replace direct console logging with unified logger interface for consistent formatting Replace direct console logging with unified logger interface for consistent formatting

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@serve.zone/nupst', name: '@serve.zone/nupst',
version: '2.6.15', version: '2.6.16',
description: 'Node.js UPS Shutdown Tool for SNMP-enabled UPS devices' description: 'Node.js UPS Shutdown Tool for SNMP-enabled UPS devices'
} }

320
ts/cli.ts
View File

@ -27,7 +27,7 @@ export class NupstCli {
// Extract debug flag from any position // Extract debug flag from any position
const debugOptions = this.extractDebugOptions(args); const debugOptions = this.extractDebugOptions(args);
if (debugOptions.debugMode) { if (debugOptions.debugMode) {
console.log('Debug mode enabled'); logger.log('Debug mode enabled');
// Enable debug mode in the SNMP client // Enable debug mode in the SNMP client
this.nupst.getSnmp().enableDebug(); this.nupst.getSnmp().enableDebug();
} }
@ -120,7 +120,7 @@ export class NupstCli {
private async enable(): Promise<void> { private async enable(): Promise<void> {
this.checkRootAccess('This command must be run as root.'); this.checkRootAccess('This command must be run as root.');
await this.nupst.getSystemd().install(); await this.nupst.getSystemd().install();
console.log('NUPST service has been installed. Use "nupst start" to start the service.'); logger.log('NUPST service has been installed. Use "nupst start" to start the service.');
} }
/** /**
@ -128,12 +128,12 @@ export class NupstCli {
* @param debugMode Whether to enable debug mode * @param debugMode Whether to enable debug mode
*/ */
private async daemonStart(debugMode: boolean = false): Promise<void> { private async daemonStart(debugMode: boolean = false): Promise<void> {
console.log('Starting NUPST daemon...'); logger.log('Starting NUPST daemon...');
try { try {
// Enable debug mode for SNMP if requested // Enable debug mode for SNMP if requested
if (debugMode) { if (debugMode) {
this.nupst.getSnmp().enableDebug(); this.nupst.getSnmp().enableDebug();
console.log('SNMP debug mode enabled'); logger.log('SNMP debug mode enabled');
} }
await this.nupst.getDaemon().start(); await this.nupst.getDaemon().start();
} catch (error) { } catch (error) {
@ -149,7 +149,7 @@ export class NupstCli {
try { try {
// Use exec with spawn to properly follow logs in real-time // Use exec with spawn to properly follow logs in real-time
const { spawn } = await import('child_process'); const { spawn } = await import('child_process');
console.log('Tailing nupst service logs (Ctrl+C to exit)...\n'); logger.log('Tailing nupst service logs (Ctrl+C to exit)...\n');
const journalctl = spawn('journalctl', ['-u', 'nupst.service', '-n', '50', '-f'], { const journalctl = spawn('journalctl', ['-u', 'nupst.service', '-n', '50', '-f'], {
stdio: ['ignore', 'inherit', 'inherit'], stdio: ['ignore', 'inherit', 'inherit'],
@ -166,7 +166,7 @@ export class NupstCli {
journalctl.on('exit', () => resolve()); journalctl.on('exit', () => resolve());
}); });
} catch (error) { } catch (error) {
console.error('Failed to retrieve logs:', error); logger.error(`Failed to retrieve logs: ${error}`);
process.exit(1); process.exit(1);
} }
} }
@ -213,7 +213,7 @@ export class NupstCli {
*/ */
private checkRootAccess(errorMessage: string): void { private checkRootAccess(errorMessage: string): void {
if (process.getuid && process.getuid() !== 0) { if (process.getuid && process.getuid() !== 0) {
console.error(errorMessage); logger.error(errorMessage);
process.exit(1); process.exit(1);
} }
} }
@ -226,19 +226,21 @@ export class NupstCli {
try { try {
// Debug mode is now handled in parseAndExecute // Debug mode is now handled in parseAndExecute
if (debugMode) { if (debugMode) {
console.log('┌─ Debug Mode ─────────────────────────────┐'); const boxWidth = 45;
console.log('│ SNMP debugging enabled - detailed logs will be shown'); logger.logBoxTitle('Debug Mode', boxWidth);
console.log('└──────────────────────────────────────────┘'); logger.logBoxLine('SNMP debugging enabled - detailed logs will be shown');
logger.logBoxEnd();
} }
// Try to load the configuration // Try to load the configuration
try { try {
await this.nupst.getDaemon().loadConfig(); await this.nupst.getDaemon().loadConfig();
} catch (error) { } catch (error) {
console.error('┌─ Configuration Error ─────────────────────┐'); const errorBoxWidth = 45;
console.error('│ No configuration found.'); logger.logBoxTitle('Configuration Error', errorBoxWidth);
console.error("│ Please run 'nupst setup' first to create a configuration."); logger.logBoxLine('No configuration found.');
console.error('└──────────────────────────────────────────┘'); logger.logBoxLine("Please run 'nupst setup' first to create a configuration.");
logger.logBoxEnd();
return; return;
} }
@ -248,7 +250,7 @@ export class NupstCli {
this.displayTestConfig(config); this.displayTestConfig(config);
await this.testConnection(config); await this.testConnection(config);
} catch (error) { } catch (error) {
console.error(`Test failed: ${error.message}`); logger.error(`Test failed: ${error.message}`);
} }
} }
@ -257,44 +259,45 @@ export class NupstCli {
* @param config Current configuration * @param config Current configuration
*/ */
private displayTestConfig(config: any): void { private displayTestConfig(config: any): void {
console.log('┌─ Testing Configuration ─────────────────────┐'); const boxWidth = 45;
console.log('│ SNMP Settings:'); logger.logBoxTitle('Testing Configuration', boxWidth);
console.log(`│ Host: ${config.snmp.host}`); logger.logBoxLine('SNMP Settings:');
console.log(`│ Port: ${config.snmp.port}`); logger.logBoxLine(` Host: ${config.snmp.host}`);
console.log(`│ Version: ${config.snmp.version}`); logger.logBoxLine(` Port: ${config.snmp.port}`);
console.log(`│ UPS Model: ${config.snmp.upsModel || 'cyberpower'}`); logger.logBoxLine(` Version: ${config.snmp.version}`);
logger.logBoxLine(` UPS Model: ${config.snmp.upsModel || 'cyberpower'}`);
if (config.snmp.version === 1 || config.snmp.version === 2) { if (config.snmp.version === 1 || config.snmp.version === 2) {
console.log(` Community: ${config.snmp.community}`); logger.logBoxLine(` Community: ${config.snmp.community}`);
} else if (config.snmp.version === 3) { } else if (config.snmp.version === 3) {
console.log(` Security Level: ${config.snmp.securityLevel}`); logger.logBoxLine(` Security Level: ${config.snmp.securityLevel}`);
console.log(` Username: ${config.snmp.username}`); logger.logBoxLine(` Username: ${config.snmp.username}`);
// Show auth and privacy details based on security level // Show auth and privacy details based on security level
if (config.snmp.securityLevel === 'authNoPriv' || config.snmp.securityLevel === 'authPriv') { if (config.snmp.securityLevel === 'authNoPriv' || config.snmp.securityLevel === 'authPriv') {
console.log(` Auth Protocol: ${config.snmp.authProtocol || 'None'}`); logger.logBoxLine(` Auth Protocol: ${config.snmp.authProtocol || 'None'}`);
} }
if (config.snmp.securityLevel === 'authPriv') { if (config.snmp.securityLevel === 'authPriv') {
console.log(` Privacy Protocol: ${config.snmp.privProtocol || 'None'}`); logger.logBoxLine(` Privacy Protocol: ${config.snmp.privProtocol || 'None'}`);
} }
// Show timeout value // Show timeout value
console.log(` Timeout: ${config.snmp.timeout / 1000} seconds`); logger.logBoxLine(` Timeout: ${config.snmp.timeout / 1000} seconds`);
} }
// Show OIDs if custom model is selected // Show OIDs if custom model is selected
if (config.snmp.upsModel === 'custom' && config.snmp.customOIDs) { if (config.snmp.upsModel === 'custom' && config.snmp.customOIDs) {
console.log('│ Custom OIDs:'); logger.logBoxLine('Custom OIDs:');
console.log(` Power Status: ${config.snmp.customOIDs.POWER_STATUS || 'Not set'}`); logger.logBoxLine(` Power Status: ${config.snmp.customOIDs.POWER_STATUS || 'Not set'}`);
console.log(` Battery Capacity: ${config.snmp.customOIDs.BATTERY_CAPACITY || 'Not set'}`); logger.logBoxLine(` Battery Capacity: ${config.snmp.customOIDs.BATTERY_CAPACITY || 'Not set'}`);
console.log(` Battery Runtime: ${config.snmp.customOIDs.BATTERY_RUNTIME || 'Not set'}`); logger.logBoxLine(` Battery Runtime: ${config.snmp.customOIDs.BATTERY_RUNTIME || 'Not set'}`);
} }
console.log('│ Thresholds:'); logger.logBoxLine('Thresholds:');
console.log(` Battery: ${config.thresholds.battery}%`); logger.logBoxLine(` Battery: ${config.thresholds.battery}%`);
console.log(` Runtime: ${config.thresholds.runtime} minutes`); logger.logBoxLine(` Runtime: ${config.thresholds.runtime} minutes`);
console.log(`Check Interval: ${config.checkInterval / 1000} seconds`); logger.logBoxLine(`Check Interval: ${config.checkInterval / 1000} seconds`);
console.log('└──────────────────────────────────────────┘'); logger.logBoxEnd();
} }
/** /**
@ -302,7 +305,7 @@ export class NupstCli {
* @param config Current configuration * @param config Current configuration
*/ */
private async testConnection(config: any): Promise<void> { private async testConnection(config: any): Promise<void> {
console.log('\nTesting connection to UPS...'); logger.log('\nTesting connection to UPS...');
try { try {
// Create a test config with a short timeout // Create a test config with a short timeout
const testConfig = { const testConfig = {
@ -312,22 +315,24 @@ export class NupstCli {
const status = await this.nupst.getSnmp().getUpsStatus(testConfig); const status = await this.nupst.getSnmp().getUpsStatus(testConfig);
console.log('┌─ Connection Successful! ─────────────────┐'); const boxWidth = 45;
console.log('│ UPS Status:'); logger.logBoxTitle('Connection Successful!', boxWidth);
console.log(`│ Power Status: ${status.powerStatus}`); logger.logBoxLine('UPS Status:');
console.log(`│ Battery Capacity: ${status.batteryCapacity}%`); logger.logBoxLine(` Power Status: ${status.powerStatus}`);
console.log(`│ Runtime Remaining: ${status.batteryRuntime} minutes`); logger.logBoxLine(` Battery Capacity: ${status.batteryCapacity}%`);
console.log('└──────────────────────────────────────────┘'); logger.logBoxLine(` Runtime Remaining: ${status.batteryRuntime} minutes`);
logger.logBoxEnd();
// Check status against thresholds if on battery // Check status against thresholds if on battery
if (status.powerStatus === 'onBattery') { if (status.powerStatus === 'onBattery') {
this.analyzeThresholds(status, config); this.analyzeThresholds(status, config);
} }
} catch (error) { } catch (error) {
console.error('┌─ Connection Failed! ───────────────────────┐'); const errorBoxWidth = 45;
console.error(`│ Error: ${error.message}`); logger.logBoxTitle('Connection Failed!', errorBoxWidth);
console.error('└──────────────────────────────────────────┘'); logger.logBoxLine(`Error: ${error.message}`);
console.log("\nPlease check your settings and run 'nupst setup' to reconfigure."); logger.logBoxEnd();
logger.log("\nPlease check your settings and run 'nupst setup' to reconfigure.");
} }
} }
@ -337,42 +342,43 @@ export class NupstCli {
* @param config Current configuration * @param config Current configuration
*/ */
private analyzeThresholds(status: any, config: any): void { private analyzeThresholds(status: any, config: any): void {
console.log('┌─ Threshold Analysis ───────────────────────┐'); const boxWidth = 45;
logger.logBoxTitle('Threshold Analysis', boxWidth);
if (status.batteryCapacity < config.thresholds.battery) { if (status.batteryCapacity < config.thresholds.battery) {
console.log('│ ⚠️ WARNING: Battery capacity below threshold'); logger.logBoxLine('⚠️ WARNING: Battery capacity below threshold');
console.log( logger.logBoxLine(
` Current: ${status.batteryCapacity}% | Threshold: ${config.thresholds.battery}%` ` Current: ${status.batteryCapacity}% | Threshold: ${config.thresholds.battery}%`
); );
console.log('│ System would initiate shutdown'); logger.logBoxLine(' System would initiate shutdown');
} else { } else {
console.log('│ ✓ Battery capacity above threshold'); logger.logBoxLine('✓ Battery capacity above threshold');
console.log( logger.logBoxLine(
` Current: ${status.batteryCapacity}% | Threshold: ${config.thresholds.battery}%` ` Current: ${status.batteryCapacity}% | Threshold: ${config.thresholds.battery}%`
); );
} }
if (status.batteryRuntime < config.thresholds.runtime) { if (status.batteryRuntime < config.thresholds.runtime) {
console.log('│ ⚠️ WARNING: Runtime below threshold'); logger.logBoxLine('⚠️ WARNING: Runtime below threshold');
console.log( logger.logBoxLine(
` Current: ${status.batteryRuntime} min | Threshold: ${config.thresholds.runtime} min` ` Current: ${status.batteryRuntime} min | Threshold: ${config.thresholds.runtime} min`
); );
console.log('│ System would initiate shutdown'); logger.logBoxLine(' System would initiate shutdown');
} else { } else {
console.log('│ ✓ Runtime above threshold'); logger.logBoxLine('✓ Runtime above threshold');
console.log( logger.logBoxLine(
` Current: ${status.batteryRuntime} min | Threshold: ${config.thresholds.runtime} min` ` Current: ${status.batteryRuntime} min | Threshold: ${config.thresholds.runtime} min`
); );
} }
console.log('└──────────────────────────────────────────┘'); logger.logBoxEnd();
} }
/** /**
* Display help message * Display help message
*/ */
private showHelp(): void { private showHelp(): void {
console.log(` logger.log(`
NUPST - Node.js UPS Shutdown Tool NUPST - Node.js UPS Shutdown Tool
Usage: Usage:
@ -406,8 +412,9 @@ Options:
'This command must be run as root to update NUPST and refresh the systemd service.' 'This command must be run as root to update NUPST and refresh the systemd service.'
); );
console.log('┌─ NUPST Update Process ──────────────────┐'); const boxWidth = 45;
console.log('│ Updating NUPST from repository...'); logger.logBoxTitle('NUPST Update Process', boxWidth);
logger.logBoxLine('Updating NUPST from repository...');
// Determine the installation directory (assuming it's either /opt/nupst or the current directory) // Determine the installation directory (assuming it's either /opt/nupst or the current directory)
const { existsSync } = await import('fs'); const { existsSync } = await import('fs');
@ -417,26 +424,26 @@ Options:
// If not installed in /opt/nupst, use the current directory // If not installed in /opt/nupst, use the current directory
const { dirname } = await import('path'); const { dirname } = await import('path');
installDir = dirname(dirname(process.argv[1])); // Go up two levels from the executable installDir = dirname(dirname(process.argv[1])); // Go up two levels from the executable
console.log(`Using local installation directory: ${installDir}`); logger.logBoxLine(`Using local installation directory: ${installDir}`);
} }
try { try {
// 1. Update the repository // 1. Update the repository
console.log('│ Pulling latest changes from git repository...'); logger.logBoxLine('Pulling latest changes from git repository...');
execSync(`cd ${installDir} && git fetch origin && git reset --hard origin/main`, { execSync(`cd ${installDir} && git fetch origin && git reset --hard origin/main`, {
stdio: 'pipe', stdio: 'pipe',
}); });
// 2. Run the install.sh script // 2. Run the install.sh script
console.log('│ Running install.sh to update NUPST...'); logger.logBoxLine('Running install.sh to update NUPST...');
execSync(`cd ${installDir} && bash ./install.sh`, { stdio: 'pipe' }); execSync(`cd ${installDir} && bash ./install.sh`, { stdio: 'pipe' });
// 3. Run the setup.sh script with force flag to update Node.js and dependencies // 3. Run the setup.sh script with force flag to update Node.js and dependencies
console.log('│ Running setup.sh to update Node.js and dependencies...'); logger.logBoxLine('Running setup.sh to update Node.js and dependencies...');
execSync(`cd ${installDir} && bash ./setup.sh --force`, { stdio: 'pipe' }); execSync(`cd ${installDir} && bash ./setup.sh --force`, { stdio: 'pipe' });
// 4. Refresh the systemd service // 4. Refresh the systemd service
console.log('│ Refreshing systemd service...'); logger.logBoxLine('Refreshing systemd service...');
// First check if service exists // First check if service exists
let serviceExists = false; let serviceExists = false;
@ -453,34 +460,34 @@ Options:
const isRunning = const isRunning =
execSync('systemctl is-active nupst.service || true').toString().trim() === 'active'; execSync('systemctl is-active nupst.service || true').toString().trim() === 'active';
if (isRunning) { if (isRunning) {
console.log('│ Stopping nupst service...'); logger.logBoxLine('Stopping nupst service...');
execSync('systemctl stop nupst.service'); execSync('systemctl stop nupst.service');
} }
// Reinstall the service // Reinstall the service
console.log('│ Reinstalling systemd service...'); logger.logBoxLine('Reinstalling systemd service...');
await this.nupst.getSystemd().install(); await this.nupst.getSystemd().install();
// Restart the service if it was running // Restart the service if it was running
if (isRunning) { if (isRunning) {
console.log('│ Restarting nupst service...'); logger.logBoxLine('Restarting nupst service...');
execSync('systemctl start nupst.service'); execSync('systemctl start nupst.service');
} }
} else { } else {
console.log('│ Systemd service not installed, skipping service refresh.'); logger.logBoxLine('Systemd service not installed, skipping service refresh.');
console.log('│ Run "nupst enable" to install the service.'); logger.logBoxLine('Run "nupst enable" to install the service.');
} }
console.log('│ Update completed successfully!'); logger.logBoxLine('Update completed successfully!');
console.log('└─────────────────────────────────────────────┘'); logger.logBoxEnd();
} catch (error) { } catch (error) {
console.error('│ Error during update process:'); logger.logBoxLine('Error during update process:');
console.error(`${error.message}`); logger.logBoxLine(`${error.message}`);
console.error('└─────────────────────────────────────────────┘'); logger.logBoxEnd();
process.exit(1); process.exit(1);
} }
} catch (error) { } catch (error) {
console.error(`Update failed: ${error.message}`); logger.error(`Update failed: ${error.message}`);
process.exit(1); process.exit(1);
} }
} }
@ -513,7 +520,7 @@ Options:
rl.close(); rl.close();
} }
} catch (error) { } catch (error) {
console.error('Setup error:', error.message); logger.error(`Setup error: ${error.message}`);
} }
} }
@ -522,9 +529,9 @@ Options:
* @param prompt Function to prompt for user input * @param prompt Function to prompt for user input
*/ */
private async runSetupProcess(prompt: (question: string) => Promise<string>): Promise<void> { private async runSetupProcess(prompt: (question: string) => Promise<string>): Promise<void> {
console.log('\nNUPST Interactive Setup'); logger.log('\nNUPST Interactive Setup');
console.log('======================\n'); logger.log('======================\n');
console.log('This will guide you through configuring your UPS SNMP settings.\n'); logger.log('This will guide you through configuring your UPS SNMP settings.\n');
// Try to load existing config if available // Try to load existing config if available
let config; let config;
@ -534,7 +541,7 @@ Options:
} catch (error) { } catch (error) {
// If config doesn't exist, use default config // If config doesn't exist, use default config
config = this.nupst.getDaemon().getConfig(); config = this.nupst.getDaemon().getConfig();
console.log('No existing configuration found. Creating a new configuration.'); logger.log('No existing configuration found. Creating a new configuration.');
} }
// Gather SNMP settings // Gather SNMP settings
@ -557,7 +564,7 @@ Options:
// Check if service is running and restart it if needed // Check if service is running and restart it if needed
await this.restartServiceIfRunning(); await this.restartServiceIfRunning();
console.log('\nSetup complete!'); logger.log('\nSetup complete!');
await this.optionallyEnableService(prompt); await this.optionallyEnableService(prompt);
} }
@ -860,15 +867,18 @@ Options:
* @param config Current configuration * @param config Current configuration
*/ */
private displayConfigSummary(config: any): void { private displayConfigSummary(config: any): void {
console.log('\n┌─ Configuration Summary ─────────────────┐'); const boxWidth = 45;
console.log(`│ SNMP Host: ${config.snmp.host}:${config.snmp.port}`); logger.log('');
console.log(`│ SNMP Version: ${config.snmp.version}`); logger.logBoxTitle('Configuration Summary', boxWidth);
console.log(`│ UPS Model: ${config.snmp.upsModel}`); logger.logBoxLine(`SNMP Host: ${config.snmp.host}:${config.snmp.port}`);
console.log( logger.logBoxLine(`SNMP Version: ${config.snmp.version}`);
`│ Thresholds: ${config.thresholds.battery}% battery, ${config.thresholds.runtime} min runtime` logger.logBoxLine(`UPS Model: ${config.snmp.upsModel}`);
logger.logBoxLine(
`Thresholds: ${config.thresholds.battery}% battery, ${config.thresholds.runtime} min runtime`
); );
console.log(`│ Check Interval: ${config.checkInterval / 1000} seconds`); logger.logBoxLine(`Check Interval: ${config.checkInterval / 1000} seconds`);
console.log('└──────────────────────────────────────────┘\n'); logger.logBoxEnd();
logger.log('');
} }
/** /**
@ -884,7 +894,7 @@ Options:
'Would you like to test the connection to your UPS? (y/N): ' 'Would you like to test the connection to your UPS? (y/N): '
); );
if (testConnection.toLowerCase() === 'y') { if (testConnection.toLowerCase() === 'y') {
console.log('\nTesting connection to UPS...'); logger.log('\nTesting connection to UPS...');
try { try {
// Create a test config with a short timeout // Create a test config with a short timeout
const testConfig = { const testConfig = {
@ -893,17 +903,21 @@ Options:
}; };
const status = await this.nupst.getSnmp().getUpsStatus(testConfig); const status = await this.nupst.getSnmp().getUpsStatus(testConfig);
console.log('\n┌─ Connection Successful! ─────────────────┐'); const boxWidth = 45;
console.log('│ UPS Status:'); logger.log('');
console.log(`│ ✓ Power Status: ${status.powerStatus}`); logger.logBoxTitle('Connection Successful!', boxWidth);
console.log(`│ ✓ Battery Capacity: ${status.batteryCapacity}%`); logger.logBoxLine('UPS Status:');
console.log(`│ ✓ Runtime Remaining: ${status.batteryRuntime} minutes`); logger.logBoxLine(`✓ Power Status: ${status.powerStatus}`);
console.log('└──────────────────────────────────────────┘'); logger.logBoxLine(`✓ Battery Capacity: ${status.batteryCapacity}%`);
logger.logBoxLine(`✓ Runtime Remaining: ${status.batteryRuntime} minutes`);
logger.logBoxEnd();
} catch (error) { } catch (error) {
console.error('\n┌─ Connection Failed! ───────────────────────┐'); const errorBoxWidth = 45;
console.error('│ Error: ' + error.message); logger.log('');
console.error('└──────────────────────────────────────────┘'); logger.logBoxTitle('Connection Failed!', errorBoxWidth);
console.log('\nPlease check your settings and try again.'); logger.logBoxLine(`Error: ${error.message}`);
logger.logBoxEnd();
logger.log('\nPlease check your settings and try again.');
} }
} }
} }
@ -920,27 +934,28 @@ Options:
if (isActive) { if (isActive) {
// Service is running, restart it // Service is running, restart it
console.log('┌─ Service Update ──────────────────────────┐'); const boxWidth = 45;
console.log('│ Configuration has changed.'); logger.logBoxTitle('Service Update', boxWidth);
console.log('│ Restarting NUPST service to apply changes...'); logger.logBoxLine('Configuration has changed.');
logger.logBoxLine('Restarting NUPST service to apply changes...');
try { try {
if (process.getuid && process.getuid() === 0) { if (process.getuid && process.getuid() === 0) {
// We have root access, restart directly // We have root access, restart directly
execSync('systemctl restart nupst.service'); execSync('systemctl restart nupst.service');
console.log('│ Service restarted successfully.'); logger.logBoxLine('Service restarted successfully.');
} else { } else {
// No root access, show instructions // No root access, show instructions
console.log('│ Please restart the service with:'); logger.logBoxLine('Please restart the service with:');
console.log('│ sudo systemctl restart nupst.service'); logger.logBoxLine(' sudo systemctl restart nupst.service');
} }
} catch (error) { } catch (error) {
console.log(`Error restarting service: ${error.message}`); logger.logBoxLine(`Error restarting service: ${error.message}`);
console.log('│ You may need to restart the service manually:'); logger.logBoxLine('You may need to restart the service manually:');
console.log('│ sudo systemctl restart nupst.service'); logger.logBoxLine(' sudo systemctl restart nupst.service');
} }
console.log('└───────────────────────────────────────────┘'); logger.logBoxEnd();
} }
} catch (error) { } catch (error) {
// Ignore errors checking service status // Ignore errors checking service status
@ -991,69 +1006,71 @@ Options:
try { try {
await this.nupst.getDaemon().loadConfig(); await this.nupst.getDaemon().loadConfig();
} catch (error) { } catch (error) {
console.error('┌─ Configuration Error ─────────────────────┐'); const errorBoxWidth = 45;
console.error('│ No configuration found.'); logger.logBoxTitle('Configuration Error', errorBoxWidth);
console.error("│ Please run 'nupst setup' first to create a configuration."); logger.logBoxLine('No configuration found.');
console.error('└──────────────────────────────────────────┘'); logger.logBoxLine("Please run 'nupst setup' first to create a configuration.");
logger.logBoxEnd();
return; return;
} }
// Get current configuration // Get current configuration
const config = this.nupst.getDaemon().getConfig(); const config = this.nupst.getDaemon().getConfig();
console.log('┌─ NUPST Configuration ──────────────────────┐'); const boxWidth = 50;
logger.logBoxTitle('NUPST Configuration', boxWidth);
// SNMP Settings // SNMP Settings
console.log('│ SNMP Settings:'); logger.logBoxLine('SNMP Settings:');
console.log(` Host: ${config.snmp.host}`); logger.logBoxLine(` Host: ${config.snmp.host}`);
console.log(` Port: ${config.snmp.port}`); logger.logBoxLine(` Port: ${config.snmp.port}`);
console.log(` Version: ${config.snmp.version}`); logger.logBoxLine(` Version: ${config.snmp.version}`);
console.log(` UPS Model: ${config.snmp.upsModel || 'cyberpower'}`); logger.logBoxLine(` UPS Model: ${config.snmp.upsModel || 'cyberpower'}`);
if (config.snmp.version === 1 || config.snmp.version === 2) { if (config.snmp.version === 1 || config.snmp.version === 2) {
console.log(` Community: ${config.snmp.community}`); logger.logBoxLine(` Community: ${config.snmp.community}`);
} else if (config.snmp.version === 3) { } else if (config.snmp.version === 3) {
console.log(` Security Level: ${config.snmp.securityLevel}`); logger.logBoxLine(` Security Level: ${config.snmp.securityLevel}`);
console.log(` Username: ${config.snmp.username}`); logger.logBoxLine(` Username: ${config.snmp.username}`);
// Show auth and privacy details based on security level // Show auth and privacy details based on security level
if ( if (
config.snmp.securityLevel === 'authNoPriv' || config.snmp.securityLevel === 'authNoPriv' ||
config.snmp.securityLevel === 'authPriv' config.snmp.securityLevel === 'authPriv'
) { ) {
console.log(` Auth Protocol: ${config.snmp.authProtocol || 'None'}`); logger.logBoxLine(` Auth Protocol: ${config.snmp.authProtocol || 'None'}`);
} }
if (config.snmp.securityLevel === 'authPriv') { if (config.snmp.securityLevel === 'authPriv') {
console.log(` Privacy Protocol: ${config.snmp.privProtocol || 'None'}`); logger.logBoxLine(` Privacy Protocol: ${config.snmp.privProtocol || 'None'}`);
} }
// Show timeout value // Show timeout value
console.log(` Timeout: ${config.snmp.timeout / 1000} seconds`); logger.logBoxLine(` Timeout: ${config.snmp.timeout / 1000} seconds`);
} }
// Show OIDs if custom model is selected // Show OIDs if custom model is selected
if (config.snmp.upsModel === 'custom' && config.snmp.customOIDs) { if (config.snmp.upsModel === 'custom' && config.snmp.customOIDs) {
console.log('│ Custom OIDs:'); logger.logBoxLine('Custom OIDs:');
console.log(` Power Status: ${config.snmp.customOIDs.POWER_STATUS || 'Not set'}`); logger.logBoxLine(` Power Status: ${config.snmp.customOIDs.POWER_STATUS || 'Not set'}`);
console.log( logger.logBoxLine(
` Battery Capacity: ${config.snmp.customOIDs.BATTERY_CAPACITY || 'Not set'}` ` Battery Capacity: ${config.snmp.customOIDs.BATTERY_CAPACITY || 'Not set'}`
); );
console.log(` Battery Runtime: ${config.snmp.customOIDs.BATTERY_RUNTIME || 'Not set'}`); logger.logBoxLine(` Battery Runtime: ${config.snmp.customOIDs.BATTERY_RUNTIME || 'Not set'}`);
} }
// Thresholds // Thresholds
console.log('│ Thresholds:'); logger.logBoxLine('Thresholds:');
console.log(` Battery: ${config.thresholds.battery}%`); logger.logBoxLine(` Battery: ${config.thresholds.battery}%`);
console.log(` Runtime: ${config.thresholds.runtime} minutes`); logger.logBoxLine(` Runtime: ${config.thresholds.runtime} minutes`);
console.log(`Check Interval: ${config.checkInterval / 1000} seconds`); logger.logBoxLine(`Check Interval: ${config.checkInterval / 1000} seconds`);
// Configuration file location // Configuration file location
console.log('│'); logger.logBoxLine('');
console.log('│ Configuration File Location:'); logger.logBoxLine('Configuration File Location:');
console.log('│ /etc/nupst/config.json'); logger.logBoxLine(' /etc/nupst/config.json');
console.log('└──────────────────────────────────────────┘'); logger.logBoxEnd();
// Show service status // Show service status
try { try {
@ -1062,15 +1079,16 @@ Options:
const isEnabled = const isEnabled =
execSync('systemctl is-enabled nupst.service || true').toString().trim() === 'enabled'; execSync('systemctl is-enabled nupst.service || true').toString().trim() === 'enabled';
console.log('┌─ Service Status ─────────────────────────┐'); const statusBoxWidth = 45;
console.log(`│ Service Active: ${isActive ? 'Yes' : 'No'}`); logger.logBoxTitle('Service Status', statusBoxWidth);
console.log(`│ Service Enabled: ${isEnabled ? 'Yes' : 'No'}`); logger.logBoxLine(`Service Active: ${isActive ? 'Yes' : 'No'}`);
console.log('└──────────────────────────────────────────┘'); logger.logBoxLine(`Service Enabled: ${isEnabled ? 'Yes' : 'No'}`);
logger.logBoxEnd();
} catch (error) { } catch (error) {
// Ignore errors checking service status // Ignore errors checking service status
} }
} catch (error) { } catch (error) {
console.error(`Failed to display configuration: ${error.message}`); logger.error(`Failed to display configuration: ${error.message}`);
} }
} }