From 2804c16f76a83ce898a852f300d594317af69936 Mon Sep 17 00:00:00 2001 From: Emily Barbour Date: Mon, 31 Aug 2020 18:57:36 -0400 Subject: [PATCH] v1.2.1 --- README.md | 36 +++++++++++++++++------------------ index.js | 48 +++++++++++++++++++++++------------------------ package-lock.json | 2 +- package.json | 5 +---- 4 files changed, 44 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index caf803e..36c24a3 100644 --- a/README.md +++ b/README.md @@ -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 @@ -11,26 +10,26 @@ 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; } ``` @@ -38,6 +37,7 @@ aisp() { - AWS CLI - Node ^8.17 + - Prompts ^2.3.0 ### License MIT diff --git a/index.js b/index.js index 67861b1..7ec0026 100755 --- a/index.js +++ b/index.js @@ -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 @@ -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', @@ -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); diff --git a/package-lock.json b/package-lock.json index 21b4a0e..7bfe49f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "aws-interactive-switch-profile", - "version": "1.2.0", + "version": "1.2.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index b53cb88..06dd8ee 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" }