2016-11-23 11:38:38 +00:00
|
|
|
import 'typings-global'
|
|
|
|
import * as plugins from './smartssh.plugins'
|
|
|
|
import * as helpers from './smartssh.classes.helpers'
|
2016-05-31 17:00:52 +00:00
|
|
|
|
2016-11-23 11:38:38 +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 {
|
2016-11-23 11:38:38 +00:00
|
|
|
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
|
2016-05-31 17:00:52 +00:00
|
|
|
};
|
2016-11-23 11:38:38 +00:00
|
|
|
|
|
|
|
// altering methods
|
|
|
|
addKey(sshKeyArg: SshKey) {
|
|
|
|
this._syncAuto('from')
|
|
|
|
this._sshKeyArray.push(sshKeyArg)
|
|
|
|
this._syncAuto('to')
|
2016-05-31 17:00:52 +00:00
|
|
|
};
|
2016-11-23 11:38:38 +00:00
|
|
|
removeKey(sshKeyArg: SshKey) {
|
|
|
|
this._syncAuto('from')
|
|
|
|
let filteredArray = this._sshKeyArray.filter((sshKeyArg2: SshKey) => {
|
|
|
|
return (sshKeyArg != sshKeyArg2)
|
|
|
|
})
|
|
|
|
this._sshKeyArray = filteredArray
|
|
|
|
this._syncAuto('to')
|
2016-06-01 01:57:17 +00:00
|
|
|
};
|
2016-11-23 11:38:38 +00:00
|
|
|
replaceKey(sshKeyOldArg: SshKey,sshKeyNewArg: SshKey) {
|
|
|
|
this._syncAuto('from')
|
|
|
|
this.removeKey(sshKeyOldArg)
|
|
|
|
this.addKey(sshKeyNewArg)
|
|
|
|
this._syncAuto('to')
|
2016-06-01 01:57:17 +00:00
|
|
|
};
|
2016-11-23 11:38:38 +00:00
|
|
|
|
2016-06-01 01:57:17 +00:00
|
|
|
//
|
2016-11-23 11:38:38 +00:00
|
|
|
getKey(hostArg: string): SshKey {
|
|
|
|
this._syncAuto('from')
|
2016-06-24 18:58:55 +00:00
|
|
|
let filteredArray = this._sshKeyArray.filter(function(keyArg){
|
2016-11-23 11:38:38 +00:00
|
|
|
return (keyArg.host === hostArg)
|
|
|
|
})
|
|
|
|
if (filteredArray.length > 0) {
|
|
|
|
return filteredArray[0]
|
2016-05-31 17:00:52 +00:00
|
|
|
} else {
|
2016-11-23 11:38:38 +00:00
|
|
|
return undefined
|
2016-05-31 17:00:52 +00:00
|
|
|
}
|
|
|
|
};
|
2016-11-23 11:38:38 +00:00
|
|
|
get sshKeys(): SshKey[] {
|
|
|
|
this._syncAuto('from')
|
|
|
|
return this._sshKeyArray
|
2016-06-24 00:49:55 +00:00
|
|
|
};
|
|
|
|
|
2016-11-23 11:38:38 +00:00
|
|
|
// FS methods
|
2016-06-24 00:49:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* write SshInstance to disk
|
|
|
|
*/
|
2016-11-23 11:38:38 +00:00
|
|
|
writeToDisk(dirPathArg?: string) {
|
|
|
|
this._sync('to',dirPathArg)
|
2016-06-01 00:48:38 +00:00
|
|
|
}
|
2016-06-24 00:49:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* read ab SshInstance from disk
|
|
|
|
*/
|
2016-11-23 11:38:38 +00:00
|
|
|
readFromDisk(dirPathArg?: string) {
|
|
|
|
this._sync('from',dirPathArg)
|
2016-06-24 00:49:55 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 18:58:55 +00:00
|
|
|
/* ===============================================================
|
|
|
|
========================= Private Methods ========================
|
|
|
|
================================================================*/
|
|
|
|
|
2016-11-23 11:38:38 +00:00
|
|
|
private _makeConfig () {
|
2016-06-24 18:58:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-24 00:49:55 +00:00
|
|
|
/**
|
|
|
|
* method to invoke SshInstance _sync automatically when sshSync is true
|
|
|
|
*/
|
2016-11-23 11:38:38 +00:00
|
|
|
private _syncAuto(directionArg) {
|
|
|
|
if (this._sshSync) this._sync(directionArg)
|
2016-06-24 00:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* private method to sync SshInstance
|
|
|
|
*/
|
2016-11-23 11:38:38 +00:00
|
|
|
private _sync(directionArg: string,dirPathArg?: string) {
|
|
|
|
if (directionArg === 'from') {
|
|
|
|
this._sshDir.readFromDir(dirPathArg) // call sync method of sshDir class;
|
|
|
|
} else if (directionArg === 'to') {
|
|
|
|
this._sshDir.writeToDir(dirPathArg)
|
2016-06-24 00:49:55 +00:00
|
|
|
} else {
|
2016-11-23 11:38:38 +00:00
|
|
|
throw new Error("directionArg not recognised. Must be 'to' or 'from'")
|
2016-05-31 17:00:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|