Skip to content

Commit

Permalink
Merge pull request #1 from Gururaj26/update-config
Browse files Browse the repository at this point in the history
Now ssh generator also updates ssh config
  • Loading branch information
Gururaj26 authored Feb 1, 2019
2 parents 0fe791b + d72e0fd commit 0529686
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
4 changes: 3 additions & 1 deletion constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const seperator = '\n' + '---------------------------------------------------' + '\n'
const urlRegex = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
const thanksDecorator = '///////////////// Thanks for using ssh-generator ! /////////////////';

module.exports = {
seperator: seperator,
urlRegex: urlRegex
urlRegex: urlRegex,
thanksDecorator: thanksDecorator
}
28 changes: 26 additions & 2 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const chalkPipe = require('chalk-pipe')
const constants = require('./constants.js')
const seperator = constants.seperator
const urlRegex = constants.urlRegex
var exec = require('child_process').exec
const thanksDecorator = constants.thanksDecorator
const exec = require('child_process').exec
const fs = require('fs');
var os = require('os');
const path = require('path')

module.exports = {
validateUrl: function(name) {
Expand All @@ -15,6 +19,25 @@ module.exports = {
const isValid = (name !== '');
return isValid || '~ Username is required';
},
updateConfig: function(username, domain){
const currentUser = os.userInfo().username;
const configPath = `${ path.parse(process.cwd()).root + 'Users/' + currentUser + '/.ssh/config'}`;
const host = `${'Host '+ username + '-' + domain}`;
const hostName = `${'HostName '+ domain}`
const identityFile = `${'IdentityFile ~/.ssh/' + username + '-' + domain}`;
const config = `${'\r\n' + host + '\r\n' + ' ' + hostName + '\r\n' + ' ' + identityFile}`;
fs.open(configPath, "a+", function(error, fd) {
if (error) {
console.error("open error: " + error.message);
} else {
fs.appendFile(fd, config, function (err) {
if (err) throw err;
console.log(seperator + 'Saved SSH to config file !' + seperator + thanksDecorator);
process.exit(1);
});
}
});
},
generateKeys: function(username, domain, type) {
let exp = 'ssh-keygen -t rsa -b 4096 -f ~/.ssh/' + username + '-' + domain + ' ' + '-C' + ' ' + '"' + username + '-' + domain + '"';
let printExp = `${'cat ~/.ssh/' + username + '-' + domain + '.pub'}`;
Expand All @@ -26,7 +49,8 @@ module.exports = {
console.log('stdout: ' + data);
let subchild = exec(printExp);
subchild.stdout.on('data', function(data) {
console.log(seperator + 'Freshly baked key: Ready to be served !' + seperator, chalkPipe('orange')(data));
console.log(seperator + 'Freshly baked key: Ready to be served !' + seperator + chalkPipe('orange')(data));
module.exports.updateConfig(username, domain);
});
subchild.stderr.on('data', function(data) {
console.log('stdout: ' + data);
Expand Down

0 comments on commit 0529686

Please sign in to comment.