Skip to content

Commit

Permalink
v1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Emily Barbour committed Aug 31, 2020
1 parent 3631ac0 commit 2804c16
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 47 deletions.
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
## aws-interactive-switch-profile

Utilizes `aws configure list-profiles` via aws-cli to allow the switching of profiles via [prompts](https://github.com/terkelg/prompts). No more remembering what your AWS profiles are called. Run the program via a helper shell function, select the profile you want to use and that's it.

Utilizes `aws configure list-profiles` via [aws-cli](https://aws.amazon.com/cli/) to allow the switching of profiles using [Prompts](https://github.com/terkelg/prompts). No more remembering what your AWS profiles are called and typing export statements in terminal. Run the program via a helper shell function, select the profile you want to use and that's it.

### Install

Expand All @@ -11,33 +10,34 @@ Utilizes `aws configure list-profiles` via aws-cli to allow the switching of pro

`aws-switch-profiles '/tmp/foo.txt'`

Because you cannot persist global environment variables outside of Node, you must use some sort of helper function to facilitate this.
Because you cannot persist global environment variables outside of Node and Prompts uses stdout, you must pass a writable temp file and also use some sort of helper function to facilitate the process.

> example zsh script
```
aisp() {
unset _PROFILE;
# creates tmp file
tf=$(mktemp /tmp/aisp.XXXXXXXXX)
# runs aws-switch-profiles and stores user selection to tmp file
node aws-switch-profiles $tf
# reads in tmp file and stores to variable
_PROFILE=$(<$tf);
if [ -z $_PROFILE ];
then
echo "AWS_PROFILE not selected.";
else
export AWS_PROFILE=$_PROFILE
fi
rm $tf;
unset _PROFILE;
# creates tmp file
tf=$(mktemp /tmp/aisp.XXXXXXXXX)
# runs aws-switch-profiles and stores user selection to tmp file
aws-switch-profiles $tf
# reads in tmp file and stores to variable
_PROFILE=$(<$tf);
if [ -z $_PROFILE ];
then
echo "AWS_PROFILE not selected.";
else
export AWS_PROFILE=$_PROFILE
fi
rm $tf;
}
```

### Requirements

- AWS CLI
- Node ^8.17
- Prompts ^2.3.0

### License
MIT
Expand Down
48 changes: 24 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,33 @@ const util = require('util');
const exec = util.promisify(require('child_process').exec);


async function requirementsCheck() {
try {
const fileName = process.argv[2];
if (!fs.existsSync(fileName)) {
console.error('The tmp file does not exist.');
}
} catch (err) {
console.error(err);
function onError(e) {
if (e.stderr) {
process.stderr.write(e.stderr)
} else {
console.error(e)
}
process.exit(1);
}

async function checkAWS() {
await exec('aws --version', (error, _, stderr) => {
if (error) {
if(error) {
console.error('Please install AWS CLI.');
process.exit(1);
} else if (stderr) {
console.error('stderr');
throw(stderr);
}
});
}

async function checkFile() {
const fileName = process.argv[2];
if (!fs.existsSync(fileName)) {
throw('The tmp file does not exist.');
}
}

async function getProfiles() {
const { stdout: profiles } = await exec('aws configure list-profiles');
return profiles
Expand All @@ -36,16 +44,17 @@ async function getProfiles() {
}

async function writeFile(profile) {
tmpFile = process.argv[2];
try {
tmpFile = process.argv[2];
fs.writeFileSync(tmpFile, profile);
} catch(e) {
console.error(e);
} catch (error) {
throw (error);
}
}

async function run() {
await requirementsCheck();
checkFile().catch(onError);
checkAWS().catch(onError);
const choices = await getProfiles();
const { profile } = await prompts({
type: 'select',
Expand All @@ -54,16 +63,7 @@ async function run() {
choices,
});

await writeFile(profile);
}

function onError(e) {
if (e.stderr) {
process.stderr.write(e.stderr)
} else {
console.error(e)
}
process.exit(1);
await writeFile(profile).catch(onError);
}

run().catch(onError);
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aws-interactive-switch-profile",
"version": "1.2.0",
"version": "1.2.1",
"description": "Switch between AWS profiles",
"author": "Emily Barbour",
"license": "MIT",
Expand All @@ -13,8 +13,5 @@
"dependencies": {
"prompts": "^2.3.0"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
"repository": "git://github.com/notacoat/aws-interactive-switch-profile.git"
}

0 comments on commit 2804c16

Please sign in to comment.