2016-05-31 17:00:52 +00:00
|
|
|
import "typings-global"
|
|
|
|
import * as plugins from "./smartssh.plugins";
|
|
|
|
import * as helpers from "./smartssh.classes.helpers";
|
|
|
|
|
2016-05-31 17:16:45 +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
|
|
|
|
2016-05-31 22:56:24 +00:00
|
|
|
export class SshInstance {
|
2016-06-24 18:58:55 +00:00
|
|
|
private _sshKeyArray:SshKey[]; //holds all ssh keys
|
2016-06-24 00:49:55 +00:00
|
|
|
private _sshConfig:SshConfig; // sshConfig (e.g. represents ~/.ssh/config)
|
2016-06-24 18:58:55 +00:00
|
|
|
private _sshDir:SshDir; // points to sshDir class instance.
|
2016-06-24 00:49:55 +00:00
|
|
|
private _sshSync:boolean; // if set to true, the ssh dir will be kept in sync automatically
|
2016-06-01 01:57:17 +00:00
|
|
|
constructor(optionsArg:{sshDirPath?:string,sshSync?:boolean}={}){
|
2016-06-01 00:31:29 +00:00
|
|
|
optionsArg ? void(0) : optionsArg = {};
|
2016-06-24 18:58:55 +00:00
|
|
|
this._sshKeyArray = [];
|
|
|
|
this._sshConfig = new SshConfig(this._sshKeyArray);
|
2016-06-25 13:30:57 +00:00
|
|
|
this._sshDir = new SshDir(this._sshKeyArray,this._sshConfig,optionsArg.sshDirPath);
|
2016-06-24 00:49:55 +00:00
|
|
|
this._sshSync = optionsArg.sshSync;
|
2016-05-31 17:00:52 +00:00
|
|
|
};
|
2016-06-01 01:57:17 +00:00
|
|
|
|
|
|
|
//altering methods
|
2016-05-31 17:16:45 +00:00
|
|
|
addKey(sshKeyArg:SshKey){
|
2016-06-24 00:49:55 +00:00
|
|
|
this._syncAuto("from");
|
2016-06-24 18:58:55 +00:00
|
|
|
this._sshKeyArray.push(sshKeyArg);
|
2016-06-24 00:49:55 +00:00
|
|
|
this._syncAuto("to");
|
2016-05-31 17:00:52 +00:00
|
|
|
};
|
2016-06-01 01:57:17 +00:00
|
|
|
removeKey(sshKeyArg:SshKey){
|
2016-06-24 00:49:55 +00:00
|
|
|
this._syncAuto("from");
|
2016-06-24 18:58:55 +00:00
|
|
|
let filteredArray = this._sshKeyArray.filter((sshKeyArg2:SshKey) => {
|
2016-06-01 02:18:31 +00:00
|
|
|
return (sshKeyArg != sshKeyArg2);
|
|
|
|
});
|
2016-06-24 18:58:55 +00:00
|
|
|
this._sshKeyArray = filteredArray;
|
2016-06-24 00:49:55 +00:00
|
|
|
this._syncAuto("to");
|
2016-06-01 01:57:17 +00:00
|
|
|
};
|
|
|
|
replaceKey(sshKeyOldArg:SshKey,sshKeyNewArg:SshKey){
|
2016-06-24 00:49:55 +00:00
|
|
|
this._syncAuto("from");
|
2016-06-01 02:25:59 +00:00
|
|
|
this.removeKey(sshKeyOldArg);
|
|
|
|
this.addKey(sshKeyNewArg);
|
2016-06-24 00:49:55 +00:00
|
|
|
this._syncAuto("to");
|
2016-06-01 01:57:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2016-06-01 02:18:31 +00:00
|
|
|
getKey(hostArg:string):SshKey{
|
2016-06-24 00:49:55 +00:00
|
|
|
this._syncAuto("from");
|
2016-06-24 18:58:55 +00:00
|
|
|
let filteredArray = this._sshKeyArray.filter(function(keyArg){
|
2016-05-31 17:00:52 +00:00
|
|
|
return (keyArg.host == hostArg);
|
|
|
|
});
|
|
|
|
if(filteredArray.length > 0){
|
|
|
|
return filteredArray[0];
|
|
|
|
} else {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
};
|
2016-06-01 01:57:17 +00:00
|
|
|
get sshKeys():SshKey[] {
|
2016-06-24 00:49:55 +00:00
|
|
|
this._syncAuto("from");
|
2016-06-24 18:58:55 +00:00
|
|
|
return this._sshKeyArray;
|
2016-06-24 00:49:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//FS methods
|
|
|
|
|
|
|
|
/**
|
|
|
|
* write SshInstance to disk
|
|
|
|
*/
|
|
|
|
writeToDisk(){
|
|
|
|
this._sync("to");
|
2016-06-01 00:48:38 +00:00
|
|
|
}
|
2016-06-24 00:49:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* read ab SshInstance from disk
|
|
|
|
*/
|
|
|
|
readFromDisk(){
|
|
|
|
this._sync("from");
|
|
|
|
}
|
|
|
|
|
2016-06-24 18:58:55 +00:00
|
|
|
/* ===============================================================
|
|
|
|
========================= Private Methods ========================
|
|
|
|
================================================================*/
|
|
|
|
|
|
|
|
private _makeConfig (){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-24 00:49:55 +00:00
|
|
|
/**
|
|
|
|
* method to invoke SshInstance _sync automatically when sshSync is true
|
|
|
|
*/
|
|
|
|
private _syncAuto(directionArg){
|
|
|
|
if(this._sshSync) this._sync(directionArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* private method to sync SshInstance
|
|
|
|
*/
|
|
|
|
private _sync(directionArg:string){
|
|
|
|
if(directionArg == "from"){
|
2016-06-24 18:58:55 +00:00
|
|
|
this._sshDir.readFromDir(); // call sync method of sshDir class;
|
2016-06-24 00:49:55 +00:00
|
|
|
} else if(directionArg == "to") {
|
2016-06-24 18:58:55 +00:00
|
|
|
this._sshDir.writeToDir();
|
2016-06-24 00:49:55 +00:00
|
|
|
} else {
|
2016-06-01 01:57:17 +00:00
|
|
|
throw new Error("directionArg not recognised. Must be 'to' or 'from'");
|
2016-05-31 17:00:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|