update to latest standards

This commit is contained in:
2016-11-23 12:38:38 +01:00
parent 8f29f234f1
commit 27237f14c7
29 changed files with 606 additions and 593 deletions

View File

@ -1,7 +1,7 @@
import "typings-global"
import * as plugins from "./smartssh.plugins";
import 'typings-global'
import * as plugins from './smartssh.plugins'
export {SshInstance} from "./smartssh.classes.sshinstance";
export {SshKey} from "./smartssh.classes.sshkey";
export {SshDir} from "./smartssh.classes.sshdir";
export {SshConfig} from "./smartssh.classes.sshconfig";
export {SshInstance} from './smartssh.classes.sshinstance'
export {SshKey} from './smartssh.classes.sshkey'
export {SshDir} from './smartssh.classes.sshdir'
export {SshConfig} from './smartssh.classes.sshconfig'

View File

@ -1,8 +1,8 @@
import "typings-global"
import * as plugins from "./smartssh.plugins";
import {SshKey} from "./smartssh.classes.sshkey";
import 'typings-global'
import * as plugins from './smartssh.plugins'
import {SshKey} from './smartssh.classes.sshkey'
export let sshKeyArrayFromDir = function(dirArg:string):SshKey[]{
let sshKeyArray = []; //TODO
return sshKeyArray;
}
export let sshKeyArrayFromDir = function(dirArg: string): SshKey[]{
let sshKeyArray = [] // TODO
return sshKeyArray
}

View File

@ -1,54 +1,54 @@
import "typings-global";
import * as plugins from "./smartssh.plugins";
import * as helpers from "./smartssh.classes.helpers";
import {SshKey} from "./smartssh.classes.sshkey"
import 'typings-global'
import * as plugins from './smartssh.plugins'
import * as helpers from './smartssh.classes.helpers'
import {SshKey} from './smartssh.classes.sshkey'
export class SshConfig {
private _sshKeyArray:SshKey[];
constructor(sshKeyArrayArg:SshKey[]){
this._sshKeyArray = sshKeyArrayArg;
private _sshKeyArray: SshKey[]
constructor(sshKeyArrayArg: SshKey[]) {
this._sshKeyArray = sshKeyArrayArg
}
/**
* stores a config file
*/
store(dirPathArg:string){
let done = plugins.q.defer();
let configArray:configObject[] = [];
let configString;
for(let key in this._sshKeyArray){
let sshKey = this._sshKeyArray[key];
if(sshKey.host){
configString = "Host " + sshKey.host + "\n" +
" HostName " + sshKey.host + "\n" +
" IdentityFile ~/.ssh/" + sshKey.host + "\n" +
" StrictHostKeyChecking no" + "\n"
store(dirPathArg: string) {
let done = plugins.q.defer()
let configArray: configObject[] = []
let configString
for (let key in this._sshKeyArray) {
let sshKey = this._sshKeyArray[key]
if (sshKey.host) {
configString = 'Host ' + sshKey.host + '\n' +
' HostName ' + sshKey.host + '\n' +
' IdentityFile ~/.ssh/' + sshKey.host + '\n' +
' StrictHostKeyChecking no' + '\n'
}
configArray.push({
configString:configString,
configString: configString,
authorized: sshKey.authorized,
sshKey: sshKey
});
})
}
let configFile:string = "";
for(let key in configArray){
configFile = configFile + configArray[key].configString + "\n";
let configFile: string = ''
for (let key in configArray) {
configFile = configFile + configArray[key].configString + '\n'
};
plugins.smartfile.memory.toFsSync(configFile,plugins.path.join(dirPathArg,"config"));
return done.promise;
plugins.smartfile.memory.toFsSync(configFile,plugins.path.join(dirPathArg,'config'))
return done.promise
}
read(dirPathArg){
let done = plugins.q.defer();
let configArray:configObject[];
plugins.smartfile.fs.toStringSync(plugins.path.join(dirPathArg,"config"));
read(dirPathArg) {
let done = plugins.q.defer()
let configArray: configObject[]
plugins.smartfile.fs.toStringSync(plugins.path.join(dirPathArg,'config'))
return done.promise;
return done.promise
}
};
export interface configObject {
configString:string;
authorized:boolean;
sshKey:SshKey;
configString: string
authorized: boolean
sshKey: SshKey
};

View File

@ -1,38 +1,38 @@
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 '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'
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;
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/");
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;
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);
sshKeyArg.store(path)
})
this._sshConfig.store(path)
};
readFromDir(dirPathArg?:string){ // syncs sshInstance from directory
let path = this._path;
if(dirPathArg) path = dirPathArg;
readFromDir(dirPathArg?: string) { // syncs sshInstance from directory
let path = this._path
if (dirPathArg) path = dirPathArg
}
updateDirPath(dirPathArg:string){
this._path = dirPathArg;
updateDirPath(dirPathArg: string) {
this._path = dirPathArg
};
getKeys(){
return helpers.sshKeyArrayFromDir(this._path);
getKeys() {
return helpers.sshKeyArrayFromDir(this._path)
}
}
}

View File

@ -1,103 +1,103 @@
import "typings-global"
import * as plugins from "./smartssh.plugins";
import * as helpers from "./smartssh.classes.helpers";
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;
};
//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;
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
};
//FS methods
// 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);
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 (){
private _makeConfig () {
}
/**
* method to invoke SshInstance _sync automatically when sshSync is true
*/
private _syncAuto(directionArg){
if(this._sshSync) this._sync(directionArg);
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);
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'");
throw new Error("directionArg not recognised. Must be 'to' or 'from'")
}
};
}

