smartssh/ts/smartssh.classes.sshinstance.ts

70 lines
2.3 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 {
private sshConfig:SshConfig; // sshConfig (e.g. represents ~/.ssh/config)
private sshDir:SshDir; // points to sshDir class instance.
2016-06-13 22:27:55 +00:00
protected sshKeyArray:SshKey[]; //holds all ssh keys
2016-05-31 17:00:52 +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-01 01:57:17 +00:00
this.sshDir = new SshDir(this,optionsArg.sshDirPath);
2016-06-13 22:27:55 +00:00
this.sshKeyArray = [];
2016-05-31 17:00:52 +00:00
this.sshSync = optionsArg.sshSync;
};
2016-06-01 01:57:17 +00:00
//altering methods
addKey(sshKeyArg:SshKey){
2016-06-01 01:57:17 +00:00
this.sync("from");
this.sshKeyArray.push(sshKeyArg);
this.sync("to");
2016-05-31 17:00:52 +00:00
};
2016-06-01 01:57:17 +00:00
removeKey(sshKeyArg:SshKey){
this.sync("from");
2016-06-01 02:18:31 +00:00
let filteredArray = this.sshKeyArray.filter((sshKeyArg2:SshKey) => {
return (sshKeyArg != sshKeyArg2);
});
this.sshKeyArray = filteredArray;
2016-06-01 01:57:17 +00:00
this.sync("to");
};
replaceKey(sshKeyOldArg:SshKey,sshKeyNewArg:SshKey){
this.sync("from");
2016-06-01 02:25:59 +00:00
this.removeKey(sshKeyOldArg);
this.addKey(sshKeyNewArg);
2016-06-01 01:57:17 +00:00
this.sync("to");
};
//
2016-06-01 02:18:31 +00:00
getKey(hostArg:string):SshKey{
2016-06-01 01:57:17 +00:00
this.sync("from");
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-01 02:25:59 +00:00
this.sync("from");
2016-06-01 01:57:17 +00:00
return this.sshKeyArray;
2016-06-01 00:48:38 +00:00
}
2016-06-01 01:57:17 +00:00
sync(directionArg:string){
if(this.sshSync && directionArg == "from"){
this.sshDir.syncFromDir(); // call sync method of sshDir class;
2016-06-01 02:18:31 +00:00
} else if(this.sshSync && directionArg == "to") {
2016-06-01 01:57:17 +00:00
this.sshDir.syncToDir();
2016-06-01 02:18:31 +00:00
} else if(this.sshSync) {
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
}
};
}