From 5d9981bfd081ca397e041878d4c671e83dcf68b3 Mon Sep 17 00:00:00 2001 From: Niall McCullagh Date: Tue, 11 Dec 2018 20:19:20 +0000 Subject: [PATCH] feat(cli): Add switches to override username and ip address fixes #1 --- README.md | 17 +++++++++++------ bin/aws-manage-sg.js | 26 ++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0131541..3efdf1b 100644 --- a/README.md +++ b/README.md @@ -45,16 +45,21 @@ It revokes old rules, and grants new rules with the user's current ip address. Find out the full range of options by running `aws-manage-sg -h` ```bash + $ aws-manage-sg -h Usage: aws-manage-sg [options] Options: - --version Show version number [boolean] - -f, --file Path to config file [required] - -g, --grant Run only the grant - -r, --revoke Run only the revoke - -p, --profile AWS profile to use - -h Show help [boolean] + --version Show version number [boolean] + -f, --file Path to config file [required] + -g, --grant Run only the grant [boolean] + -r, --revoke Run only the revoke [boolean] + -p, --profile AWS profile to use + -u, --username Username to tag rules with + --ip Use specified IP address. If not supplied the detected IP will + be used + -h Show help [boolean] + ``` ## Using in another application/library diff --git a/bin/aws-manage-sg.js b/bin/aws-manage-sg.js index 4931ea0..13d0654 100755 --- a/bin/aws-manage-sg.js +++ b/bin/aws-manage-sg.js @@ -15,11 +15,24 @@ async function getIPAddress() { return response.replace(/\s/g, ''); } -async function buildConfig(configFile) { - return { +function validate(config) { + if (!config.username) { + throw new Error('Username not set in config'); + } +} + +async function buildConfig(options, configFile) { + const config = { ...configFile, - ipAddress: await getIPAddress(), + ipAddress: options.ip || await getIPAddress(), }; + + if (options.username) { + config.username = options.username; + } + + validate(config); + return config; } async function run(options, config) { @@ -55,10 +68,15 @@ function getOptions() { .describe('f', 'Path to config file') .alias('g', 'grant') .describe('g', 'Run only the grant') + .boolean('g') .alias('r', 'revoke') .describe('r', 'Run only the revoke') + .boolean('r') .alias('p', 'profile') .describe('p', 'AWS profile to use') + .alias('u', 'username') + .describe('u', 'Username to tag rules with') + .describe('ip', 'Use specified IP address. If not supplied the detected IP will be used') .demandOption(['file'], 'Please provide a path to a config file') .help('h').argv; } @@ -71,7 +89,7 @@ function readConfigFile(path) { try { const options = getOptions(); const config = readConfigFile(options.file); - await run(options, await buildConfig(config)); + await run(options, await buildConfig(options, config)); } catch (e) { debug(`ERROR: ${e.message}`); process.exit(1);