ernest
Version:
Web framework for HTTP and HTTPS, using ExpressJS, Session, Mongo, Socket IO, Redis
977 lines (881 loc) • 17.9 kB
JavaScript
const fs = require('fs');
const DB_BKP = require('./Ernest_DB_BKP');
class Ernest_Commands
{
constructor(ernest,logPath,inputLine,outputLine)
{
this.ernest = ernest;
this.currentUser = null;
this.inputLine = inputLine;
this.outputLine = outputLine;
this.logPath = logPath;
};
MainMenu()
{
var _this = this;
_this.show("\x1b[37mErnest CLI\\>\x1b[0m ",function(input)
{
_this.ProcessCMD(input);
});
};
show(message,callback)
{
this.inputLine(message + " ", (answer) =>
{
callback(answer);
});
};
ProcessCMD(cmd)
{
var _this = this;
if(cmd == "")
{
_this.MainMenu();
}
else
{
if( cmd == 'stop')
{
_this.outputLine("Good Bye");
process.exit(0);
}
else
{
if(cmd == 'clear')
{
process.stdout.write('\x1Bc');
_this.MainMenu();
}else
{
var functs = Object.getOwnPropertyNames(Ernest_Commands.prototype).filter(function (p) {return (p.indexOf("_") == 0); });
var till = functs.length -1;
let notfound = true;
functs.map(function(f,index)
{
if( cmd == f.slice(1))
{
_this[f].call(_this,_this);
notfound = false;
};
if((till == index) && notfound)
{
_this.outputLine("Command not found");
_this.MainMenu();
};
});
};
};
};
};
UserSelection(cli,callback)
{
var _this = this;
if(_this.currentUser != null)
{
callback(_this.currentUser);
}
else
{
cli.show("User: ", user =>
{
callback(user);
});
};
}
_onUser(cli)
{
var _this = this;
cli.show("User: ", user =>
{
_this.currentUser = user;
_this.outputLine("Done");
cli.MainMenu();
});
};
_outUser(cli)
{
var _this = this;
this.currentUser = null;
_this.outputLine("Done");
cli.MainMenu();
}
_reloadREST(cli)
{
var _this = this;
_this.ernest.ReloadTemplaters(function(e,r)
{
_this.outputLine("Reloading Rest Templates ...");
if(e)
{
_this.outputLine(" Error: ");
_this.outputLine(e );
};
if(r)
{
_this.outputLine(" Response: ");
_this.outputLine(r);
};
_this.outputLine("Done");
cli.MainMenu();
});
};
_dbCommand(cli)
{
var _this = this;
cli.show("Command:",function(strObj)
{
var jsonObj = JSON.parse(JSON.stringify(strObj));
_this.ernest.dbc.DBCommand(jsonObj,function(e,r)
{
if(e)
{
_this.outputLine(" Error: ");
_this.outputLine(e );
};
if(r)
{
_this.outputLine(" Response: ");
_this.outputLine(r);
};
cli.MainMenu();
});
});
};
_adminCommand(cli)
{
var _this = this;
cli.show("Command:",function(strObj)
{
var jsonObj = JSON.parse(JSON.stringify(strObj));
_this.ernest.dbc.AdminCommand(jsonObj,function(e,r)
{
if(e)
{
_this.outputLine(" Error: ");
_this.outputLine(e );
};
if(r)
{
_this.outputLine(" Response: ");
_this.outputLine(r);
};
cli.MainMenu();
});
});
};
_rsStatus(cli)
{
var _this = this;
_this.ernest.dbc.rsStatus(function(e,r)
{
if(e)
{
_this.outputLine(" Error: ");
_this.outputLine(e );
};
if(r)
{
_this.outputLine(" Response: ");
_this.outputLine(r);
};
cli.MainMenu();
});
};
_serverStatus(cli)
{
var _this = this;
_this.ernest.dbc.ServerStatus(function(e,r)
{
if(e)
{
_this.outputLine(" Error: ");
_this.outputLine(e );
};
if(r)
{
_this.outputLine(" Response: ");
_this.outputLine(r);
};
cli.MainMenu();
});
};
_addUser(cli)
{
var _this = this ;
_this.UserSelection(cli, user =>
{
if(user=="")
{
_this.outputLine("Cancelled");
_this.outputLine("");
cli.MainMenu();
}
else
{
cli.show("password:",function(password)
{
cli.show("properties (as json):",function(valor)
{
var jprop = JSON.parse(JSON.parse(JSON.stringify(valor)));
_this.ernest.Users.CreateUser(user,password,jprop,function(success)
{
if(success)
{
_this.outputLine("Success: User has been added");
_this.outputLine("");
cli.MainMenu();
}
else
{
_this.outputLine("Error: User already exist");
_this.outputLine("");
cli.MainMenu();
};
});
});
});
};
});
};
_deleteUser(cli)
{
var _this = this ;
_this.UserSelection(cli, user =>
{
if(user=="")
{
_this.outputLine("Cancelled");
_this.outputLine("");
cli.MainMenu();
}
else
{
_this.ernest.Users.DeleteUser(user,function()
{
_this.outputLine("User has been deleted");
_this.outputLine("");
cli.MainMenu();
});
};
});
};
_showUser(cli)
{
var _this = this ;
_this.UserSelection(cli, user =>
{
if(user=="")
{
_this.outputLine("Cancelled");
_this.outputLine("");
cli.MainMenu();
}
else
{
_this.ernest.Users.GetUserAccesses(user,function(r)
{
if(r.length == 1)
{
delete r[0]['_id'];
_this.outputLine(r[0], {depth: null, colors: true});
_this.outputLine("");
cli.MainMenu();
}
else
{
_this.outputLine("Error: User not Found");
_this.outputLine("");
cli.MainMenu();
};
});
};
});
};
_status(cli)
{
var _this = this;
_this.outputLine(_this.ernest.status + ". Ernest Mongo Database Name: '"+ _this.ernest.db_name + "'. ");
cli.MainMenu();
};
_pid(cli)
{
var _this = this;
_this.outputLine("Current Erenst PID: " + process.pid);
cli.MainMenu();
}
_db(cli)
{
var _this = this;
_this.outputLine(_this.ernest.db_name);
cli.MainMenu();
};
_setdb(cli)
{
var _this = this;
_this.outputLine("Current Ernest DB: " + _this.ernest.db_name);
cli.show("New DB Name:",input =>
{
if(input != "")
{
_this.ernest.setDB(input);
_this.outputLine("Done");
};
cli.MainMenu();
});
};
_logs(cli)
{
var _this = this;
fs.readdir(_this.logPath , (e, files) =>
{
if(e)
{
_this.outputLine(" Error: ");
_this.outputLine(e );
}
else
{
files.map((file) =>
{
let d =file.replace(".log","");
if(!isNaN(d))
{
let n = Number(d)
let date = new Date(n);
_this.outputLine(" --> Log Date: " + date + ", --> Log File Name: " + file);
};
});
};
cli.MainMenu();
});
};
_log(cli)
{
var _this = this;
cli.show("Log File Name:",logfile =>
{
if(logfile != "")
{
var data = fs.readFileSync(_this.logPath + "/" + logfile, 'utf-8');
_this.outputLine(" >>>>>>>>>>>>>>>>>>>>>>> " +logfile + " <<<<<<<<<<<<<<<<<<<<<<<");
_this.outputLine(data);
_this.outputLine(" >>>>>>>>>>>>>>>>>>>>>>> " +logfile + " <<<<<<<<<<<<<<<<<<<<<<<");
};
cli.MainMenu();
});
};
_updateUserPassword(cli)
{
var _this = this;
_this.UserSelection(cli, user =>
{
cli.show("New Password: ", pass =>
{
_this.ernest.Users.UpdateUserPassword(user,pass,function(r)
{
_this.outputLine("Done");
cli.MainMenu();
});
});
});
};
_updateUserProperties(cli)
{
var _this = this;
_this.UserSelection(cli, user =>
{
cli.show("New Properties: ", properties =>
{
var json_properties = JSON.parse(properties);
_this.ernest.Users.UpdateUserProperties(user,json_properties,function(r)
{
_this.outputLine("Done");
cli.MainMenu();
});
});
});
};
_deleteUserProperty(cli)
{
var _this = this;
_this.UserSelection(cli, user =>
{
cli.show("Property: ", property =>
{
_this.ernest.Users.DeleteUserProperty(user,property,function(r)
{
_this.outputLine("Done");
cli.MainMenu();
});
});
});
};
_addPagetoUser(cli)
{
var _this = this;
_this.UserSelection(cli, user =>
{
cli.show("New Page: ", page =>
{
cli.show("Level Access: ", level =>
{
_this.ernest.Users.SetUserAccess(user,page,level,function(r)
{
_this.outputLine("Done");
cli.MainMenu();
});
});
});
});
};
_removePageUser(cli)
{
var _this = this;
_this.UserSelection(cli, user =>
{
cli.show("Remove Page: ", page =>
{
_this.ernest.Users.UnsetUserAccess(user,page,function(r)
{
_this.outputLine("Done");
cli.MainMenu();
});
});
});
};
_createCollection(cli)
{
var _this = this;
cli.show("New Collection: ", col =>
{
_this.ernest.dbc.CreateCollection(col,function(e,r)
{
if(e)
{
_this.outputLine(" Error: ");
_this.outputLine(e );
};
if(r)
{
_this.outputLine(" Response: ");
_this.outputLine(r);
};
cli.MainMenu();
});
});
};
_deleteCollection(cli)
{
var _this = this;
cli.show("Collection: ", col =>
{
_this.ernest.dbc.DropCollection(col,function(e,r)
{
if(e)
{
_this.outputLine(" Error: ");
_this.outputLine(e );
};
if(r)
{
_this.outputLine(" Response: ");
_this.outputLine(r);
};
cli.MainMenu();
});
});
};
_collectionExist(cli)
{
var _this = this;
cli.show("Collection: ", col =>
{
_this.ernest.dbc.CollectionExist(col,function(e,r)
{
if(e)
{
_this.outputLine(" Error: ");
_this.outputLine(e );
};
if(r)
{
_this.outputLine(" Response: ");
_this.outputLine(r);
};
cli.MainMenu();
});
});
};
_Collections(cli)
{
var _this = this;
_this.ernest.dbc.getCollectionNames(function(e,d)
{
_this.outputLine({error:e,data:d}, {depth: null, colors: true});
cli.MainMenu();
});
};
_IsPublicAccess(cli)
{
var _this = this;
cli.show("URL: ", iurl =>
{
if(iurl.length > 0)
{
_this.ernest.Permission.IsPublicAccess(iurl,function(r)
{
if(r)
{
_this.outputLine("URL has public access in DB");
cli.MainMenu();
}
else
{
_this.outputLine("URL doesn't has public access in DB");
cli.MainMenu();
};
});
}
else
{
_this.outputLine("Cancelled!");
cli.MainMenu();
}
});
};
_SetPublicAccess(cli)
{
var _this = this;
cli.show("URL: ", iurl =>
{
if(iurl.length > 0)
{
_this.ernest.Permission.SetPublicAccess(iurl,function(r)
{
if(r)
{
_this.outputLine("URL now haspublic access in DB");
cli.MainMenu();
}
else
{
_this.outputLine("operation incomplete");
cli.MainMenu();
};
});
}
else
{
_this.outputLine("Cancelled!");
cli.MainMenu();
}
});
};
_UnSetPublicAccess(cli)
{
var _this = this;
cli.show("URL: ", iurl =>
{
if(iurl.length > 0)
{
_this.ernest.Permission.UnSetPublicAccess(iurl,function(r)
{
if(r)
{
_this.outputLine("URL now doesn't has public access in DB");
cli.MainMenu();
}
else
{
_this.outputLine("operation incomplete");
cli.MainMenu();
};
});
}
else
{
_this.outputLine("Cancelled!");
cli.MainMenu();
}
});
};
_CreatePublicAccess(cli)
{
var _this = this;
cli.show("Access Collection: ", iaccess =>
{
if(iaccess.length > 0)
{
_this.ernest.Permission.CreatePublicAccess(iaccess,function(r)
{
if(r)
{
_this.outputLine("public access in DB created");
cli.MainMenu();
}
else
{
_this.outputLine("public access in DB couldn't being created");
cli.MainMenu();
};
});
}
else
{
_this.outputLine("Cancelled!");
cli.MainMenu();
}
});
};
_ListPublicFiles(cli)
{
var _this = this;
if(_this.ernest.public == null)
{
_this.outputLine("Ernest working without public folder");
cli.MainMenu();
}
else
{
cli.show("Path inside Public folder (default:public/):",function(pth)
{
if(pth == "")
{
fs.readdir(_this.ernest.public, (err, files) =>
{
if(err)
{
throw err;
cli.MainMenu();
}
else
{
var till = files.length -1;
if(till == -1)
{
_this.outputLine("Ernest Public folder has no files");
cli.MainMenu();
}
else
{
files.map(function(file,index)
{
_this.outputLine(file);
if(index == till)
{
cli.MainMenu();
};
});
};
};
});
}
else
{
fs.readdir(_this.ernest.public + "/" +pth, (err, files) =>
{
if(err)
{
throw err;
cli.MainMenu();
}
else
{
var till = files.length -1;
if(till == -1)
{
_this.outputLine("Ernest Public folder has no files");
cli.MainMenu();
}
else
{
files.map(function(file,index)
{
_this.outputLine(file);
if(index == till)
{
cli.MainMenu();
};
});
};
};
});
};
});
};
};
_CopyPublicFile(cli)
{
var _this = this;
if(_this.ernest.public == null)
{
_this.outputLine("Ernest working without public folder");
cli.MainMenu();
}
else
{
cli.show("Public File Path (src. Default public/):",function(pth_src)
{
cli.show("Public File Name (src):",function(name_src)
{
cli.show("New Public File Name (copy):",function(name_cpy)
{
copyFile(_this.ernest.public + "/" +pth_src + name_src, _this.ernest.public + "/" + pth_src + name_cpy , (err) =>
{
if(err)
{
_this.outputLine("Erenst couldn't copy the file");
cli.MainMenu();
}
else
{
cli.MainMenu();
};
});
});
});
});
};
};
_DB_Backup(cli)
{
var _this = this;
let db_bkp = new DB_BKP(_this.ernest.db_name);
cli.show("Output DB Backup Folder:",function(pth_dst)
{
db_bkp.Create_BackUp(pth_dst,function()
{
_this.outputLine("Done");
cli.MainMenu();
});
});
};
_DB_Restore(cli)
{
var _this = this;
let db_bkp = new DB_BKP(_this.ernest.db_name);
cli.show("Backup Source Folder (without file): ",function(src_path)
{
cli.show("Backup Source TAR.GZ File Name (without extension): ",function(tarfilename)
{
cli.show("Database name target: ",function(new_dbname)
{
let tarfile = tarfilename + ".tar.gz";
db_bkp.Restore_BackUp(src_path,tarfile,new_dbname,function(r)
{
if(r)
{
_this.outputLine("Done");
}
else
{
_this.outputLine("Error");
};
cli.MainMenu();
});
});
});
});
};
_man(cli)
{
var _this = this;
let info = `
\x1b[37m reloadREST \x1b[0m
Reload all template controlers after an update in templates or views;
\x1b[37m logs \x1b[0m
Shows all log files;
\x1b[37m log \x1b[0m
Shows the content of a log file;
\x1b[37m dbCommand \x1b[0m
Executes a Obj Form Mongo Command in DB;
\x1b[37m adminCommand \x1b[0m
Executes a Obj Form Mongo Command in adminDB;
\x1b[37m serverStatus \x1b[0m
Show the DB server Status.
\x1b[37m rsStatus \x1b[0m
Show the replicaSet Status.
\x1b[37m onUser \x1b[0m
Sets the user for the next operations.
\x1b[37m outUser \x1b[0m
Unsets the user for the next operations.
\x1b[37m addUser \x1b[0m
Add a new user to your login web system.(user, password, propiertis in json format : {"name": "john doe", "age":34, level:33})
\x1b[37m deleteUser \x1b[0m
Delete a user to your login web system
\x1b[37m showUser \x1b[0m
Show user info
\x1b[37m status \x1b[0m
Show Ernest info (port - http / https)
\x1b[37m pid \x1b[0m
Show Current Ernest PID procces
\x1b[37m db \x1b[0m
Show current Mongo DB
\x1b[37m setdb \x1b[0m
Set a Mongo DB
\x1b[37m updateUserPassword \x1b[0m
Update user password
\x1b[37m updateUserProperties \x1b[0m
Update User properties
\x1b[37m _deleteUserProperty \x1b[0m
Update User properties
\x1b[37m addPagetoUser \x1b[0m
Add permission for user access to the a specific page.
\x1b[37m removePageUser \x1b[0m
Remove permission for user access to the a specific page.
\x1b[37m createCollection \x1b[0m
Create a collection in the current Mongo Db.
\x1b[37m deleteCollection \x1b[0m
Delete a collection in the current Mongo Db.
\x1b[37m collectionExist \x1b[0m
Show if collection exits in the current Mongo DB.
\x1b[37m Collections \x1b[0m
Show all collections in the current Mongo DB.
\x1b[37m IsPublicAccess \x1b[0m
Show if the file has public access in the db.
\x1b[37m SetPublicAccess \x1b[0m
Set file's public access in the db.
\x1b[37m UnSetPublicAccess \x1b[0m
Unset file's public access in the db.
\x1b[37m CreatePublicAccess \x1b[0m
Create public access in DB for first time(One time per DB)
\x1b[37m ListPublicFiles \x1b[0m
Show the files in public folder.
\x1b[37m CopyPublicFile \x1b[0m
Copy one file in public folder, with other name(example: template file) inside public folder.
\x1b[37m DB_Backup \x1b[0m
Create Current Ernest DB Backup. Exports DB to a specific tar.gz file
\x1b[37m DB_Restore \x1b[0m
Restore Ernest DB from Backup. Imports DB from a specific tar.gz file
\x1b[37m man \x1b[0m
Show Ernest command info.
\x1b[37m clear \x1b[0m
Clear Ernest CLI.
\x1b[37m stop \x1b[0m
Stop Ernest an exit Ernest CLI.
`;
_this.outputLine(info);
cli.MainMenu();
};
};
module.exports = Ernest_Commands;
function copyFile(src, dest,callback)
{
if (!fs.existsSync(src))
{
callback(true);
}
else
{
var data = fs.readFileSync(src, 'utf-8');
fs.writeFileSync(dest, data);
if (!fs.existsSync(dest))
{
callback(true);
}
else
{
callback(null);
};
};
};