1
0
Fork 0

command line interface; WIP

This commit is contained in:
Ruben Meyer 2019-06-17 02:28:44 +02:00
parent f5a7c94c14
commit f0863b517a
5 changed files with 248 additions and 0 deletions

View File

@ -0,0 +1,52 @@
/*
* 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['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();
}
}
};

View File

@ -0,0 +1,14 @@
/*
* 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();
}
};

View File

@ -0,0 +1,14 @@
/*
* 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) => {
this.log(JSON.stringify(global['gds'].cache.logs));
cb();
}
};

View File

@ -0,0 +1,80 @@
/*
* This file is part of the authRXBN single sign-on package.
*
* (c) Ruben Meyer <contact@rxbn.de>
*/
module.exports = {
'command': 'user <action> <nick> [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();
}
}
}
};

88
bin/cli/module.js Normal file
View File

@ -0,0 +1,88 @@
/*
* This file is part of the authRXBN single sign-on package.
*
* (c) Ruben Meyer <contact@rxbn.de>
*/
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();