2014-02-07 23:21:24 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
/**
|
2015-08-27 03:21:27 +03:00
|
|
|
* Copyright (c) 2015 Joyent Inc. All rights reserved.
|
2014-02-07 23:21:24 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
var assert = require('assert-plus');
|
2015-08-27 03:21:27 +03:00
|
|
|
var format = require('util').format;
|
2014-02-07 23:21:24 +02:00
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
|
|
|
|
var common = require('./common');
|
2015-07-26 08:45:20 +03:00
|
|
|
var errors = require('./errors');
|
2014-02-07 23:21:24 +02:00
|
|
|
|
|
|
|
|
2015-09-02 20:47:06 +03:00
|
|
|
var DEFAULT_USER_CONFIG_PATH = path.resolve(process.env.HOME,
|
|
|
|
'.triton', 'config.json');
|
2014-02-07 23:21:24 +02:00
|
|
|
var DEFAULTS_PATH = path.resolve(__dirname, '..', 'etc', 'defaults.json');
|
2015-08-25 23:11:40 +03:00
|
|
|
var OVERRIDE_KEYS = []; // config object keys to do a one-level deep override
|
2014-02-07 23:21:24 +02:00
|
|
|
|
2015-08-27 03:21:27 +03:00
|
|
|
|
|
|
|
|
2015-07-26 08:45:20 +03:00
|
|
|
/**
|
2015-08-25 23:11:40 +03:00
|
|
|
* Load the Triton client config. This is a merge of the built-in "defaults" (at
|
|
|
|
* etc/defaults.json) and the "user" config (at ~/.triton/config.json if it
|
2015-07-26 08:45:20 +03:00
|
|
|
* exists).
|
|
|
|
*
|
|
|
|
* This includes some internal data on keys with a leading underscore.
|
|
|
|
*/
|
2015-08-27 03:21:27 +03:00
|
|
|
function loadConfigSync(opts) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.configPath, 'opts.configPath');
|
|
|
|
assert.optionalObject(opts.envProfile, 'opts.envProfile');
|
|
|
|
|
2015-07-26 08:45:20 +03:00
|
|
|
var c = fs.readFileSync(DEFAULTS_PATH, 'utf8');
|
|
|
|
var _defaults = JSON.parse(c);
|
|
|
|
var config = JSON.parse(c);
|
2015-08-27 03:21:27 +03:00
|
|
|
if (opts.configPath && fs.existsSync(opts.configPath)) {
|
|
|
|
c = fs.readFileSync(opts.configPath, 'utf8');
|
2015-07-26 08:45:20 +03:00
|
|
|
var _user = JSON.parse(c);
|
|
|
|
var userConfig = JSON.parse(c);
|
2015-09-01 22:03:52 +03:00
|
|
|
if (typeof (userConfig) !== 'object' || Array.isArray(userConfig)) {
|
2015-07-26 08:45:20 +03:00
|
|
|
throw new errors.ConfigError(
|
2015-08-27 03:21:27 +03:00
|
|
|
format('"%s" is not an object', opts.configPath));
|
2015-07-26 08:45:20 +03:00
|
|
|
}
|
|
|
|
// These special keys are merged into the key of the same name in the
|
|
|
|
// base "defaults.json".
|
|
|
|
Object.keys(userConfig).forEach(function (key) {
|
|
|
|
if (~OVERRIDE_KEYS.indexOf(key) && config[key] !== undefined) {
|
|
|
|
Object.keys(userConfig[key]).forEach(function (subKey) {
|
|
|
|
if (userConfig[key][subKey] === null) {
|
|
|
|
delete config[key][subKey];
|
|
|
|
} else {
|
|
|
|
config[key][subKey] = userConfig[key][subKey];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
config[key] = userConfig[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
config._user = _user;
|
2014-02-07 23:21:24 +02:00
|
|
|
}
|
2015-07-26 08:45:20 +03:00
|
|
|
config._defaults = _defaults;
|
2014-02-07 23:21:24 +02:00
|
|
|
|
2015-08-27 03:21:27 +03:00
|
|
|
// Add 'env' profile, if given.
|
|
|
|
if (opts.envProfile) {
|
|
|
|
if (!config.profiles) {
|
|
|
|
config.profiles = [];
|
|
|
|
}
|
|
|
|
config.profiles.push(opts.envProfile);
|
2014-02-07 23:21:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---- exports
|
|
|
|
|
|
|
|
module.exports = {
|
2015-08-26 19:59:12 +03:00
|
|
|
DEFAULT_USER_CONFIG_PATH: DEFAULT_USER_CONFIG_PATH,
|
2015-08-25 23:11:40 +03:00
|
|
|
loadConfigSync: loadConfigSync
|
2014-02-07 23:21:24 +02:00
|
|
|
};
|
|
|
|
// vim: set softtabstop=4 shiftwidth=4:
|