Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 2 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Showing with 74 additions and 69 deletions.
  1. +6 −6 lib/system/linux/index.js
  2. +8 −4 lib/system/linux/sudo.js
  3. +42 −41 lib/system/mac/airport.js
  4. +18 −18 lib/system/mac/index.js
View
12 lib/system/linux/index.js
@@ -49,16 +49,16 @@ exports.get_os_version = function(cb) {
};
exports.process_running = function(process_name, callback){
- var cmd = 'ps ax | grep -v grep | grep -q ' + process_name + ' && echo 1';
- exec(cmd, function(err, stdout){
- callback(stdout && stdout.toString().trim() === '1');
- });
+ var cmd = 'ps ax | grep -v grep | grep -q ' + process_name + ' && echo 1';
+ exec(cmd, function(err, stdout){
+ callback(stdout && stdout.toString().trim() === '1');
+ });
};
// restarts NetworkManager so it reconnects
exports.reconnect = function(callback){
- var cmd = "service NetworkManager restart";
- exec(cmd, callback);
+ var cmd = "service NetworkManager restart";
+ exec(cmd, callback);
};
View
12 lib/system/linux/sudo.js
@@ -1,9 +1,9 @@
-var spawn = require('child_process').spawn,
+var fs = require('fs'),
+ spawn = require('child_process').spawn,
sudo_bin = '/usr/bin/sudo',
pass_required = 'a password is required',
sudo_args = ['-n'];
-
module.exports = function(bin, args, cb){
var cmd = [bin].concat(args),
@@ -12,6 +12,10 @@ module.exports = function(bin, args, cb){
err = '',
returned = false;
+ var exists = fs.existsSync(bin);
+ if (!exists)
+ return cb(new Error('Command not found: ' + bin));
+
var sudo_env = process.env;
sudo_env.LANG = 'en'; // to avoid sudo i18n that breaks our out.match()
@@ -25,11 +29,11 @@ module.exports = function(bin, args, cb){
var child = spawn(sudo_bin, all_args, opts);
- child.stdout.on('data', function(data){
+ child.stdout.on('data', function(data) {
out += data.toString();
})
- child.stderr.on('data', function(data){
+ child.stderr.on('data', function(data) {
if (data.toString().match(pass_required))
return done(new Error('No sudo access for ' + bin));
View
83 lib/system/mac/airport.js
@@ -2,61 +2,62 @@ var exec = require('child_process').exec;
exports.reconnect = function(callback){
- var network_service,
- airport_name = '',
- network_setup_cmd = '/usr/sbin/networksetup',
- providers = require('./../../agent/providers');
+ var network_service,
+ airport_name = '',
+ network_setup_cmd = '/usr/sbin/networksetup',
+ providers = require('./../../agent/providers');
- var set_airport_names = function(osx_version){
- network_service = "AirPort";
+ var set_airport_names = function(osx_version){
+ network_service = "AirPort";
- if (parseFloat(osx_version) >= 10.7){
- network_service = 'Wi-Fi';
- airport_name = 'en1';
- } else if(parseFloat(osx_version) > 10.6){
- airport_name = 'AirPort';
- }
+ if (parseFloat(osx_version) >= 10.7){
+ network_service = 'Wi-Fi';
+ airport_name = 'en1';
+ } else if(parseFloat(osx_version) > 10.6){
+ airport_name = 'AirPort';
+ }
- }
+ }
- var toggle_airport = function(direction, cb){
- exec([network_setup_cmd, '-setnetworkserviceenabled', network_service, direction].join(' '), function(err, stdout){
- if (err) return cb(err);
- exec([network_setup_cmd, '-setairportpower', airport_name, direction].join(' '), cb)
- })
- }
+ var toggle_airport = function(direction, cb){
+ exec([network_setup_cmd, '-setnetworkserviceenabled', network_service, direction].join(' '), function(err, stdout){
+ if (err) return cb(err);
+ exec([network_setup_cmd, '-setairportpower', airport_name, direction].join(' '), cb)
+ })
+ }
- var connect_to_access_point = function(ap, callback){
- var cmd = [network_setup_cmd, '-setairportnetwork', airport_name, ap.ssid].join(' ');
- exec(cmd, callback);
- }
+ var connect_to_access_point = function(ap, callback){
+ var cmd = [network_setup_cmd, '-setairportnetwork', airport_name, ap.ssid].join(' ');
+ exec(cmd, callback);
+ }
- require('./').get_os_version(function(err, version){
+ // yes, this is callback spaghetti. FIXME!
+ require('./').get_os_version(function(err, version){
- if (err) return callback(err);
- set_airport_names(version);
+ if (err) return callback(err);
+ set_airport_names(version);
- // logger.debug('Toggling Airport off...');
- toggle_airport('off', function(err){
- if (err) return callback(err);
+ // logger.debug('Toggling Airport off...');
+ toggle_airport('off', function(err){
+ if (err) return callback(err);
- // logger.debug('Toggling Airport back on...');
- toggle_airport('on', function(err){
- if (err) return callback(err);
+ // logger.debug('Toggling Airport back on...');
+ toggle_airport('on', function(err){
+ if (err) return callback(err);
- // logger.debug('Getting list of open Wifi access points...');
- providers.get('open_access_points_list', function(err, list){
+ // logger.debug('Getting list of open Wifi access points...');
+ providers.get('open_access_points_list', function(err, list){
- if (err || !list[0]) return callback(err);
- // logger.debug('Connecting to ' + list[0] + '...');
- connect_to_access_point(list[0], callback);
+ if (err || !list[0]) return callback(err);
+ // logger.debug('Connecting to ' + list[0] + '...');
+ connect_to_access_point(list[0], callback);
- });
+ });
- });
+ });
- });
+ });
- });
+ });
}
View
36 lib/system/mac/index.js
@@ -13,21 +13,21 @@ exports.reconnect = airport.reconnect;
// var get_logged_user_cmd = "stat /dev/console | cut -d' ' -f5";
// var get_logged_user_pid_cmd = "ps ax | grep -v grep | grep loginwindow | awk '{print $1}'"
-//var get_logged_users_cmd = "ps aux | grep -v grep | grep loginwindow | awk '{print $1}'"
+// var get_logged_users_cmd = "ps aux | grep -v grep | grep loginwindow | awk '{print $1}'"
var get_logged_users_cmd = "stat -f%Su /dev/console"
-var get_logged_user_pid = function(){
- var cmd = "ps ax | grep loginwindow.app | head -1 | awk '{print $1}'";
- // var cmd = "ps aux | awk -v Var=" + process.env.LOGGED_USER + " '/loginwindow.app/ && match(Var,$1) { print $2 }'";
+var get_logged_user_pid = function() {
+ var cmd = "ps ax | grep loginwindow.app | head -1 | awk '{print $1}'";
+ // var cmd = "ps aux | awk -v Var=" + process.env.LOGGED_USER + " '/loginwindow.app/ && match(Var,$1) { print $2 }'";
- exec(cmd, function(err, stdout, stderr){
- process.env.LOGGED_USER_PID = stdout.toString().trim();
- // console.log("Logged user PID is " + process.env.LOGGED_USER_PID);
- });
+ exec(cmd, function(err, stdout, stderr){
+ process.env.LOGGED_USER_PID = stdout.toString().trim();
+ // console.log("Logged user PID is " + process.env.LOGGED_USER_PID);
+ });
};
-exports.find_logged_user = function(cb){
- exec(get_logged_users_cmd, function(err, out){
+exports.find_logged_user = function(cb) {
+ exec(get_logged_users_cmd, function(err, out) {
if (err) return cb(err)
var items = out.split('\n');
@@ -48,14 +48,14 @@ exports.get_os_name = function(cb) {
}
exports.get_os_version = function(cb) {
- exec('sw_vers -productVersion', function(err, stdout){
- cb(err, stdout && stdout.toString().trim())
- })
+ exec('sw_vers -productVersion', function(err, stdout) {
+ cb(err, stdout && stdout.toString().trim())
+ })
}
-exports.process_running = function(process_name, callback){
- var cmd = 'ps ax | grep -v grep | grep -q ' + process_name + ' && echo 1';
- exec(cmd, function(err, stdout, stderr){
- callback(stdout && stdout.toString().trim() == '1')
- });
+exports.process_running = function(process_name, callback) {
+ var cmd = 'ps ax | grep -v grep | grep -q ' + process_name + ' && echo 1';
+ exec(cmd, function(err, stdout, stderr) {
+ callback(stdout && stdout.toString().trim() == '1')
+ });
}

No commit comments for this range

Something went wrong with that request. Please try again.