update structure
This commit is contained in:
@ -1,38 +1,42 @@
|
||||
import 'typings-global'
|
||||
import * as plugins from './smartssh.plugins'
|
||||
import * as helpers from './smartssh.classes.helpers'
|
||||
import {SshInstance} from './smartssh.classes.sshinstance'
|
||||
import {SshKey} from './smartssh.classes.sshkey'
|
||||
import {SshConfig} from './smartssh.classes.sshconfig'
|
||||
import { SshInstance } from './smartssh.classes.sshinstance'
|
||||
import { SshKey } from './smartssh.classes.sshkey'
|
||||
import { SshConfig } from './smartssh.classes.sshconfig'
|
||||
|
||||
export class SshDir { // sshDir class -> NOT EXPORTED, ONLY FOR INTERNAL USE
|
||||
private _path: string // the path of the ssh directory
|
||||
private _sshKeyArray: SshKey[]
|
||||
private _sshConfig: SshConfig
|
||||
constructor(sshKeyArray: SshKey[],sshConfig: SshConfig,sshDirPathArg?: string) {
|
||||
this._sshKeyArray = sshKeyArray
|
||||
this._sshConfig = sshConfig
|
||||
if (sshDirPathArg) {
|
||||
this._path = sshDirPathArg
|
||||
} else {
|
||||
this._path = plugins.path.join(plugins.smartpath.get.home(),'.ssh/')
|
||||
};
|
||||
}
|
||||
writeToDir(dirPathArg?: string) { // syncs sshInstance to directory
|
||||
let path = this._path
|
||||
if (dirPathArg) path = dirPathArg
|
||||
this._sshKeyArray.forEach((sshKeyArg) => {
|
||||
sshKeyArg.store(path)
|
||||
})
|
||||
this._sshConfig.store(path)
|
||||
};
|
||||
readFromDir(dirPathArg?: string) { // syncs sshInstance from directory
|
||||
let path = this._path
|
||||
if (dirPathArg) path = dirPathArg
|
||||
}
|
||||
updateDirPath(dirPathArg: string) {
|
||||
this._path = dirPathArg
|
||||
};
|
||||
getKeys() {
|
||||
return helpers.sshKeyArrayFromDir(this._path)
|
||||
private _path: string // the path of the ssh directory
|
||||
private _sshKeyArray: SshKey[]
|
||||
private _sshConfig: SshConfig
|
||||
constructor (sshKeyArray: SshKey[], sshConfig: SshConfig, sshDirPathArg?: string) {
|
||||
this._sshKeyArray = sshKeyArray
|
||||
this._sshConfig = sshConfig
|
||||
if (sshDirPathArg) {
|
||||
this._path = sshDirPathArg
|
||||
} else {
|
||||
this._path = plugins.path.join(plugins.smartpath.get.home(), '.ssh/')
|
||||
}
|
||||
}
|
||||
|
||||
writeToDir (dirPathArg?: string) { // syncs sshInstance to directory
|
||||
let path = this._path
|
||||
if (dirPathArg) path = dirPathArg
|
||||
this._sshKeyArray.forEach((sshKeyArg) => {
|
||||
sshKeyArg.store(path)
|
||||
})
|
||||
this._sshConfig.store(path)
|
||||
}
|
||||
|
||||
readFromDir (dirPathArg?: string) { // syncs sshInstance from directory
|
||||
let path = this._path
|
||||
if (dirPathArg) path = dirPathArg
|
||||
}
|
||||
updateDirPath (dirPathArg: string) {
|
||||
this._path = dirPathArg
|
||||
}
|
||||
|
||||
getKeys () {
|
||||
return helpers.sshKeyArrayFromDir(this._path)
|
||||
}
|
||||
}
|
||||
|
@ -2,102 +2,102 @@ 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'
|
||||
import { SshDir } from './smartssh.classes.sshdir'
|
||||
import { SshConfig } from './smartssh.classes.sshconfig'
|
||||
import { SshKey } from './smartssh.classes.sshkey'
|
||||
|
||||
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')
|
||||
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')
|
||||
};
|
||||
// altering methods
|
||||
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')
|
||||
};
|
||||
|
||||
//
|
||||
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]
|
||||
} else {
|
||||
return undefined
|
||||
}
|
||||
};
|
||||
get sshKeys(): SshKey[] {
|
||||
this._syncAuto('from')
|
||||
return this._sshKeyArray
|
||||
};
|
||||
|
||||
// FS methods
|
||||
|
||||
/**
|
||||
* write SshInstance to disk
|
||||
*/
|
||||
writeToDisk(dirPathArg?: string) {
|
||||
this._sync('to',dirPathArg)
|
||||
//
|
||||
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 ]
|
||||
} else {
|
||||
return undefined
|
||||
}
|
||||
};
|
||||
get sshKeys (): SshKey[] {
|
||||
this._syncAuto('from')
|
||||
return this._sshKeyArray
|
||||
};
|
||||
|
||||
/**
|
||||
* read ab SshInstance from disk
|
||||
*/
|
||||
readFromDisk(dirPathArg?: string) {
|
||||
this._sync('from',dirPathArg)
|
||||
// FS methods
|
||||
|
||||
/**
|
||||
* write SshInstance to disk
|
||||
*/
|
||||
writeToDisk (dirPathArg?: string) {
|
||||
this._sync('to', dirPathArg)
|
||||
}
|
||||
|
||||
/**
|
||||
* read ab SshInstance from disk
|
||||
*/
|
||||
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 method to sync SshInstance
|
||||
*/
|
||||
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)
|
||||
} else {
|
||||
throw new Error("directionArg not recognised. Must be 'to' or 'from'")
|
||||
}
|
||||
|
||||
/* ===============================================================
|
||||
========================= Private Methods ========================
|
||||
================================================================*/
|
||||
|
||||
private _makeConfig () {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* method to invoke SshInstance _sync automatically when sshSync is true
|
||||
*/
|
||||
private _syncAuto(directionArg) {
|
||||
if (this._sshSync) this._sync(directionArg)
|
||||
}
|
||||
|
||||
/**
|
||||
* private method to sync SshInstance
|
||||
*/
|
||||
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)
|
||||
} else {
|
||||
throw new Error("directionArg not recognised. Must be 'to' or 'from'")
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user