fix(core): update
This commit is contained in:
@@ -1,103 +1,102 @@
|
||||
import 'typings-global'
|
||||
import * as plugins from './smartssh.plugins'
|
||||
import * as helpers from './smartssh.classes.helpers'
|
||||
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'
|
||||
import { SshDir } from './smartssh.classes.sshdir';
|
||||
import { SshConfig } from './smartssh.classes.sshconfig';
|
||||
import { SshKey } from './smartssh.classes.sshkey';
|
||||
|
||||
/**
|
||||
* SshInstance is the main class dealing with ssh management
|
||||
*/
|
||||
export class SshInstance {
|
||||
private _sshKeyArray: SshKey[] // holds all ssh keys
|
||||
private _sshConfig: SshConfig // sshConfig (e.g. represents ~/.ssh/config)
|
||||
private _sshDir: SshDir // points to sshDir class instance.
|
||||
private _sshSync: boolean // if set to true, the ssh dir will be kept in sync automatically
|
||||
constructor(optionsArg: { sshDirPath?: string, sshSync?: boolean } = {}) {
|
||||
optionsArg ? void (0) : optionsArg = {}
|
||||
this._sshKeyArray = []
|
||||
this._sshConfig = new SshConfig(this._sshKeyArray)
|
||||
this._sshDir = new SshDir(this._sshKeyArray, this._sshConfig, optionsArg.sshDirPath)
|
||||
this._sshSync = optionsArg.sshSync
|
||||
};
|
||||
private _sshKeyArray: SshKey[]; // holds all ssh keys
|
||||
private _sshConfig: SshConfig; // sshConfig (e.g. represents ~/.ssh/config)
|
||||
private _sshDir: SshDir; // points to sshDir class instance.
|
||||
private _sshSync: boolean; // if set to true, the ssh dir will be kept in sync automatically
|
||||
constructor(optionsArg: { sshDirPath?: string; sshSync?: boolean } = {}) {
|
||||
optionsArg ? void 0 : (optionsArg = {});
|
||||
this._sshKeyArray = [];
|
||||
this._sshConfig = new SshConfig(this._sshKeyArray);
|
||||
this._sshDir = new SshDir(this._sshKeyArray, this._sshConfig, optionsArg.sshDirPath);
|
||||
this._sshSync = optionsArg.sshSync;
|
||||
}
|
||||
|
||||
// altering methods
|
||||
addKey (sshKeyArg: SshKey) {
|
||||
this._syncAuto('from')
|
||||
this._sshKeyArray.push(sshKeyArg)
|
||||
this._syncAuto('to')
|
||||
};
|
||||
removeKey (sshKeyArg: SshKey) {
|
||||
this._syncAuto('from')
|
||||
addKey(sshKeyArg: SshKey) {
|
||||
this._syncAuto('from');
|
||||
this._sshKeyArray.push(sshKeyArg);
|
||||
this._syncAuto('to');
|
||||
}
|
||||
removeKey(sshKeyArg: SshKey) {
|
||||
this._syncAuto('from');
|
||||
let filteredArray = this._sshKeyArray.filter((sshKeyArg2: SshKey) => {
|
||||
return (sshKeyArg != sshKeyArg2)
|
||||
})
|
||||
this._sshKeyArray = filteredArray
|
||||
this._syncAuto('to')
|
||||
};
|
||||
replaceKey (sshKeyOldArg: SshKey, sshKeyNewArg: SshKey) {
|
||||
this._syncAuto('from')
|
||||
this.removeKey(sshKeyOldArg)
|
||||
this.addKey(sshKeyNewArg)
|
||||
this._syncAuto('to')
|
||||
};
|
||||
return sshKeyArg != sshKeyArg2;
|
||||
});
|
||||
this._sshKeyArray = filteredArray;
|
||||
this._syncAuto('to');
|
||||
}
|
||||
replaceKey(sshKeyOldArg: SshKey, sshKeyNewArg: SshKey) {
|
||||
this._syncAuto('from');
|
||||
this.removeKey(sshKeyOldArg);
|
||||
this.addKey(sshKeyNewArg);
|
||||
this._syncAuto('to');
|
||||
}
|
||||
|
||||
//
|
||||
getKey (hostArg: string): SshKey {
|
||||
this._syncAuto('from')
|
||||
let filteredArray = this._sshKeyArray.filter(function (keyArg) {
|
||||
return (keyArg.host === hostArg)
|
||||
})
|
||||
// non altering methods
|
||||
getKey(hostArg: string): SshKey {
|
||||
this._syncAuto('from');
|
||||
let filteredArray = this._sshKeyArray.filter(function(keyArg) {
|
||||
return keyArg.host === hostArg;
|
||||
});
|
||||
if (filteredArray.length > 0) {
|
||||
return filteredArray[ 0 ]
|
||||
return filteredArray[0];
|
||||
} else {
|
||||
return undefined
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
get sshKeys (): SshKey[] {
|
||||
this._syncAuto('from')
|
||||
return this._sshKeyArray
|
||||
};
|
||||
}
|
||||
get sshKeys(): SshKey[] {
|
||||
this._syncAuto('from');
|
||||
return this._sshKeyArray;
|
||||
}
|
||||
|
||||
// FS methods
|
||||
|
||||
/**
|
||||
* write SshInstance to disk
|
||||
*/
|
||||
writeToDisk (dirPathArg?: string) {
|
||||
this._sync('to', dirPathArg)
|
||||
writeToDisk(dirPathArg?: string) {
|
||||
this._sync('to', dirPathArg);
|
||||
}
|
||||
|
||||
/**
|
||||
* read ab SshInstance from disk
|
||||
*/
|
||||
readFromDisk (dirPathArg?: string) {
|
||||
this._sync('from', dirPathArg)
|
||||
readFromDisk(dirPathArg?: string) {
|
||||
this._sync('from', dirPathArg);
|
||||
}
|
||||
|
||||
/* ===============================================================
|
||||
========================= Private Methods ========================
|
||||
================================================================*/
|
||||
|
||||
private _makeConfig () {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* method to invoke SshInstance _sync automatically when sshSync is true
|
||||
*/
|
||||
private _syncAuto (directionArg) {
|
||||
if (this._sshSync) this._sync(directionArg)
|
||||
private _syncAuto(directionArg: 'from' | 'to') {
|
||||
if (this._sshSync) {
|
||||
// if auto sync is not enabled, do nothing
|
||||
this._sync(directionArg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* private method to sync SshInstance
|
||||
*/
|
||||
private _sync (directionArg: string, dirPathArg?: string) {
|
||||
private _sync(directionArg: 'from' | 'to', dirPathArg?: string) {
|
||||
if (directionArg === 'from') {
|
||||
this._sshDir.readFromDir(dirPathArg) // call sync method of sshDir class;
|
||||
this._sshDir.readFromDir(dirPathArg); // call sync method of sshDir class;
|
||||
} else if (directionArg === 'to') {
|
||||
this._sshDir.writeToDir(dirPathArg)
|
||||
} else {
|
||||
throw new Error("directionArg not recognised. Must be 'to' or 'from'")
|
||||
this._sshDir.writeToDir(dirPathArg);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user