balena-cli/build/utils/helpers.js
Juan Cruz Viotti 445e37ccaf Call os initialize as an elevated process
Currently, the fact that `os initialize` requires elevated permissions
forced us to require calling commands that reuse it, such as `device
init` and `quickstart` with administrator permissions as well.

This ended up causing issues like saving images in the cache that belong
to root, or initializing git repositories that requires `sudo` to
commit.

The solution is to call `os initialize` as a child process preppending
`sudo` within `device init`.

Fixes: https://github.com/resin-io/resin-cli/issues/109
2015-10-01 13:07:53 -04:00

64 lines
1.7 KiB
JavaScript

(function() {
var Promise, _, capitano, chalk, child_process, os;
Promise = require('bluebird');
capitano = Promise.promisifyAll(require('capitano'));
_ = require('lodash');
_.str = require('underscore.string');
child_process = require('child_process');
os = require('os');
chalk = require('chalk');
exports.getOperatingSystem = function() {
var platform;
platform = os.platform();
if (platform === 'darwin') {
platform = 'osx';
}
return platform;
};
exports.stateToString = function(state) {
var percentage, result;
percentage = _.str.lpad(state.percentage, 3, '0') + '%';
result = (chalk.blue(percentage)) + " " + (chalk.cyan(state.operation.command));
switch (state.operation.command) {
case 'copy':
return result + " " + state.operation.from.path + " -> " + state.operation.to.path;
case 'replace':
return result + " " + state.operation.file.path + ", " + state.operation.copy + " -> " + state.operation.replace;
case 'run-script':
return result + " " + state.operation.script;
default:
throw new Error("Unsupported operation: " + state.operation.type);
}
};
exports.waitStream = function(stream) {
return new Promise(function(resolve, reject) {
stream.on('close', resolve);
stream.on('end', resolve);
return stream.on('error', reject);
});
};
exports.sudo = function(command) {
var spawn;
if (os.platform() === 'win32') {
return capitano.runAsync(command.join(' '));
}
command = _.union(_.take(process.argv, 2), command);
spawn = child_process.spawn('sudo', command, {
stdio: 'inherit'
});
return exports.waitStream(spawn);
};
}).call(this);