2018-09-17 20:32:31 +00:00
|
|
|
import * as plugins from './smartssh.plugins';
|
|
|
|
import * as helpers from './smartssh.classes.helpers';
|
2016-05-31 17:00:52 +00:00
|
|
|
|
2018-09-17 20:32:31 +00:00
|
|
|
import { SshDir } from './smartssh.classes.sshdir';
|
|
|
|
import { SshConfig } from './smartssh.classes.sshconfig';
|
|
|
|
import { SshKey } from './smartssh.classes.sshkey';
|
2016-05-31 17:00:52 +00:00
|
|
|
|
2018-09-17 20:32:31 +00:00
|
|
|
/**
|
|
|
|
* SshInstance is the main class dealing with ssh management
|
|
|
|
*/
|
2016-05-31 22:56:24 +00:00
|
|
|
export class SshInstance {
|
2018-09-17 20:32:31 +00:00
|
|
|
private _sshKeyArray: SshKey[]; // holds all ssh keys
|
|
|
|
private _sshConfig: SshConfig; // sshConfig (e.g. represents ~/.ssh/config)
|
|
|
|
private _sshDir: SshDir; // points to sshDir class instance.
|
|
|
|
private _sshSync: boolean; // if set to true, the ssh dir will be kept in sync automatically
|
|
|
|
constructor(optionsArg: { sshDirPath?: string; sshSync?: boolean } = {}) {
|
|
|
|
optionsArg ? void 0 : (optionsArg = {});
|
|
|
|
this._sshKeyArray = [];
|
|
|
|
this._sshConfig = new SshConfig(this._sshKeyArray);
|
|
|
|
this._sshDir = new SshDir(this._sshKeyArray, this._sshConfig, optionsArg.sshDirPath);
|
|
|
|
this._sshSync = optionsArg.sshSync;
|
|
|
|
}
|
2016-11-23 11:38:38 +00:00
|
|
|
|
2017-06-15 17:25:12 +00:00
|
|
|
// altering methods
|
2018-09-17 20:32:31 +00:00
|
|
|
addKey(sshKeyArg: SshKey) {
|
|
|
|
this._syncAuto('from');
|
|
|
|
this._sshKeyArray.push(sshKeyArg);
|
|
|
|
this._syncAuto('to');
|
|
|
|
}
|
|
|
|
removeKey(sshKeyArg: SshKey) {
|
|
|
|
this._syncAuto('from');
|
2017-06-15 17:25:12 +00:00
|
|
|
let filteredArray = this._sshKeyArray.filter((sshKeyArg2: SshKey) => {
|
2018-09-17 20:32:31 +00:00
|
|
|
return sshKeyArg != sshKeyArg2;
|
|
|
|
});
|
|
|
|
this._sshKeyArray = filteredArray;
|
|
|
|
this._syncAuto('to');
|
|
|
|
}
|
|
|
|
replaceKey(sshKeyOldArg: SshKey, sshKeyNewArg: SshKey) {
|
|
|
|
this._syncAuto('from');
|
|
|
|
this.removeKey(sshKeyOldArg);
|
|
|
|
this.addKey(sshKeyNewArg);
|
|
|
|
this._syncAuto('to');
|
|
|
|
}
|
2016-11-23 11:38:38 +00:00
|
|
|
|
2018-09-17 20:32:31 +00:00
|
|
|
// non altering methods
|
|
|
|
getKey(hostArg: string): SshKey {
|
|
|
|
this._syncAuto('from');
|
|
|
|
let filteredArray = this._sshKeyArray.filter(function(keyArg) {
|
|
|
|
return keyArg.host === hostArg;
|
|
|
|
});
|
2017-06-15 17:25:12 +00:00
|
|
|
if (filteredArray.length > 0) {
|
2018-09-17 20:32:31 +00:00
|
|
|
return filteredArray[0];
|
2017-06-15 17:25:12 +00:00
|
|
|
} else {
|
2018-09-17 20:32:31 +00:00
|
|
|
return undefined;
|
2017-06-15 17:25:12 +00:00
|
|
|
}
|
2018-09-17 20:32:31 +00:00
|
|
|
}
|
|
|
|
get sshKeys(): SshKey[] {
|
|
|
|
this._syncAuto('from');
|
|
|
|
return this._sshKeyArray;
|
|
|
|
}
|
2016-06-24 00:49:55 +00:00
|
|
|
|
2017-06-15 17:25:12 +00:00
|
|
|
// FS methods
|
2016-06-24 00:49:55 +00:00
|
|
|
|
2017-06-15 17:25:12 +00:00
|
|
|
/**
|
|
|
|
* write SshInstance to disk
|
|
|
|
*/
|
2018-09-17 20:32:31 +00:00
|
|
|
writeToDisk(dirPathArg?: string) {
|
|
|
|
this._sync('to', dirPathArg);
|
2017-06-15 17:25:12 +00:00
|
|
|
}
|
2016-06-24 00:49:55 +00:00
|
|
|
|
2017-06-15 17:25:12 +00:00
|
|
|
/**
|
|
|
|
* read ab SshInstance from disk
|
|
|
|
*/
|
2018-09-17 20:32:31 +00:00
|
|
|
readFromDisk(dirPathArg?: string) {
|
|
|
|
this._sync('from', dirPathArg);
|
2017-06-15 17:25:12 +00:00
|
|
|
}
|
2016-06-24 00:49:55 +00:00
|
|
|
|
2017-06-15 17:25:12 +00:00
|
|
|
/* ===============================================================
|
|
|
|
========================= Private Methods ========================
|
|
|
|
================================================================*/
|
2016-06-24 18:58:55 +00:00
|
|
|
|
2017-06-15 17:25:12 +00:00
|
|
|
/**
|
|
|
|
* method to invoke SshInstance _sync automatically when sshSync is true
|
|
|
|
*/
|
2018-09-17 20:32:31 +00:00
|
|
|
private _syncAuto(directionArg: 'from' | 'to') {
|
|
|
|
if (this._sshSync) {
|
|
|
|
// if auto sync is not enabled, do nothing
|
|
|
|
this._sync(directionArg);
|
|
|
|
}
|
2017-06-15 17:25:12 +00:00
|
|
|
}
|
2016-06-24 00:49:55 +00:00
|
|
|
|
2017-06-15 17:25:12 +00:00
|
|
|
/**
|
|
|
|
* private method to sync SshInstance
|
|
|
|
*/
|
2018-09-17 20:32:31 +00:00
|
|
|
private _sync(directionArg: 'from' | 'to', dirPathArg?: string) {
|
2017-06-15 17:25:12 +00:00
|
|
|
if (directionArg === 'from') {
|
2018-09-17 20:32:31 +00:00
|
|
|
this._sshDir.readFromDir(dirPathArg); // call sync method of sshDir class;
|
2017-06-15 17:25:12 +00:00
|
|
|
} else if (directionArg === 'to') {
|
2018-09-17 20:32:31 +00:00
|
|
|
this._sshDir.writeToDir(dirPathArg);
|
2017-06-15 17:25:12 +00:00
|
|
|
}
|
2018-09-17 20:32:31 +00:00
|
|
|
}
|
2016-05-31 17:00:52 +00:00
|
|
|
}
|