smartssh/ts/smartssh.classes.sshinstance.ts

97 lines
2.7 KiB
TypeScript
Raw Normal View History

2016-05-31 17:00:52 +00:00
import "typings-global"
import * as plugins from "./smartssh.plugins";
import * as helpers from "./smartssh.classes.helpers";
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 00:49:55 +00:00
private _sshConfig:SshConfig; // sshConfig (e.g. represents ~/.ssh/config)
sshDir:SshDir; // points to sshDir class instance.
2016-06-13 22:27:55 +00:00
protected sshKeyArray:SshKey[]; //holds all ssh keys
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-13 22:27:55 +00:00
this.sshKeyArray = [];
2016-06-24 00:49:55 +00:00
this._sshSync = optionsArg.sshSync;
this.sshDir = new SshDir(this,optionsArg.sshDirPath);
2016-05-31 17:00:52 +00:00
};
2016-06-01 01:57:17 +00:00
//altering methods
addKey(sshKeyArg:SshKey){
2016-06-24 00:49:55 +00:00
this._syncAuto("from");
2016-06-01 01:57:17 +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-01 02:18:31 +00:00
let filteredArray = this.sshKeyArray.filter((sshKeyArg2:SshKey) => {
return (sshKeyArg != sshKeyArg2);
});
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-01 01:57:17 +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-01 01:57:17 +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");
}
/**
* 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"){
this.sshDir.readFromDir(); // call sync method of sshDir class;
} else if(directionArg == "to") {
this.sshDir.writeToDir();
} 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
}
};
}