var redis      = require("redis"),
    client     = redis.createClient(),
    listname   = process.argv[2];
    itemsToAdd = process.argv.splice(3),
    validLists = ["watchlist", "emaillist", "slackloglist", "slackalertlist"];

client.on("error", function (err) {
	console.log("Error " + err);
});

// taken from http://stackoverflow.com/questions/784012/javascript-equivalent-of-phps-in-array on 20150518 @ 19:50 EST
function inArray (needle, haystack) {
	var length = haystack.length;
	for(var i = 0; i < length; i++) {
		if(haystack[i] == needle) return true;
	}
	return false;
};

function addToList (listname, rawArgs, verbose) {
	rawArgs.forEach(function (val, index, array) {
		// skip the first two element ['node', 'add-to-list']
		if (index >= 2)
			itemsToAdd.push (val);
	});

	client.get (listname, function (err, reply) {
		if (reply != null) {
			var hasChanged = false;
			list = JSON.parse(reply.toString());

			itemsToAdd.forEach (function (val, index, array) {
				if (!inArray (val, list)) {
					if (verbose) console.log ("pushing " + val + " to array");

					list.push (val);
					hasChanged = true;
				} else {
					if (verbose) console.log ("'" + val + "' already in " + listname);
				}
			});

			if (hasChanged) {
				if (verbose) console.log ("pushing " + listname + " back to redis");
				client.set (listname, JSON.stringify (list), function (err, reply) {
					if (err != null) console.log ("ERROR:" + err);
					client.quit();
				});
			} else {
				client.quit();
			}
		} else {
			if (verbose) console.log ("adding first item(s) " + itemsToAdd + " to " + listname);
			client.set (listname, JSON.stringify (itemsToAdd), function (err, reply) {
				client.quit();
			});
		}
	});
};

if (inArray (listname, validLists)) {
	addToList (listname, itemsToAdd, true);
} else {
	console.log ("Error: '" + listname + "' is not a valid list name please use " + validLists);
	client.quit();
}
var redis      = require("redis"),
    client     = redis.createClient(),
    listname   = process.argv[2];
    itemsToAdd = process.argv.splice(3),
    validLists = ["watchlist", "emaillist", "slackloglist", "slackalertlist"];

client.on("error", function (err) {
	console.log("Error " + err);
});

// TODO: add/remove both use this same function, need to abstract out
// taken from http://stackoverflow.com/questions/784012/javascript-equivalent-of-phps-in-array on 20150518 @ 19:50 EST
function inArray (needle, haystack) {
	var length = haystack.length;
	for(var i = 0; i < length; i++) {
		if(haystack[i] == needle) return true;
	}
	return false;
};

function addToList (listname, rawArgs, verbose) {
	rawArgs.forEach(function (val, index, array) {
		// skip the first two element ['node', 'add-to-list']
		if (index >= 2)
			itemsToAdd.push (val);
	});

	client.get (listname, function (err, reply) {
		if (reply != null) {
			var hasChanged = false;
			list = JSON.parse(reply.toString());

			itemsToAdd.forEach (function (val, index, array) {
				if (!inArray (val, list)) {
					if (verbose) console.log ("pushing '" + val + "' to " + listname);

					list.push (val);
					hasChanged = true;
				} else {
					if (verbose) console.log ("'" + val + "' already in " + listname);
				}
			});

			if (hasChanged) {
				if (verbose) console.log ("pushing " + listname + " back to redis");
				client.set (listname, JSON.stringify (list), function (err, reply) {
					if (err != null) console.log ("ERROR:" + err);
					client.quit();
				});
			} else {
				client.quit();
			}
		} else {
			if (verbose) console.log ("adding first item(s) " + itemsToAdd + " to " + listname);
			client.set (listname, JSON.stringify (itemsToAdd), function (err, reply) {
				client.quit();
			});
		}
	});
};

if (inArray (listname, validLists)) {
	addToList (listname, itemsToAdd, true);
} else {
	console.log ("Error: '" + listname + "' is not a valid list name please use " + validLists);
	client.quit();
}
add comment about common function in 2 files, update copy