UNPKG

domotz-remote-pawn

Version:

Domotz Agent

106 lines (94 loc) 4.29 kB
/** * This file is part of Domotz Agent. * Copyright (C) 2016 Domotz Ltd * * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. * * Created by Tommaso Latini <tommaso@domotz.com> on 26/09/2015 */ const DOMOTZ_ENV = process.platform === 'win32' ? require('path').join(process.env.ALLUSERSPROFILE, 'domotz', 'domotz.env') : '/etc/domotz.env'; const ENV_VAR_REGEXP = /\s*export ([\w\.\-]+)\s*=\s*["']?([^'"]*)?["']?\s*/; var fs = require('fs'); function calculateNewEnvironmentValue(fileOverridesEnvironment, newValue, key, oldValue) { if (fileOverridesEnvironment) { if (newValue) { console.verbose('ENV - Variable %s to be replace with value %s', key, newValue); return newValue; } else { console.verbose('ENV - Keeping old %s value %s, a new one was not found', key, oldValue); return oldValue; } } else { if (oldValue) { console.verbose('ENV - Keeping existing old %s value %s', key, oldValue); return oldValue; } else { console.verbose('ENV - Setting value for variable %s=%s', key, newValue); return newValue; } } } module.exports.load = function (envFile, fileOverridesEnvironment) { var env = process.env; // When true, the file contents override the environment values, e.g. when env file is specified // on command-line, its settings should override the current environment. // By default, the environment values, when present, should be kept. fileOverridesEnvironment = fileOverridesEnvironment || false; // Configure the environment [For OpenWRT] // N.B. It's ok also for the other architectures var lines = []; try { var content = fs.readFileSync(envFile || DOMOTZ_ENV, { encoding: 'utf8' }); lines = content.toString().split('\n'); } catch (error) { console.error('Environment configuration: ' + error.message); console.info(error.message); } console.debug('ENV - Old:\n%s', JSON.stringify(env, null, 2)); lines.forEach(function (line) { var keyValueArr = line.match(ENV_VAR_REGEXP); if (keyValueArr !== null) { var key = keyValueArr[1], value = keyValueArr[2]; console.silly('ENV - Process line "%s". Key [%s], Value "%s"', line, key, value); if (value) { // Remove processes if (value.charAt(0) === '`' || value.slice(0, 2) === '$(') { console.verbose("ENV - It's a bash process: support for execution not available... Skip"); return; } // Expand newlines in quoted values var len = value ? value.length : 0; if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"') { value = value.replace(/\\n/gm, '\n'); } var newValue = value.replace(/\${[A-Z_]+}/g, function (oldValue) { return process.env[/\$[{]?([A-Z_]+)[}]?/.exec(oldValue)[1]]; }); env[key] = calculateNewEnvironmentValue(fileOverridesEnvironment, newValue, key, env[key]); } else { console.verbose('ENV - Value not available... Skip'); } } }); console.debug('ENV - New:\n%s', JSON.stringify(env, null, 2)); if (process.env.DPLATFORM === 'raspberry') { //required when executing external scripts that use tput process.env.TERM = process.env.TERM || 'xterm-256color'; } }; //used for unit tests only module.exports.get = function () { return process.env; };