1
0
Fork 0

general - remove CLI

master
Ruben Meyer 2 years ago
parent 265e270110
commit 4cf687bd58
Signed by: rxbn_
GPG Key ID: BE3BF898BE352FE2

@ -34,7 +34,6 @@ env_vars.forEach((el) => {
if(env_missing) process.exit();
global['modules'].events = load('events'); // event handler
global['modules'].cli = load('cli'); // command line interface
global['modules'].logs = load('logs'); // log handler
global['logs'] = global['modules'].logs; // alias

@ -1,52 +0,0 @@
/*
* This file is part of the authRXBN single sign-on package.
*
* (c) Ruben Meyer <contact@rxbn.de>
*/
module.exports = {
'command': 'cache [action] [type]',
'description': 'do something with the cache',
'actionDependencies': ['vorpal'],
'action': (actionDependencies) => {
let vorpal = actionDependencies.vorpal;
return (args, cb) => {
if(typeof args.action !== 'undefined') {
let action = args.action.toLowerCase();
if(typeof args.type !== 'undefined') { var type = args.type.toLowerCase(); }
else { var type = ''; }
//Object.keys(args.options).length > 0
if(action == 'help' || action == '?') {
vorpal.exec('cache --help');
}
else if(action == 'flush') {
if(typeof global['gds'].cache[type] !== "undefined") {
global['gds'].cache[type] = {};
console.log(type+' cache flush');
} else if(type == 'all') {
global['gds'].cache = {};
console.log('full cache flush');
}
else vorpal.exec('cache --help');
}
else if(action == 'get') {
console.log(Object.keys(global['gds'].cache));
console.log(type);
console.log(type in Object.keys(global['gds'].cache));
console.log(Object.keys(global['gds'].cache).hasOwnProperty(type));
if(typeof global['gds'].cache[type] !== "undefined") {
console.log(JSON.stringify(global['gds'].cache[type]));
} else if(type == 'all'){
console.log(JSON.stringify(global['gds'].cache));
} else {
console.log("The cache \""+type+"\" doesnt exists.");
vorpal.exec('cache --help');
}
} else vorpal.exec('cache --help');
} else vorpal.exec('cache --help');
cb();
}
}
};

@ -1,14 +0,0 @@
/*
* This file is part of the authRXBN single sign-on package.
*
* (c) Ruben Meyer <contact@rxbn.de>
*/
module.exports = {
'command': 'clear',
'description': 'clear the output',
'action': (args, cb) => {
process.stdout.write ("\u001B[2J\u001B[0;0f");
cb();
}
};

@ -1,14 +0,0 @@
/*
* This file is part of the authRXBN single sign-on package.
*
* (c) Ruben Meyer <contact@rxbn.de>
*/
module.exports = {
'command': 'logs',
'description': 'output logs',
'action': (args, cb) => {
console.log(JSON.stringify(global['gds'].cache.logs));
cb();
}
};

@ -1,238 +0,0 @@
/*
* This file is part of the authRXBN single sign-on package.
*
* (c) Ruben Meyer <contact@rxbn.de>
*/
module.exports = {
'command': 'user <action> <nick> [data...]',
'description': 'add, get, update or remove an user',
'actionDependencies': ['vorpal'],
'action': (actionDependencies) => {
let vorpal = actionDependencies.vorpal;
return (args, cb) => {
if(typeof args.action !== 'undefined') {
let action = args.action.toLowerCase();
let profile = {
user: args.nick,
pass: null,
mail: null,
group: 0
};
// add a new user
if(action === 'add') {
if(Array.isArray(args.data) && args.data.length >= 1) {
// set data
profile.pass = global['modules'].auth.generateHash(args.data[0]);
if(args.data.length >= 2) profile.mail = args.data[1];
if(args.data.length >= 3) profile.group = args.data[2];
// haystack verifying
let haystack = profile.user;
if(typeof profile.mail !== 'undefined' && profile.mail !== null) haystack = [profile.user, profile.mail];
// query user by haystack
global['modules'].database.getUser(haystack, (err, rep) => {
if(err) {
global['logs'].error("ERR: While finding user");
global['logs'].error(err);
}
else {
// no users exist, add user
if (!rep) {
global['modules'].database.addUser(profile.user, (profile.mail || ''), profile.pass, profile.group, (errAdd, repAdd) => {
if(errAdd) {
global['logs'].error("ERR: While adding user");
global['logs'].error(errAdd);
}
else vorpal.log("Reply: "+repAdd);
});
// user already exists
} else {
vorpal.log("User exists: ");
rep.forEach((el, i) => {
el["passhash"] = undefined; // not needed
rep[i] = el;
});
vorpal.log(rep);
}
}
});
// missing data
} else {
global['logs'].log("No data is present or is missing. Please see:");
global['logs'].log("$ user help add");
cb();
}
// query users
} else if(action === 'get' || action === 'ls') {
// wildcard catch-all
if(profile.user === '*') {
global['modules'].database.getUsers((err, rep) => {
if(rep) {
rep.forEach((el, i) => {
el["passhash"] = undefined; // not needed
rep[i] = el;
});
global['logs'].log(rep);
}
if(err) {
global['logs'].error('$ user get *');
global['logs'].error(err);
}
});
} else {
// query users by first input <nickname>
global['modules'].database.getUser(profile.user, (err, rep) => {
// user exists
if(rep && rep.length == 1) {
global['logs'].log("User exists: ");
rep.forEach((el, i) => {
el["passhash"] = undefined; // not needed
rep[i] = el;
});
global['logs'].log(rep);
} else {
// found more than one user
if(rep && rep.length >= 2) {
global['logs'].warn("multiple users found for "+profile.user+".");
rep.forEach((el) => {
global['logs'].warn("found user with id: "+el._id);
});
// user does not exist
} else {
global['logs'].warn("User "+profile.user+" doesn't exist.");
}
}
// query error
if(err) {
global['logs'].error('$ user get '+profile.user);
global['logs'].error(err);
}
});
}
// update users, just one property
} else if(action === 'update') {
if(args.data.length < 2) global['logs'].error("No data supplied.");
else {
let property = args.data[0];
let param = args.data[1];
global['logs'].debug("Prop: "+property+"; Param: "+param);
// query user
global['modules'].database.getUser(profile.user, (err, rep) => {
// user exists
if(rep && rep.length == 1) {
let obj = {};
obj[property] = param;
global['modules'].database.updateUser(String(rep[0]._id), obj, (errUpd, repUpd) => {
// updated user
if(repUpd) {
global['logs'].log("User with id "+String(rep[0]._id)+" was updated.");
// user not updated
} else {
global['logs'].warn("User with id "+String(rep[0]._id)+" doesn't exist.");
}
// query error
if(errUpd) {
global['logs'].error('$ user update '+profile.user+' '+property+' '+param+' [on update]');
global['logs'].error(errUpd);
}
});
} else {
// found more than one user
if(rep && rep.length >= 2) {
global['logs'].warn("multiple users found for "+profile.user+". bad state. can't update.");
rep.forEach((el) => {
global['logs'].warn("found user with id: "+el._id);
});
// user does not exist
} else {
global['logs'].warn("User "+profile.user+" doesn't exist.");
}
}
// query error
if(err) {
global['logs'].error('$ user update '+profile.user+' '+field+' '+param+' [on query]');
global['logs'].error(err);
}
});
}
// remove users
} else if(action === 'remove' || action === 'delete') {
// haystack
let haystack = profile.user;
if(typeof profile.mail !== 'undefined' && profile.mail !== null) haystack = [profile.user, profile.mail];
// query user
global['modules'].database.getUser(haystack, (err, rep) => {
if(rep) {
vorpal.log("user exists. deleting him.");
global['logs'].debug(rep);
// remove user
global['modules'].database.delUser(rep[0].email, (errDel, repDel) => {
if(repDel) {
vorpal.log("deleted user.");
global['logs'].debug(repDel);
}
else {
vorpal.log("ERR: while deleting user.");
global['logs'].debug(errDel);
}
});
}
});
} else if(action === 'help') {
if(args.nick === 'add') {
vorpal.log("user add <nickname> <raw password> <mail> [group]");
vorpal.log("<nickname>: user nickname");
vorpal.log("<raw password>: will be hashed ASAP");
vorpal.log("<mail>: format: user@example.tld");
vorpal.log("[group]: not needed; only Numbers; group id");
vorpal.log("---");
vorpal.log("returns 0 or 1 and printing errors");
} else if(args.nick === 'get') {
vorpal.log("user get <nickname or email>");
vorpal.log("<nickname or email>: searching both in both; format: foobar OR user@example.tld");
vorpal.log("---");
vorpal.log("user get * - to get all users");
vorpal.log("prints JSON-object of user data");
} else if(args.nick === 'update') {
vorpal.log("user update <nickname> <field> <parameter>");
vorpal.log("<nickname>: user nickname");
vorpal.log("<field>: string");
vorpal.log("<parameter>: mixed data; will be converted to Boolean, Number or String");
vorpal.log("---");
vorpal.log("returns 0 or 1 and printing errors");
vorpal.log("prints JSON-object of updated user data");
} else if(args.nick === 'remove' || args.nick === 'delete') {
vorpal.log("user remove|delete <nickname>");
vorpal.log("<nickname>: user nickname or mail");
vorpal.log("---");
vorpal.log("returns 0 or 1 and printing errors");
}
}
cb();
} else {
vorpal.exec('user --help');
cb();
}
}
}
};

@ -1,94 +0,0 @@
/*
* This file is part of the authRXBN single sign-on package.
*
* (c) Ruben Meyer <contact@rxbn.de>
*/
let vorpal = require('vorpal')();
let chalk = require('chalk');
let fs = require('fs');
/**
* read command files and interpret them
* @author Ruben Meyer
* @todo options, types, hide command, parsing, help, autocompletion, allow unknown options
*/
let cmdPath = global['__dirname']+'/bin/cli/cmds';
fs.readdir(cmdPath, (err, files) => {
if(files.length > 0)
files.forEach((file) => {
let cmd = require(cmdPath+'/'+file); // read file
// exported data is an object
if(typeof cmd == 'object' && typeof cmd.command !== 'undefined') {
// set initial building steps
let builder = vorpal.command(cmd.command);
// description
if(typeof cmd.description !== 'undefined') builder = builder.description(cmd.description);
// aliases
if(typeof cmd.alias !== 'undefined') {
if(typeof cmd.alias === 'object' && Array.isArray(cmd.alias)) builder = builder['alias'](...cmd.alias);
if(typeof cmd.alias === 'string' && cmd.alias.split(',').length >= 2) {
let args = cmd.alias.split(',');
for(let i = 0; i < args.length; i++)
args[i] = args[i].trim();
builder = builder['alias'](...cmd.alias);
}
}
// action
if(typeof cmd.action !== 'undefined' && typeof cmd.action === 'function') {
if(typeof cmd.actionDependencies !== 'undefined') {
let dependencies = [];
let actionDependencies = {};
// format input
if(typeof cmd.actionDependencies === 'object') {
if(Array.isArray(cmd.actionDependencies))
cmd.actionDependencies.forEach((dependency) => dependencies.push(dependency));
} else if(typeof cmd.actionDependencies === 'string') {
let strArr = cmd.actionDependencies.split(',');
for(let i = 0; i < strArr.length; i++)
dependencies.push(strArr[i].trim());
}
// retrieve dependencies; unknown dependencies wont be handled
dependencies.forEach((dependency) => {
switch(dependency) {
case 'vorpal':
actionDependencies['vorpal'] = vorpal;
break;
case 'chalk':
actionDependencies['chalk'] = chalk;
break;
}
});
builder = builder['action'](cmd.action(actionDependencies));
} else {
builder = builder['action'](cmd.action);
}
}
}
});
});
/**
@TODO remove code
isJson = (str) => {
try {
let o = JSON.parse(str);
return (o && typeof o === "object") ? true : false;
} catch (e) {
return false;
}
};
*/
vorpal.delimiter('auth@rxbn$').show();
module.exports = vorpal;

@ -46,7 +46,7 @@ newLine = (prefix, obj) => {
}
};
fallback = (fn, ...data) => {
log = (fn, ...data) => {
if(data.length == 1) data = data[0];
fn.apply(null, data);
@ -55,8 +55,7 @@ fallback = (fn, ...data) => {
// LOG | INFO
methods.log = (...data) => {
if(global['modules'].cli) global['modules'].cli.log.apply(global['modules'].cli, data);
else fallback(console.log, data);
log(console.log, data);
if(data.length == 1) data = data[0];
newLine(" [LOG]", data);
@ -65,8 +64,7 @@ methods.info = methods.log;
// WARNING
methods.warn = (...data) => {
if(global['modules'].cli) global['modules'].cli.log.apply(global['modules'].cli, data);
else fallback(console.warn, data);
log(console.warn, data);
if(data.length == 1) data = data[0];
newLine(" [WARN]", data);
@ -74,8 +72,7 @@ methods.warn = (...data) => {
// ERROR
methods.error = (...data) => {
if(global['modules'].cli) global['modules'].cli.log.apply(global['modules'].cli, data);
else fallback(console.error, data);
log(console.error, data);
if(data.length == 1) data = data[0];
newLine("[ERROR]", data);
@ -85,8 +82,7 @@ methods.err = methods.error;
// DEBUG
methods.debug = (...data) => {
if(global['gds'].debug) {
if(global['modules'].cli) global['modules'].cli.log.apply(global['modules'].cli, data);
else fallback(console.log, data);
log(console.log, data);
if(data.length == 1) data = data[0];
newLine("[DEBUG]", data);

Loading…
Cancel
Save