update directory sync
This commit is contained in:
@ -6,7 +6,8 @@ import {SshKey} from "./smartssh.classes.sshkey";
|
||||
import {SshConfig} from "./smartssh.classes.sshconfig";
|
||||
export class SshDir { // sshDir class -> NOT EXPORTED, ONLY FOR INTERNAL USE
|
||||
path:string; // the path of the ssh directory
|
||||
constructor(sshDirPathArg:string){
|
||||
sshInstance:SshInstance;
|
||||
constructor(sshInstanceArg:SshInstance,sshDirPathArg?:string){
|
||||
let sshDirPath:string;
|
||||
if(sshDirPathArg){
|
||||
sshDirPath = sshDirPathArg;
|
||||
@ -15,7 +16,7 @@ export class SshDir { // sshDir class -> NOT EXPORTED, ONLY FOR INTERNAL USE
|
||||
}
|
||||
this.path = sshDirPath;
|
||||
}
|
||||
syncToDir(sshInstanceArg:SshInstance){ //syncs
|
||||
syncToDir(){ //syncs
|
||||
|
||||
};
|
||||
syncFromDir(){
|
||||
|
@ -9,20 +9,39 @@ import {SshKey} from "./smartssh.classes.sshkey";
|
||||
export class SshInstance {
|
||||
private sshConfig:SshConfig; // sshConfig (e.g. represents ~/.ssh/config)
|
||||
private sshDir:SshDir; // points to sshDir class instance.
|
||||
private sshKeys:SshKey[]; //holds all ssh keys
|
||||
private sshKeyArray:SshKey[]; //holds all ssh keys
|
||||
private sshSync:boolean; // if set to true, the ssh dir will be kept in sync automatically
|
||||
constructor(optionsArg:{sshDir?:string,sshSync?:boolean}={}){
|
||||
constructor(optionsArg:{sshDirPath?:string,sshSync?:boolean}={}){
|
||||
optionsArg ? void(0) : optionsArg = {};
|
||||
this.sshDir = new SshDir(optionsArg.sshDir);
|
||||
this.sshKeys = this.sshDir.getKeys();
|
||||
|
||||
this.sshDir = new SshDir(this,optionsArg.sshDirPath);
|
||||
this.sshKeyArray = this.sshDir.getKeys();
|
||||
this.sshSync = optionsArg.sshSync;
|
||||
};
|
||||
|
||||
//altering methods
|
||||
addKey(sshKeyArg:SshKey){
|
||||
this.sshKeys.push(sshKeyArg);
|
||||
this.sync();
|
||||
this.sync("from");
|
||||
this.sshKeyArray.push(sshKeyArg);
|
||||
this.sync("to");
|
||||
};
|
||||
removeKey(sshKeyArg:SshKey){
|
||||
this.sync("from");
|
||||
let keyIndex = helpers.getKeyIndex(sshKeyArg.host);
|
||||
this.sshKeyArray.splice(keyIndex,1);
|
||||
this.sync("to");
|
||||
};
|
||||
replaceKey(sshKeyOldArg:SshKey,sshKeyNewArg:SshKey){
|
||||
this.sync("from");
|
||||
let keyIndex = helpers.getKeyIndex(sshKeyOldArg.host);
|
||||
this.sshKeyArray.splice(keyIndex,1,sshKeyNewArg);
|
||||
this.sync("to");
|
||||
};
|
||||
|
||||
//
|
||||
getKey(hostArg:string){
|
||||
let filteredArray = this.sshKeys.filter(function(keyArg){
|
||||
this.sync("from");
|
||||
let filteredArray = this.sshKeyArray.filter(function(keyArg){
|
||||
return (keyArg.host == hostArg);
|
||||
});
|
||||
if(filteredArray.length > 0){
|
||||
@ -31,24 +50,16 @@ export class SshInstance {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
getKeys(){
|
||||
return this.sshKeys;
|
||||
get sshKeys():SshKey[] {
|
||||
return this.sshKeyArray;
|
||||
}
|
||||
|
||||
removeKey(sshKeyArg:SshKey){
|
||||
let keyIndex = helpers.getKeyIndex(sshKeyArg.host);
|
||||
this.sshKeys.splice(keyIndex,1);
|
||||
this.sync();
|
||||
};
|
||||
replaceKey(sshKeyOldArg:SshKey,sshKeyNewArg:SshKey){
|
||||
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;
|
||||
sync(directionArg:string){
|
||||
if(this.sshSync && directionArg == "from"){
|
||||
this.sshDir.syncFromDir(); // call sync method of sshDir class;
|
||||
} else if (this.sshSync && directionArg == "to") {
|
||||
this.sshDir.syncToDir();
|
||||
} else {
|
||||
throw new Error("directionArg not recognised. Must be 'to' or 'from'");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user