Skip to content

Commit

Permalink
Merge pull request #48 from Megapixel99/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Megapixel99 authored Apr 9, 2020
2 parents 428bbae + 66a46d3 commit 765ebfb
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
28 changes: 25 additions & 3 deletions API.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Methods
### AddSshKeyAgent
### addSshKeyAgent
###### Params:
`alias` - The name of the Alias

Expand All @@ -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

Expand Down Expand Up @@ -171,7 +171,7 @@ Would output:
Alias: alias
```

### GenerateKey
### generateKey
###### Params:
`alias` - The name of the Alias

Expand All @@ -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 protected]"
}
```
4 changes: 2 additions & 2 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -42,4 +42,4 @@
"url": "https://github.com/Megapixel99/GAM/issues"
},
"homepage": "https://github.com/Megapixel99/GAM#readme"
}
}
10 changes: 9 additions & 1 deletion src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,21 @@ 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'
+ ' ' + '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'
Expand Down
24 changes: 20 additions & 4 deletions src/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const {
exec,
} = require('child_process');

const emailRegex = /\S+@\S+\.\S+/g;

function getFormattedDate(date) {
const year = date.getFullYear();

Expand Down Expand Up @@ -101,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()
Expand Down Expand Up @@ -150,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,
});
}

Expand Down Expand Up @@ -222,7 +237,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',
Expand Down Expand Up @@ -363,4 +378,5 @@ module.exports = {
currentAliasEmail,
deleteAlias,
generateKey,
getAliasEmail,
};

0 comments on commit 765ebfb

Please sign in to comment.