ogit
Version:
A lazy developer's Git CLI made simple. Makes using git on cloud IDEs (i.e. C9) a walk in the park.
97 lines (96 loc) • 4.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const command_1 = require("@oclif/command");
const git_1 = require("../wrapper/git");
const inquirer = require("inquirer");
const path = require("path");
const fs = require("fs");
const { exec } = require('child_process');
class GenerateSSHKeyPairs extends command_1.Command {
constructor() {
super(...arguments);
this.DEFAULT_TYPE = 'rsa';
this.DEFAULT_BITS = 4096;
}
run() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
// console.log(process.env.SSH_AGENT_PID);
if (!process.env.SSH_AGENT_PID) {
console.log('Unable to find any ssh-agent running.');
console.log('Please start the agent (i.e. eval `ssh-agent -s`) as an admin and try again.');
if (this.isWindowsOs()) {
console.log('This command best works with GitBash.');
}
}
else {
yield this.runCommand();
}
});
}
runCommand() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const userName = yield git_1.GitFacade.getConfigData('user.email');
const answers = yield inquirer.prompt([
{
message: 'Please enter a name for the key',
type: 'input',
name: 'name',
validate: input => {
const keyPath = this.getFilePath(input, this.DEFAULT_TYPE, this.DEFAULT_BITS);
if (fs.existsSync(keyPath)) {
return 'File already exists!';
}
return !!input;
}
},
{
message: 'Please enter a pass phrase for private key (optional)',
type: 'password',
mask: '*',
name: 'passphrase'
},
{
message: 'Please re-enter the pass phrase',
type: 'password',
mask: '*',
name: 'confirmPassphrase',
when: (response) => response.passphrase
}
]);
if (answers.passphrase &&
answers.passphrase !== answers.confirmPassphrase) {
console.error('Pass phrases do not match. Please try again!');
}
else {
answers.comment = (yield inquirer.prompt({
message: 'Select a comment for ssh public key (optional)',
type: 'input',
name: 'comment',
default: userName
})).comment;
answers.name = escape(answers.name);
answers.type = this.DEFAULT_TYPE;
answers.bits = this.DEFAULT_BITS;
answers.keep = true;
answers.location = this.getFilePath(answers.name, answers.type, answers.bits);
const generatedSSHKeyPairs = yield git_1.GitFacade.generateSSHKeys(answers);
// console.log(generatedSSHKeyPairs);
yield exec(`ssh-add ${answers.location}`);
console.log(`Please copy the following text and use it as your public key. \n${generatedSSHKeyPairs.public}`);
}
});
}
getFilePath(name, type, bits) {
const homeDir = this.getHomeDirectory();
return path.resolve(`${homeDir}/.ssh/${escape(name)}_${type}_${bits}`);
}
isWindowsOs() {
return require('os').platform() === 'win32';
}
getHomeDirectory() {
return require('os').homedir();
}
}
GenerateSSHKeyPairs.description = 'Generates SSH key pairs to authenticate the user. For Windows OS, requires git bash to be pre-installed and run as administrator for this command';
exports.GenerateSSHKeyPairs = GenerateSSHKeyPairs;