View File

@ -1,96 +1,99 @@
import "typings-global";
import * as plugins from "./smartssh.plugins";
import * as helpers from "./smartssh.classes.helpers";
import 'typings-global'
import * as plugins from './smartssh.plugins'
import * as helpers from './smartssh.classes.helpers'
export class SshKey {
private _privKey:string;
private _pubKey:string;
private _hostVar:string;
private _authorized:boolean;
constructor(optionsArg:{private?:string,public?:string,host?:string,authorized?:boolean}={}){
this._privKey = optionsArg.private;
this._pubKey = optionsArg.public;
this._hostVar = optionsArg.host;
this._authorized = optionsArg.authorized;
private _privKey: string
private _pubKey: string
private _hostVar: string
private _authorized: boolean
/**
* the constructor for class SshKey
*/
constructor(optionsArg: {private?: string,public?: string,host?: string,authorized?: boolean}= {}) {
this._privKey = optionsArg.private
this._pubKey = optionsArg.public
this._hostVar = optionsArg.host
this._authorized = optionsArg.authorized
};
// this.host
get host(){
return this._hostVar;
return this._hostVar
};
set host(hostArg:string){
this._hostVar = hostArg;
set host(hostArg: string){
this._hostVar = hostArg
};
// this.privKey
get privKey(){
return this._privKey;
return this._privKey
};
set privKey(privateKeyArg:string){
this._privKey = privateKeyArg;
set privKey(privateKeyArg: string){
this._privKey = privateKeyArg
};
// this.privKeyBase64
get privKeyBase64(){
return plugins.base64.encode(this._privKey);
return plugins.smartstring.base64.encode(this._privKey)
}
set privKeyBase64(privateKeyArg:string) {
this._privKey = plugins.base64.decode(privateKeyArg);
set privKeyBase64(privateKeyArg: string) {
this._privKey = plugins.smartstring.base64.decode(privateKeyArg)
}
// this.pubKey
get pubKey(){
return this._pubKey;
return this._pubKey
}
set pubKey(publicKeyArg:string){
this._pubKey = publicKeyArg;
set pubKey(publicKeyArg: string){
this._pubKey = publicKeyArg
};
// this.pubKeyBase64
get pubKeyBase64(){
return plugins.base64.encode(this._pubKey);
return plugins.smartstring.base64.encode(this._pubKey)
}
set pubKeyBase64(publicKeyArg:string) {
this._pubKey = plugins.base64.decode(publicKeyArg);
set pubKeyBase64(publicKeyArg: string) {
this._pubKey = plugins.smartstring.base64.decode(publicKeyArg)
}
get authorized(){
return this._authorized;
return this._authorized
}
set authorized(authorizedArg:boolean){
this._authorized = authorizedArg;
set authorized(authorizedArg: boolean){
this._authorized = authorizedArg
}
get type(){
if(this._privKey && this._pubKey){
return "duplex";
} else if(this._privKey){
return "private";
} else if(this._pubKey){
return "public";
if (this._privKey && this._pubKey) {
return 'duplex'
} else if (this._privKey) {
return 'private'
} else if (this._pubKey) {
return 'public'
}
};
set type(someVlueArg:any){
console.log("the type of an SshKey connot be set. This value is autpcomputed.")
set type(someVlueArg: any){
console.log('the type of an SshKey connot be set. This value is autpcomputed.')
}
// methods
read(filePathArg){
read(filePathArg) {
}
store(dirPathArg:string){
plugins.fs.ensureDirSync(dirPathArg);
let fileNameBase = this.host;
if(this._privKey){
let filePath = plugins.path.join(dirPathArg,fileNameBase);
plugins.smartfile.memory.toFsSync(this._privKey,filePath);
plugins.shelljs.chmod(600,filePath);
store(dirPathArg: string) {
plugins.fs.ensureDirSync(dirPathArg)
let fileNameBase = this.host
if (this._privKey) {
let filePath = plugins.path.join(dirPathArg,fileNameBase)
plugins.smartfile.memory.toFsSync(this._privKey,filePath)
plugins.shelljs.chmod(600,filePath)
};
if (this._pubKey){
let filePath = plugins.path.join(dirPathArg,fileNameBase + ".pub");
plugins.smartfile.memory.toFsSync(this._pubKey,filePath);
plugins.shelljs.chmod(600,filePath);
if (this._pubKey) {
let filePath = plugins.path.join(dirPathArg,fileNameBase + '.pub')
plugins.smartfile.memory.toFsSync(this._pubKey,filePath)
plugins.shelljs.chmod(600,filePath)
}
}
}
let testKey = new SshKey();

View File

@ -1,10 +1,22 @@
import "typings-global"
export import beautylog = require("beautylog");
export let base64 = require("js-base64").Base64;
export let fs = require("fs-extra");
export let minimatch = require("minimatch");
export import path = require("path");
export let q = require("q");
export let shelljs = require("shelljs");
export import smartfile = require("smartfile");
export import smartpath = require("smartpath");
import 'typings-global'
import * as beautylog from 'beautylog'
import * as fs from 'fs-extra'
import * as minimatch from 'minimatch'
import * as path from 'path'
import * as q from 'q'
import * as shelljs from 'shelljs'
import * as smartfile from 'smartfile'
import * as smartpath from 'smartpath'
import * as smartstring from 'smartstring'
export {
beautylog,
fs,
minimatch,
path,
q,
shelljs,
smartfile,
smartpath,
smartstring
}