From 4cf687bd58f0554123c0fe3530d3d7811c9029a6 Mon Sep 17 00:00:00 2001 From: rxbn_ Date: Wed, 12 Aug 2020 11:53:42 +0200 Subject: [PATCH] general - remove CLI --- app.js | 1 - bin/cli/cmds/cache_handler.js | 52 ------- bin/cli/cmds/clear_console.js | 14 -- bin/cli/cmds/print_cached_log.js | 14 -- bin/cli/cmds/user_management.js | 238 ------------------------------- bin/cli/module.js | 94 ------------ bin/logs/module.js | 14 +- 7 files changed, 5 insertions(+), 422 deletions(-) delete mode 100644 bin/cli/cmds/cache_handler.js delete mode 100644 bin/cli/cmds/clear_console.js delete mode 100644 bin/cli/cmds/print_cached_log.js delete mode 100644 bin/cli/cmds/user_management.js delete mode 100644 bin/cli/module.js diff --git a/app.js b/app.js index b2fb18a..ea52023 100644 --- a/app.js +++ b/app.js @@ -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 diff --git a/bin/cli/cmds/cache_handler.js b/bin/cli/cmds/cache_handler.js deleted file mode 100644 index f499fab..0000000 --- a/bin/cli/cmds/cache_handler.js +++ /dev/null @@ -1,52 +0,0 @@ -/* - * This file is part of the authRXBN single sign-on package. - * - * (c) Ruben Meyer - */ - -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(); - } - } -}; diff --git a/bin/cli/cmds/clear_console.js b/bin/cli/cmds/clear_console.js deleted file mode 100644 index 9989c01..0000000 --- a/bin/cli/cmds/clear_console.js +++ /dev/null @@ -1,14 +0,0 @@ -/* - * This file is part of the authRXBN single sign-on package. - * - * (c) Ruben Meyer - */ - -module.exports = { - 'command': 'clear', - 'description': 'clear the output', - 'action': (args, cb) => { - process.stdout.write ("\u001B[2J\u001B[0;0f"); - cb(); - } -}; diff --git a/bin/cli/cmds/print_cached_log.js b/bin/cli/cmds/print_cached_log.js deleted file mode 100644 index 485552a..0000000 --- a/bin/cli/cmds/print_cached_log.js +++ /dev/null @@ -1,14 +0,0 @@ -/* - * This file is part of the authRXBN single sign-on package. - * - * (c) Ruben Meyer - */ - -module.exports = { - 'command': 'logs', - 'description': 'output logs', - 'action': (args, cb) => { - console.log(JSON.stringify(global['gds'].cache.logs)); - cb(); - } -}; diff --git a/bin/cli/cmds/user_management.js b/bin/cli/cmds/user_management.js deleted file mode 100644 index 33c011a..0000000 --- a/bin/cli/cmds/user_management.js +++ /dev/null @@ -1,238 +0,0 @@ -/* - * This file is part of the authRXBN single sign-on package. - * - * (c) Ruben Meyer - */ - -module.exports = { - 'command': 'user [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 - 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 [group]"); - vorpal.log(": user nickname"); - vorpal.log(": will be hashed ASAP"); - vorpal.log(": 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 "); - vorpal.log(": 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 "); - vorpal.log(": user nickname"); - vorpal.log(": string"); - vorpal.log(": 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 "); - vorpal.log(": user nickname or mail"); - - vorpal.log("---"); - - vorpal.log("returns 0 or 1 and printing errors"); - } - } - - cb(); - } else { - vorpal.exec('user --help'); - cb(); - } - } - } -}; diff --git a/bin/cli/module.js b/bin/cli/module.js deleted file mode 100644 index 27c89b1..0000000 --- a/bin/cli/module.js +++ /dev/null @@ -1,94 +0,0 @@ -/* - * This file is part of the authRXBN single sign-on package. - * - * (c) Ruben Meyer - */ - -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; diff --git a/bin/logs/module.js b/bin/logs/module.js index 9481c5c..0c98b60 100644 --- a/bin/logs/module.js +++ b/bin/logs/module.js @@ -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);