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 {
|
|
|
|
private sshConfig:SshConfig; // sshConfig (e.g. represents ~/.ssh/config)
|
2016-05-31 17:16:45 +00:00
|
|
|
private sshDir:SshDir; // points to sshDir class instance.
|
|
|
|
private sshKeys: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
|
|
|
|
constructor(optionsArg:{sshDir?:string,sshSync?:boolean}={}){
|
2016-06-01 00:31:29 +00:00
|
|
|
optionsArg ? void(0) : optionsArg = {};
|
2016-05-31 17:16:45 +00:00
|
|
|
this.sshDir = new SshDir(optionsArg.sshDir);
|
2016-05-31 17:00:52 +00:00
|
|
|
this.sshKeys = this.sshDir.getKeys();
|
|
|
|
this.sshSync = optionsArg.sshSync;
|
|
|
|
};
|
2016-05-31 17:16:45 +00:00
|
|
|
addKey(sshKeyArg:SshKey){
|
2016-05-31 17:00:52 +00:00
|
|
|
this.sshKeys.push(sshKeyArg);
|
|
|
|
this.sync();
|
|
|
|
};
|
|
|
|
getKey(hostArg:string){
|
|
|
|
let filteredArray = this.sshKeys.filter(function(keyArg){
|
|
|
|
return (keyArg.host == hostArg);
|
|
|
|
});
|
|
|
|
if(filteredArray.length > 0){
|
|
|
|
return filteredArray[0];
|
|
|
|
} else {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
};
|
2016-05-31 17:16:45 +00:00
|
|
|
removeKey(sshKeyArg:SshKey){
|
2016-05-31 17:00:52 +00:00
|
|
|
let keyIndex = helpers.getKeyIndex(sshKeyArg.host);
|
|
|
|
this.sshKeys.splice(keyIndex,1);
|
|
|
|
this.sync();
|
|
|
|
};
|
2016-05-31 17:16:45 +00:00
|
|
|
replaceKey(sshKeyOldArg:SshKey,sshKeyNewArg:SshKey){
|
2016-05-31 17:00:52 +00:00
|
|
|
let keyIndex = helpers.getKeyIndex(sshKeyOldArg.host);
|
|
|
|
this.sshKeys.splice(keyIndex,1,sshKeyNewArg);
|
|
|
|
this.sync();
|
|
|
|
};
|
|
|
|
sync(){
|
|
|
|
if(this.sshSync){
|
|
|
|
this.sshDir.sync(this.sshConfig,this.sshKeys); // call sync method of sshDir class;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|