-
Notifications
You must be signed in to change notification settings - Fork 7
Kdm config setup #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Kdm config setup #106
Changes from all commits
153dd26
7374eae
bcef684
854c8ab
c6aa6b3
e8afbf4
4db6ebc
b3441f7
6471fab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,31 @@ import chalk from 'chalk'; | |
| import { setConfig, getConfig, clearConfig, clearNotificationCredentials } from '../utils/config'; | ||
| import { select, input } from '@vr_patel/tui'; | ||
|
|
||
| const promptReconfigurationIfNeeded = async (): Promise<boolean> => { | ||
| const currentConfig = getConfig(); | ||
| if (currentConfig.notification_service && currentConfig.notification_service !== 'none') { | ||
| const serviceLabel = | ||
| currentConfig.notification_service === 'discord' ? 'Discord' : 'Email (SMTP)'; | ||
| console.log( | ||
| chalk.yellow(`\nβ Current notification service is set to: ${chalk.bold(serviceLabel)}`) | ||
| ); | ||
|
|
||
| const shouldReconfigure = await select({ | ||
| message: 'Would you like to reconfigure?', | ||
| options: [ | ||
| { label: 'Yes', value: 'yes' }, | ||
| { label: 'No', value: 'no' }, | ||
| ], | ||
| }); | ||
|
|
||
| if (shouldReconfigure === 'no') { | ||
| console.log(chalk.dim('Setup cancelled. Current configuration unchanged.')); | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| }; | ||
|
|
||
| export const registerConfigCommand = (program: Command) => { | ||
| const config = program | ||
| .command('config') | ||
|
|
@@ -13,6 +38,10 @@ export const registerConfigCommand = (program: Command) => { | |
| .description('Interactively set up notification service') | ||
| .action(async () => { | ||
| try { | ||
| // Check for existing configuration | ||
|
Yuvraj-Sarathe marked this conversation as resolved.
|
||
| const shouldProceed = await promptReconfigurationIfNeeded(); | ||
| if (!shouldProceed) return; | ||
|
|
||
|
Comment on lines
+41
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. β Getting worse: Complex Method
Comment on lines
+41
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. β New issue: Bumpy Road Ahead |
||
| const choice = await select({ | ||
| message: 'Select notification service:', | ||
| options: [ | ||
|
|
@@ -39,7 +68,7 @@ export const registerConfigCommand = (program: Command) => { | |
| return discordWebhookRegex.test(v) || 'Must be a valid Discord webhook URL (including ID and Token)'; | ||
| }, | ||
| }); | ||
|
|
||
| clearNotificationCredentials(); | ||
| setConfig('discord_webhook', webhook); | ||
| setConfig('notification_service', 'discord'); | ||
|
|
@@ -82,13 +111,13 @@ export const registerConfigCommand = (program: Command) => { | |
| setConfig('email_password', password); | ||
| } | ||
| setConfig('notification_service', 'email'); | ||
|
|
||
| console.log(chalk.green('\nβ Email SMTP configured.')); | ||
| } | ||
|
|
||
| console.log(chalk.green(`β Notification service set to: ${chalk.bold(choice.toUpperCase())}`)); | ||
| console.log(chalk.green(`\nβ Notification service set to: ${chalk.bold(choice.toUpperCase())}`)); | ||
| } catch (error) { | ||
| console.error(chalk.red(`β Set up cancelled or failed: ${(error as Error).message}`)); | ||
| console.error(chalk.red(`\nβ Set up cancelled or failed: ${(error as Error).message}`)); | ||
| } | ||
|
Yuvraj-Sarathe marked this conversation as resolved.
|
||
| }); | ||
|
|
||
|
|
@@ -97,12 +126,26 @@ export const registerConfigCommand = (program: Command) => { | |
| .description('Set a configuration value') | ||
| .action((key, value) => { | ||
| try { | ||
| // Credential keys that should be configured via the interactive setup | ||
| const credentialKeys = ['notification_service', 'discord_webhook', 'email_host', 'email_port', 'email_user', 'email_to']; | ||
| const credentialKeyPattern = new RegExp(`^(${credentialKeys.join('|')})$`); | ||
|
|
||
| if (credentialKeyPattern.test(key)) { | ||
| console.log(chalk.yellow(`\nβ Deprecation warning: Setting "${key}" via "kdm config set" is deprecated.`)); | ||
| console.log(chalk.yellow(` Use ${chalk.bold('kdm config setup')} for guided configuration.\n`)); | ||
| } | ||
|
|
||
| // Convert value to number if key is alert_cooldown or email_port | ||
| let finalValue = value; | ||
| let finalValue = value; | ||
| if (key === 'alert_cooldown' || key === 'email_port') { | ||
| finalValue = parseInt(value, 10); | ||
| const parsed = Number.parseInt(value, 10); | ||
| if (Number.isNaN(parsed)) { | ||
| throw new Error(`Invalid numeric value for "${key}"`); | ||
| } | ||
| finalValue = parsed; | ||
| } | ||
|
Yuvraj-Sarathe marked this conversation as resolved.
|
||
|
|
||
| setConfig(key as any, finalValue); | ||
| console.log(chalk.green(`β Set ${key} to ${finalValue}`)); | ||
| } catch (error) { | ||
|
|
@@ -117,15 +160,15 @@ export const registerConfigCommand = (program: Command) => { | |
| const current = getConfig(); | ||
| console.log(chalk.bold('\nCurrent KDM Configuration:')); | ||
| console.log(chalk.gray('ββββββββββββββββββββββββββββββββββββββββββββββββββ')); | ||
|
|
||
| if (Object.keys(current).length === 0) { | ||
| console.log(chalk.yellow(' No configuration found. Use "kdm config set <key> <value>"')); | ||
| } else { | ||
| Object.entries(current).forEach(([key, value]) => { | ||
| console.log(` ${chalk.cyan(key.padEnd(20))} : ${chalk.white(value)}`); | ||
| console.log(`${chalk.cyan(key.padEnd(20))} : ${chalk.white(value)}`); | ||
| }); | ||
| } | ||
|
|
||
| console.log(chalk.gray('ββββββββββββββββββββββββββββββββββββββββββββββββββ')); | ||
| console.log(chalk.dim('\n Note: SMTP password can be set either in config or via the KDM_SMTP_PASSWORD environment variable, which takes precedence if both are set.\n')); | ||
| }); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.