Skip to content

Commit

Permalink
feat(cli): Add switches to override username and ip address
Browse files Browse the repository at this point in the history
fixes #1
  • Loading branch information
niallmccullagh committed Dec 11, 2018
1 parent ed39c02 commit 5d9981b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 22 additions & 4 deletions bin/aws-manage-sg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down

0 comments on commit 5d9981b

Please sign in to comment.