diff --git a/README.md b/README.md index f2567f6..2b4a8bd 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ To update the package run `npm update -g @vafanassieff/email-alias` ## Usage +Start by using the command `email-alias configuration` to choose your default to email and your default provider + ```Shell Usage: email-alias [options] [command] diff --git a/bin/index.js b/bin/index.js index dff067e..813e66c 100755 --- a/bin/index.js +++ b/bin/index.js @@ -10,10 +10,11 @@ import { URL } from 'node:url' import { Command } from 'commander' import add from '#src/base/add.js' +import configuration from '#src/base/configuration.js' import list from '#src/base/list.js' +import printConfiguration from '#src/base/print-configuration.js' import remove from '#src/base/remove.js' import configure from '#src/provider/configure.js' -import set from '#src/provider/set.js' const program = new Command() @@ -33,12 +34,6 @@ provider .argument('', 'Provider to configure') .action(configure) -provider - .command('set') - .description('Set the used provider') - .argument('', 'Provider to use') - .action(set) - program.name('email-alias').description('Manage email alias').version(version) program.command('list').description('List current alias').action(list) @@ -46,10 +41,20 @@ program.command('list').description('List current alias').action(list) program .command('add') .description('Add a new alias') - .requiredOption('--from ', 'alias email') - .requiredOption('--to ', 'Destination email') + .argument('', 'alias email') + .option('--to ', 'Destination email') .action(add) +program + .command('configuration') + .description('Configure the application') + .action(configuration) + +program + .command('print-config') + .description('Print the app configuration') + .action(printConfiguration) + program .command('remove') .description('Add a new alias') diff --git a/package.json b/package.json index 0104ca2..f6e7ccc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@vafanassieff/email-alias", - "version": "0.1.3", + "version": "0.1.4", "description": "Create a email alias to keep you real email hidden !", "main": "src/index.js", "repository": { diff --git a/src/base/add.js b/src/base/add.js index 74eaefb..174b764 100644 --- a/src/base/add.js +++ b/src/base/add.js @@ -5,14 +5,14 @@ import { loadConfig, loadProvider } from '#src/utils.js' /** * Add a new redirecion to the current provider * + * @param {string} from From address * @param {object} params Object - * @param {string} params.from From address * @param {string} params.to To address */ -export default async ({ from, to }) => { +export default async (from, { to }) => { const appConfig = await loadConfig() const provider = await loadProvider(appConfig.provider) - await provider.add({ from, to }, appConfig[appConfig.provider]) + await provider.add({ from, to }, appConfig[appConfig.provider], appConfig) console.log(`New alias from ${from} to ${to} !`) } diff --git a/src/base/configuration.js b/src/base/configuration.js new file mode 100644 index 0000000..da1d19e --- /dev/null +++ b/src/base/configuration.js @@ -0,0 +1,41 @@ +import console from 'node:console' +import fs from 'node:fs/promises' + +import config from '#src/config.js' +import configure from '#src/provider/configure.js' +import { loadConfig, getProviders } from '#src/utils.js' + +/** + * Configure the application + */ +export default async () => { + const appConfig = await loadConfig() + + appConfig.defaultTo = await question( + 'On which email do you want a redirection by default ? ', + { + choices: [appConfig.defaultTo], + } + ) + + const providers = await getProviders() + + appConfig.provider = await question('Which provider will you use ? ', { + choices: providers, + }) + + await fs.writeFile(config.path, JSON.stringify(appConfig, undefined, 2)) + + console.log('Successfully configure email-alias ! Yai !') + + const shouldConfigProvider = await question( + `Do you want to configure the ${appConfig.provider} provider ? `, + { + choices: ['Yes', 'No'], + } + ) + + if (shouldConfigProvider === 'Yes') { + await configure(appConfig.provider) + } +} diff --git a/src/base/list.js b/src/base/list.js index dcb57dc..6160434 100644 --- a/src/base/list.js +++ b/src/base/list.js @@ -8,7 +8,7 @@ import { loadConfig, loadProvider } from '#src/utils.js' export default async () => { const appConfig = await loadConfig() const provider = await loadProvider(appConfig.provider) - const list = await provider.list(appConfig[appConfig.provider]) + const list = await provider.list(appConfig[appConfig.provider], appConfig) console.log(`Found ${list.length} alias :`) diff --git a/src/base/print-configuration.js b/src/base/print-configuration.js new file mode 100644 index 0000000..e2dc2a9 --- /dev/null +++ b/src/base/print-configuration.js @@ -0,0 +1,11 @@ +import console from 'node:console' + +import { loadConfig } from '#src/utils.js' + +/** + * Print the app configuration + */ +export default async () => { + const config = await loadConfig() + console.log(config) +} diff --git a/src/base/remove.js b/src/base/remove.js index 1261641..1d07846 100644 --- a/src/base/remove.js +++ b/src/base/remove.js @@ -11,7 +11,7 @@ export default async (alias) => { const appConfig = await loadConfig() const provider = await loadProvider(appConfig.provider) - await provider.remove(alias, appConfig[appConfig.provider]) + await provider.remove(alias, appConfig[appConfig.provider], appConfig) console.log(`Successfully removed ${alias} !`) } diff --git a/src/provider/set.js b/src/provider/set.js deleted file mode 100644 index eaf3eb4..0000000 --- a/src/provider/set.js +++ /dev/null @@ -1,18 +0,0 @@ -import console from 'node:console' -import fs from 'node:fs/promises' - -import config from '#src/config.js' -import { loadConfig, loadProvider } from '#src/utils.js' - -/** - * Choose the current provider - * - * @param {string} name Provider name - */ -export default async (name) => { - await loadProvider(name) - const appConfig = await loadConfig() - appConfig.provider = name - await fs.writeFile(config.path, JSON.stringify(appConfig, undefined, 2)) - console.log(`Provider set to ${name}`) -} diff --git a/src/providers/ovh.js b/src/providers/ovh.js index 99aec40..3b0abcd 100644 --- a/src/providers/ovh.js +++ b/src/providers/ovh.js @@ -71,10 +71,12 @@ const request = async (path, method, parameters, config) => { * @returns {OvhConfiguration} User OVH configuration */ export const configure = async () => { - const endpoint = await question('Which api endpoint should we use ? ') + const endpoint = await question('Which api endpoint should we use ? ', { + choices: Object.keys(endpoints), + }) if (!endpoint) { - throw new Error('You need a endpoint', Object.keys(endpoints)) + throw new Error('You need a endpoint') } const applicationKey = await question('What is your OVH application key ? ') @@ -152,7 +154,7 @@ export const list = async (config) => { * @param {OvhConfiguration} config OVH configuration * @returns {Array} List of alias */ -export const add = async ({ from, to }, config) => { +export const add = async ({ from, to }, config, { defaultTo }) => { if (!from.includes(config.domain)) { throw new Error(`From address should use the ${config.domain} domain`) } @@ -160,7 +162,7 @@ export const add = async ({ from, to }, config) => { await request( `/email/domain/${config.domain}/redirection`, 'POST', - { from, to, localCopy: false }, + { from, to: to || defaultTo, localCopy: false }, config ) } diff --git a/src/utils.js b/src/utils.js index 688a32d..16ca9aa 100644 --- a/src/utils.js +++ b/src/utils.js @@ -52,3 +52,14 @@ export const loadProvider = async (name) => { return import(providerPath) } + +/** + * Get all the supported providers + * + * @returns {Array} Array of providers name + */ +export const getProviders = async () => { + const providerFolder = path.join(__dirname, '../src/providers') + const files = await fs.readdir(providerFolder) + return files.map((p) => p.split('.js')[0]) +}