lib/cli-controller.js
/*
 * parses a hosts file into an object
 * @param path {String}: the path to the hosts file
 * @return {Object|Boolean}: the parsed hosts object, or error object if any occured
 */
parses a hosts file into an object
@param path {String}: the path to the hosts file
@return {Object|Boolean}: the parsed hosts object, or error object if any occured
parses a hosts file into an object @param path { String } : the path to the hosts file @return { Object|Boolean } : the parsed hosts object or error object if any occured
function parseHosts(path) {
    try {
        var hFile = fs.readFileSync(path, {encoding: 'utf8'});
        var _hosts = JSON.parse(hFile);
        var hosts = {}; 
        for(var host in _hosts) {
            var current = _hosts[host];
            if(typeof current !== "object") {
                throw new Error("INVALID: an entry in hosts file is not an object");
            }
            current.max_procs = current.max_procs || Infinity;
            if(host !== "local") {
                current.port = current.port || 2222;
                current.passwd = current.passwd || "";
            }
            if(typeof current.max_procs !== "number") {
                throw new Error("INVALID: an entry's processes count is not a number");
            }
            if(current.max_procs < 1) {
                throw new Error("INVALID: an entry's processes count is less than one");
            }
            if(host !== "local") {
                if(typeof current.port !== "number") {
                    throw new Error("INVALID: an entry's port number is not a number!");
                }
                if(typeof current.passwd !== "string") {
                    throw new Error("INVALID: an entry's password is not a string");
                }
            }
            if(host === "local") {
                hosts.local = {max_procs: current.max_procs};
            }
            else {
                hosts[host + ":" + current.port] = {
                    max_procs: current.max_procs,
                    password: current.passwd
                }
            }
        }
        return hosts;
    }
    catch(error) {
        return {error: error.message};
    }
}
function parseHosts ( path ) { try { var hFile = fs . readFileSync ( path , { encoding : 'utf8' } ) ; var _hosts = JSON . parse ( hFile ) ; var hosts = { } ; for ( var host in _hosts ) { var current = _hosts [ host ] ; if ( typeof current !== "object" ) { throw new Error ( "INVALID: an entry in hosts file is not an object" ) ; } current . max_procs = current . max_procs || Infinity ; if ( host !== "local" ) { current . port = current . port || 2222 ; current . passwd = current . passwd || "" ; } if ( typeof current . max_procs !== "number" ) { throw new Error ( "INVALID: an entry's processes count is not a number" ) ; } if ( current . max_procs < 1 ) { throw new Error ( "INVALID: an entry's processes count is less than one" ) ; } if ( host !== "local" ) { if ( typeof current . port !== "number" ) { throw new Error ( "INVALID: an entry's port number is not a number!" ) ; } if ( typeof current . passwd !== "string" ) { throw new Error ( "INVALID: an entry's password is not a string" ) ; } } if ( host === "local" ) { hosts . local = { max_procs : current . max_procs } ; } else { hosts [ host + ":" + current . port ] = { max_procs : current . max_procs , password : current . passwd } } } return hosts ; } catch ( error ) { return { error : error . message } ; } }
parses a hosts file into an object
 @param path {String}: the path to the hosts file
 @return {Object|Boolean}: the parsed hosts object, or error object if any occured
parses a hosts file into an object @param path { String } : the path to the hosts file @return { Object|Boolean } : the parsed hosts object or error object if any occured
// the parsed hosts object // iterate over the entries, check for errors, fill in missing data, and parse // fill in data if missing // check for errors in entry's data