Merge pull request #123 from sdepold/feature/configurable-home

Load home directory from helper and make it configurable.
This commit is contained in:
Mattias Erming 2014-09-13 18:29:33 +02:00
commit ead12493ff
9 changed files with 53 additions and 25 deletions

View File

@ -106,6 +106,14 @@ Pretty simple, huh?
If you want to edit users manually, see `users/example/user.json`. If you want to edit users manually, see `users/example/user.json`.
## Custom paths
You can define Shout's file storage path via the following options:
- Add an entry called `home` to the `config.json` file.
- Set the environment variable `SHOUT_HOME` before calling the shout executable.
- Use the default of `$HOME/.shout`.
## Commands ## Commands
These are the commands currently implemented: These are the commands currently implemented:

View File

@ -2,8 +2,7 @@ var _ = require("lodash");
var fs = require("fs"); var fs = require("fs");
var Client = require("./client"); var Client = require("./client");
var mkdirp = require("mkdirp"); var mkdirp = require("mkdirp");
var Helper = require("./helper");
const HOME = process.env.HOME + "/.shout";
module.exports = ClientManager; module.exports = ClientManager;
@ -41,7 +40,7 @@ ClientManager.prototype.loadUsers = function(sockets) {
ClientManager.prototype.loadUser = function(name) { ClientManager.prototype.loadUser = function(name) {
try { try {
var json = fs.readFileSync( var json = fs.readFileSync(
HOME + "/users/" + name + "/user.json", Helper.resolveHomePath("users", name, "user.json"),
"utf-8" "utf-8"
); );
json = JSON.parse(json); json = JSON.parse(json);
@ -54,8 +53,10 @@ ClientManager.prototype.loadUser = function(name) {
ClientManager.prototype.getUsers = function() { ClientManager.prototype.getUsers = function() {
var users = []; var users = [];
var path = HOME + "/users/"; var path = Helper.resolveHomePath("users");
mkdirp.sync(path); mkdirp.sync(path);
try { try {
users = fs.readdirSync(path); users = fs.readdirSync(path);
} catch(e) { } catch(e) {
@ -75,7 +76,7 @@ ClientManager.prototype.addUser = function(name, password) {
return false; return false;
} }
try { try {
var path = HOME + "/users/" + name; var path = Helper.resolveHomePath("users", name);
var user = { var user = {
user: name, user: name,
password: password || "", password: password || "",
@ -99,7 +100,7 @@ ClientManager.prototype.removeUser = function(name) {
return false; return false;
} }
try { try {
var path = HOME + "/users/" + name; var path = Helper.resolveHomePath("users", name);
fs.unlinkSync(path + "/user.json"); fs.unlinkSync(path + "/user.json");
fs.rmdirSync(path); fs.rmdirSync(path);
} catch(e) { } catch(e) {

View File

@ -3,15 +3,14 @@ var bcrypt = require("bcrypt");
var fs = require("fs"); var fs = require("fs");
var program = require("commander"); var program = require("commander");
var mkdirp = require("mkdirp"); var mkdirp = require("mkdirp");
var Helper = require("../helper");
const HOME = process.env.HOME + "/.shout";
program program
.command("add <name>") .command("add <name>")
.description("Add a new user") .description("Add a new user")
.action(function(name) { .action(function(name) {
try { try {
var path = HOME + "/users"; var path = Helper.resolveHomePath("users");
mkdirp.sync(path); mkdirp.sync(path);
} catch (e) { } catch (e) {
console.log(""); console.log("");
@ -21,7 +20,7 @@ program
return; return;
} }
try { try {
var path = HOME + "/users"; var path = Helper.resolveHomePath("users");
var test = path + "/.test"; var test = path + "/.test";
fs.mkdirSync(test); fs.mkdirSync(test);
fs.rmdirSync(test); fs.rmdirSync(test);

View File

@ -1,12 +1,11 @@
var ClientManager = new require("../clientManager"); var ClientManager = new require("../clientManager");
var program = require("commander"); var program = require("commander");
var child = require("child_process"); var child = require("child_process");
var Helper = require("../helper");
const HOME = process.env.HOME + "/.shout";
program program
.command("edit <name>") .command("edit <name>")
.description("Edit user: '" + HOME + "/users/<name>/user.json'") .description("Edit user: '" + Helper.resolveHomePath("users", "<name>", "user.json") + "'")
.action(function(name) { .action(function(name) {
var users = new ClientManager().getUsers(); var users = new ClientManager().getUsers();
if (users.indexOf(name) === -1) { if (users.indexOf(name) === -1) {
@ -15,10 +14,9 @@ program
console.log(""); console.log("");
return; return;
} }
var path = HOME + "/users/";
child.spawn( child.spawn(
"vi", "vi",
[path + name + "/user.json"], [Helper.resolveHomePath("users", name, "user.json")],
{stdio: "inherit"} {stdio: "inherit"}
); );
}); });

View File

@ -1,15 +1,14 @@
var ClientManager = new require("../clientManager"); var ClientManager = new require("../clientManager");
var fs = require("fs"); var fs = require("fs");
var program = require("commander"); var program = require("commander");
var Helper = require("../helper");
const HOME = process.env.HOME + "/.shout";
program program
.command("remove <name>") .command("remove <name>")
.description("Remove an existing user") .description("Remove an existing user")
.action(function(name) { .action(function(name) {
try { try {
var path = HOME + "/users"; var path = Helper.resolveHomePath("users");
var test = path + "/.test"; var test = path + "/.test";
fs.mkdirSync(test); fs.mkdirSync(test);
fs.rmdirSync(test); fs.rmdirSync(test);

View File

@ -2,8 +2,7 @@ var bcrypt = require("bcrypt");
var ClientManager = new require("../clientManager"); var ClientManager = new require("../clientManager");
var fs = require("fs"); var fs = require("fs");
var program = require("commander"); var program = require("commander");
var Helper = require("../helper");
const HOME = process.env.HOME + "/.shout";
program program
.command("reset <name>") .command("reset <name>")
@ -16,7 +15,7 @@ program
console.log(""); console.log("");
return; return;
} }
var path = HOME + "/users/"; var path = Helper.resolveHomePath("users");
var file = path + name + "/user.json"; var file = path + name + "/user.json";
var user = require(file); var user = require(file);
require("read")({ require("read")({

22
src/helper.js Normal file
View File

@ -0,0 +1,22 @@
var path = require("path");
var Helper = module.exports = {
getConfig: function () {
return require(path.resolve(__dirname, "..", "config.json"));
},
getHomeDirectory: function () {
return (
this.getConfig().home ||
process.env.SHOUT_HOME ||
path.resolve(process.env.HOME, ".shout")
);
},
resolveHomePath: function () {
var fragments = [ Helper.HOME ].concat([].slice.apply(arguments));
return path.resolve.apply(path, fragments);
}
};
Helper.HOME = Helper.getHomeDirectory()

View File

@ -4,6 +4,7 @@ var config = require("../../../config.json");
var fs = require("fs"); var fs = require("fs");
var mkdirp = require("mkdirp"); var mkdirp = require("mkdirp");
var request = require("superagent"); var request = require("superagent");
var Helper = require("../../helper");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
@ -40,7 +41,7 @@ module.exports = function(irc, network) {
}; };
function fetchImage(url, callback) { function fetchImage(url, callback) {
var path = process.env.HOME + "/.shout/cache/thumbs"; var path = Helper.resolveHomePath("cache", "thumbs");
var name = new Date().getTime().toString(); var name = new Date().getTime().toString();
mkdirp(path, function(e) { mkdirp(path, function(e) {
if (e) { if (e) {

View File

@ -6,6 +6,7 @@ var config = require("../config.json");
var fs = require("fs"); var fs = require("fs");
var http = require("connect"); var http = require("connect");
var io = require("socket.io"); var io = require("socket.io");
var Helper = require("./helper");
var sockets = null; var sockets = null;
var manager = new ClientManager(); var manager = new ClientManager();
@ -18,7 +19,7 @@ module.exports = function(port, host, isPublic) {
var app = http() var app = http()
.use(index) .use(index)
.use(http.static("client")) .use(http.static("client"))
.use(http.static(process.env.HOME + "/.shout/cache")) .use(http.static(Helper.resolveHomePath("cache")))
.listen(config.port, config.host); .listen(config.port, config.host);
sockets = io(app); sockets = io(app);