From f0863b517a7ba026e49f0c545b7ce12983fa4d1c Mon Sep 17 00:00:00 2001 From: Ruben Meyer <46384706+rxbnDE@users.noreply.github.com> Date: Mon, 17 Jun 2019 02:28:44 +0200 Subject: [PATCH] command line interface; WIP --- 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 | 80 +++++++++++++++++++++++++++++ bin/cli/module.js | 88 ++++++++++++++++++++++++++++++++ 5 files changed, 248 insertions(+) create mode 100644 bin/cli/cmds/cache_handler.js create mode 100644 bin/cli/cmds/clear_console.js create mode 100644 bin/cli/cmds/print_cached_log.js create mode 100644 bin/cli/cmds/user_management.js create mode 100644 bin/cli/module.js diff --git a/bin/cli/cmds/cache_handler.js b/bin/cli/cmds/cache_handler.js new file mode 100644 index 0000000..37b3874 --- /dev/null +++ b/bin/cli/cmds/cache_handler.js @@ -0,0 +1,52 @@ +/* + * 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['app'].cache[type] !== "undefined") { + global['app'].cache[type] = {}; + console.log(type+' cache flush'); + } else if(type == 'all') { + global['app'].cache = {}; + console.log('full cache flush'); + } + else vorpal.exec('cache --help'); + } + else if(action == 'get') { + console.log(Object.keys(global['app'].cache)); + console.log(type); + console.log(type in Object.keys(global['app'].cache)); + console.log(Object.keys(global['app'].cache).hasOwnProperty(type)); + if(typeof global['app'].cache[type] !== "undefined") { + console.log(JSON.stringify(global['app'].cache[type])); + } else if(type == 'all'){ + console.log(JSON.stringify(global['app'].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 new file mode 100644 index 0000000..9989c01 --- /dev/null +++ b/bin/cli/cmds/clear_console.js @@ -0,0 +1,14 @@ +/* + * 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 new file mode 100644 index 0000000..39e5feb --- /dev/null +++ b/bin/cli/cmds/print_cached_log.js @@ -0,0 +1,14 @@ +/* + * This file is part of the authRXBN single sign-on package. + * + * (c) Ruben Meyer + */ + +module.exports = { + 'command': 'logs', + 'description': 'output logs', + 'action': (args, cb) => { + this.log(JSON.stringify(global['gds'].cache.logs)); + cb(); + } +}; diff --git a/bin/cli/cmds/user_management.js b/bin/cli/cmds/user_management.js new file mode 100644 index 0000000..2f1d0cf --- /dev/null +++ b/bin/cli/cmds/user_management.js @@ -0,0 +1,80 @@ +/* + * This file is part of the authRXBN single sign-on package. + * + * (c) Ruben Meyer + */ + +module.exports = { + 'command': 'user [pass] [mail] [group] [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: (args.pass) ? global['app'].modules.auth.generateHash(args.pass) : "-", + mail: null, + group: 0 + }; + if(!args.mail) profile.mail = profile.user; + if(args.mail) profile.mail = args.mail; + if(args.group) profile.group = args.group; + + if(action === 'add') { + + global['app'].modules.database.getUser([profile.user, profile.mail], (err, rep) => { + if(err) vorpal.log("ERR: While finding user"); + else { + if (!rep) { + global['app'].modules.database.addUser(profile.user, profile.mail, profile.pass, profile.group, (errAdd, repAdd) => { + if(errAdd) vorpal.log("ERR: While adding user"); + else vorpal.log("Reply: "+rep); + }); + } else { + vorpal.log("User exists: "); + vorpal.log(rep); + } + } + }); + } else if(action === 'get') { + global['app'].modules.database.getUser([profile.user, profile.mail], (err, rep) => { + if(rep) { + vorpal.log("User exists: "); + vorpal.log(rep); + } else { + vorpal.log("User "+profile.user+" / "+profile.mail+" doesn't exist."); + } + }); + } else if(action === 'update') { + if(args.data.length < 2) vorpal.log("No data supplied."); + else { + let field = args.data[0]; + let param = args.data[1]; + vorpal.log("Field: "+field+"; Param: "+param); + } + } else if(action === 'remove' || action === 'delete') { + global['app'].modules.database.getUser([profile.user, profile.mail], (err, rep) => { + if(rep) { + vorpal.log("User exists. Deleting him."); + vorpal.log(rep); + global['app'].modules.database.delUser(rep.email, (errDel, repDel) => { + if(repDel) vorpal.log("Deleted user."); + else vorpal.log("ERR: While deleting user."); + }); + } + }); + } else if(action === 'genpass') { + vorpal.log(profile.pass); + } + + cb(); + } else { + vorpal.exec('user --help'); + cb(); + } + } + } +}; diff --git a/bin/cli/module.js b/bin/cli/module.js new file mode 100644 index 0000000..1bdcc08 --- /dev/null +++ b/bin/cli/module.js @@ -0,0 +1,88 @@ +/* + * This file is part of the authRXBN single sign-on package. + * + * (c) Ruben Meyer + */ + +let vorpal = require('vorpal')(); +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; + } + }); + + 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();