fix(cli): Simplify UPS ID generation by removing the redundant promptForUniqueUpsId function in the CLI module and replacing it with the shortId helper.
This commit is contained in:
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@serve.zone/nupst',
|
||||
version: '3.0.0',
|
||||
version: '3.0.1',
|
||||
description: 'Node.js UPS Shutdown Tool for SNMP-enabled UPS devices'
|
||||
}
|
||||
|
33
ts/cli.ts
33
ts/cli.ts
@ -595,7 +595,7 @@ export class NupstCli {
|
||||
}
|
||||
|
||||
// Get UPS ID and name
|
||||
const upsId = await this.promptForUniqueUpsId(prompt, config.upsDevices);
|
||||
const upsId = helpers.shortId();
|
||||
const name = await prompt('UPS Name: ');
|
||||
|
||||
// Create a new UPS configuration object with defaults
|
||||
@ -1651,37 +1651,6 @@ Options:
|
||||
logger.error(`Failed to display configuration: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique UPS ID
|
||||
* @param prompt Function to prompt for user input
|
||||
* @param existingUpsDevices Array of existing UPS devices
|
||||
* @returns Unique UPS ID
|
||||
*/
|
||||
private async promptForUniqueUpsId(
|
||||
prompt: (question: string) => Promise<string>,
|
||||
existingUpsDevices: any[]
|
||||
): Promise<string> {
|
||||
const existingIds = existingUpsDevices.map(ups => ups.id);
|
||||
|
||||
// First ask for a custom ID
|
||||
const customId = await prompt('UPS ID (leave empty for auto-generated): ');
|
||||
|
||||
if (customId.trim()) {
|
||||
// Check if ID is already in use
|
||||
if (existingIds.includes(customId.trim())) {
|
||||
logger.error(`UPS ID "${customId.trim()}" is already in use.`);
|
||||
// Recursively call this function to try again
|
||||
return this.promptForUniqueUpsId(prompt, existingUpsDevices);
|
||||
}
|
||||
return customId.trim();
|
||||
}
|
||||
|
||||
// Generate a unique ID with timestamp
|
||||
const timestamp = new Date().getTime().toString(36);
|
||||
const randomPart = Math.floor(Math.random() * 1000).toString(36);
|
||||
return `ups-${timestamp}-${randomPart}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique group ID
|
||||
|
Reference in New Issue
Block a user