From 6e06f500fe38c588532ff9484dba4cc1e5b32b8a Mon Sep 17 00:00:00 2001 From: Seth Wheeler Date: Wed, 8 Apr 2020 22:50:36 -0700 Subject: [PATCH 1/7] Make use of lowercase consistent --- API.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/API.md b/API.md index bc30eb8..138d0a7 100644 --- a/API.md +++ b/API.md @@ -1,5 +1,5 @@ ## Methods -### AddSshKeyAgent +### addSshKeyAgent ###### Params: `alias` - The name of the Alias @@ -15,7 +15,7 @@ console.log('Folder: ' + gam.addSshKeyAgent('alias', '/.ssh')); ``` No output -### Backup +### backup ###### Params: `dir` - Optional, the directory to use, defaults to the `.ssh` folder in the users home directory @@ -171,7 +171,7 @@ Would output: Alias: alias ``` -### GenerateKey +### generateKey ###### Params: `alias` - The name of the Alias From ed6903c476b2703cb0a8bf264118df7e2f4a10ba Mon Sep 17 00:00:00 2001 From: Seth Wheeler Date: Wed, 8 Apr 2020 22:55:21 -0700 Subject: [PATCH 2/7] Regex for email is now stored globaly in the file --- src/methods.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/methods.js b/src/methods.js index 9e4fd1d..4fe3575 100644 --- a/src/methods.js +++ b/src/methods.js @@ -9,6 +9,8 @@ const { exec, } = require('child_process'); +const emailRegex = /\S+@\S+\.\S+/g; + function getFormattedDate(date) { const year = date.getFullYear(); @@ -222,7 +224,7 @@ async function createAlias(alias, email, passphrase, bits = 4096, dir = path.joi }) .then(async (answer) => { email = answer.email; - while (!email.match(/\S+@\S+\.\S+/g)) { + while (!email.match(emailRegex)) { await inquirer.prompt({ type: 'input', name: 'email', From d4bb42307d4c3328853af44fec278d97aef5568a Mon Sep 17 00:00:00 2001 From: Seth Wheeler Date: Wed, 8 Apr 2020 22:56:05 -0700 Subject: [PATCH 3/7] Get alias email --- src/manager.js | 8 ++++++++ src/methods.js | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/manager.js b/src/manager.js index 6355dfe..9a8860e 100755 --- a/src/manager.js +++ b/src/manager.js @@ -95,9 +95,17 @@ if (args[0] === 'create-alias') { } else if (args[0] === 'v' || args[0] === 'version') { const version = process.env.npm_package_version === null ? process.env.npm_package_version : require('../package.json').version; console.log(`Current version: ${version}`); +} else if (args[0] === 'alias-email') { + methods.chooseAlias('', dir).then(async (_alias) => { + const res = methods.getAliasEmail(_alias, dir); + console.log(' ' + `Email for ${_alias}: ` + `${res.email}`); + }).catch((err) => { + throw (err); + }); } else if (args[0] === 'h' || args[0] === 'help' || args.length === 0) { console.log('Usage: gam [command] [options]' + '\n\n' + 'Available Commands:' + '\n' + + ' ' + 'alias-email: ' + ' ' + 'Retrives the email assosiated with the specified alias' + '\n' + ' ' + 'backup: ' + ' ' + 'Creates a backup of all of the public and private keys' + '\n' + ' ' + 'create-alias: ' + ' ' + 'Creates a public and private key for a new alias' + '\n' + ' ' + 'change-alias: ' + ' ' + 'Changes the current public and private key to the specified alias' + '\n' diff --git a/src/methods.js b/src/methods.js index 4fe3575..a2cd0eb 100644 --- a/src/methods.js +++ b/src/methods.js @@ -103,7 +103,20 @@ function generateKey(alias, email, passphrase, bits = 4096, dir = path.join(requ })); } -function getCurrentEmail(dir = path.join(require('os').homedir(), '/.ssh')) { +function getAliasEmail(alias, dir = path.join(require('os').homedir(), '/.ssh')) { + let userEmail; + const pathToPubKey = path.join(dir, `id_rsa_${alias}.pub`); + if (fs.readFileSync(pathToPubKey).toString().match(emailRegex)) { + userEmail = fs.readFileSync(pathToPubKey).toString().match(emailRegex)[0]; + } else { + userEmail = 'None found'; + } + return ({ + email: userEmail, + }); +} + +function getCurrentEmail(dir = path.join(require('os').homedir(), '/.ssh', 'id_rsa')) { let userEmail; if (fs.readFileSync(dir).toString().match(/\[user\](.*\n\t)(.*\n)/g)) { const user = fs.readFileSync(dir).toString() @@ -365,4 +378,5 @@ module.exports = { currentAliasEmail, deleteAlias, generateKey, + getAliasEmail, }; From a8ef42ed06d62ef65396731e256041f61d56c747 Mon Sep 17 00:00:00 2001 From: Seth Wheeler Date: Wed, 8 Apr 2020 22:56:31 -0700 Subject: [PATCH 4/7] Add documentation --- API.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/API.md b/API.md index 138d0a7..804b257 100644 --- a/API.md +++ b/API.md @@ -193,3 +193,25 @@ console.log(gam.generateKey('alias', 'email', 'passphrase', 2048, '/.ssh'); ``` No output + +### getAliasEmail +###### Params: +`alias` - The name of the Alias + +`dir` - Optional, the directory to search in, defaults to the `.ssh` folder in the users home directory + +###### Description: +Retrieves the email associated with an alias + +##### Example: +```javascript +const gam = require('git-alias-manager'); +console.log(gam.getAliasEmail('alias', '/.ssh'); +``` +Would output: + +```json +{ + "email": "email@domian.com" +} +``` From 49bf47a2b948237732fd8cbd203caa5942fc9c95 Mon Sep 17 00:00:00 2001 From: Seth Wheeler Date: Wed, 8 Apr 2020 22:56:57 -0700 Subject: [PATCH 5/7] verbiage change --- src/manager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manager.js b/src/manager.js index 9a8860e..52af75d 100755 --- a/src/manager.js +++ b/src/manager.js @@ -109,7 +109,7 @@ if (args[0] === 'create-alias') { + ' ' + 'backup: ' + ' ' + 'Creates a backup of all of the public and private keys' + '\n' + ' ' + 'create-alias: ' + ' ' + 'Creates a public and private key for a new alias' + '\n' + ' ' + 'change-alias: ' + ' ' + 'Changes the current public and private key to the specified alias' + '\n' - + ' ' + 'current-alias-email:' + ' ' + 'Retrives the email assosiated with the current alias' + '\n' + + ' ' + 'current-alias-email:' + ' ' + 'Retrives the local and global email assosiated with the current alias' + '\n' + ' ' + 'delete-alias: ' + ' ' + 'Deletes the public and private key for an alias' + '\n' + ' ' + 'h, help: ' + ' ' + 'Print available command line commands and options (currently set)' + '\n' + ' ' + 'v, version: ' + ' ' + 'Print the current version' + '\n\n' From 0d9d8290039a175b8dab658ca0c80d724616b191 Mon Sep 17 00:00:00 2001 From: Seth Wheeler Date: Wed, 8 Apr 2020 23:02:16 -0700 Subject: [PATCH 6/7] Minor bugfix --- src/methods.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/methods.js b/src/methods.js index a2cd0eb..c82d9c3 100644 --- a/src/methods.js +++ b/src/methods.js @@ -165,8 +165,8 @@ function changeLocalEmail(email) { function currentAliasEmail() { return ({ - localEmail: getCurrentEmail(path.join(require('os').homedir(), '.gitconfig')).email, - globalEmail: getCurrentEmail(path.resolve(`${process.cwd()}/.git/config`)).email, + localEmail: getCurrentEmail(path.resolve(`${process.cwd()}/.git/config`)).email, + globalEmail: getCurrentEmail(path.join(require('os').homedir(), '.gitconfig')).email, }); } From 66a46d39e30ce745b8ec22788013a2fee905e17b Mon Sep 17 00:00:00 2001 From: Seth Wheeler Date: Wed, 8 Apr 2020 23:05:00 -0700 Subject: [PATCH 7/7] Version Bump 1.2.4 --- package-lock.json | 4 ++-- package.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index c80841f..e5a52a3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "git-alias-manager", - "version": "1.2.3", + "version": "1.2.4", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1705,4 +1705,4 @@ } } } -} +} \ No newline at end of file diff --git a/package.json b/package.json index 7c72c4a..f5821a7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "git-alias-manager", - "version": "1.2.3", + "version": "1.2.4", "description": "GAM (Git Alias Manager) is a NodeJS application for managing multiple Git accounts (aliases).", "main": "index.js", "scripts": { @@ -42,4 +42,4 @@ "url": "https://github.com/Megapixel99/GAM/issues" }, "homepage": "https://github.com/Megapixel99/GAM#readme" -} +} \ No newline at end of file