aboutsummaryrefslogblamecommitdiffhomepage
path: root/cli/config.js
blob: 68eae5f1a5a503aa3df6e2e28eb4f07e4edc97f2 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
















                                  


                                                      











































                                                                               
/* jshint node:true */

'use strict';

var fs = require('fs'),
    path = require('path'),
    safe = require('safetydance'),
    _ = require('underscore');

exports = module.exports = {
    clear: clear,
    set: set,
    get: get,
    unset: unset,
    has: has,

    // convenience
    server: function () { return get('server'); },
    username: function () { return get('username'); },
    password: function () { return get('password'); }
};

var HOME = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
var CONFIG_FILE_PATH = path.join(HOME, '.surfer.json');

var gConfig = (function () {
    return safe.JSON.parse(safe.fs.readFileSync(CONFIG_FILE_PATH)) || {};
})();

function save() {
    fs.writeFileSync(CONFIG_FILE_PATH, JSON.stringify(gConfig, null, 4));
}

function clear() {
    safe.fs.unlinkSync(CONFIG_FILE_PATH);
}

function set(key, value) {
    if (typeof key === 'object') {
        _.extend(gConfig, key);
    } else {
        safe.set(gConfig, key, value);
    }
    save();
}

function get(key) {
    return safe.query(gConfig, key);
}

function unset(key /*, .... */) {
    for (var i = 0; i < arguments.length; i++) {
        gConfig = safe.unset(gConfig, arguments[i]);
    }

    save();
}

function has(key /*, ... */) {
    for (var i = 0; i < arguments.length; i++) {
        if (!(arguments[i] in gConfig)) return false;
    }
    return true;
}