2017-06-08 10:52:47 +00:00
|
|
|
// Generated by CoffeeScript 1.12.6
|
2016-01-11 19:58:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Copyright 2016 Resin.io
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2017-03-22 09:46:06 +00:00
|
|
|
var Promise, _, chalk, form, messages, resin, validation, visuals,
|
|
|
|
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
|
2016-01-11 19:58:35 +00:00
|
|
|
|
2017-03-22 09:46:06 +00:00
|
|
|
_ = require('lodash');
|
2015-08-20 19:54:42 +00:00
|
|
|
|
2017-03-22 09:46:06 +00:00
|
|
|
Promise = require('bluebird');
|
2015-08-20 19:54:42 +00:00
|
|
|
|
2017-03-22 09:46:06 +00:00
|
|
|
form = require('resin-cli-form');
|
2015-08-20 19:54:42 +00:00
|
|
|
|
2017-03-22 09:46:06 +00:00
|
|
|
visuals = require('resin-cli-visuals');
|
2015-08-20 19:54:42 +00:00
|
|
|
|
2017-03-22 09:46:06 +00:00
|
|
|
resin = require('resin-sdk-preconfigured');
|
2015-08-20 19:54:42 +00:00
|
|
|
|
2017-03-22 09:46:06 +00:00
|
|
|
chalk = require('chalk');
|
2015-08-20 19:54:42 +00:00
|
|
|
|
2017-03-22 09:46:06 +00:00
|
|
|
validation = require('./validation');
|
2015-10-06 22:51:17 +00:00
|
|
|
|
2017-03-22 09:46:06 +00:00
|
|
|
messages = require('./messages');
|
2015-08-20 19:54:42 +00:00
|
|
|
|
2017-03-22 09:46:06 +00:00
|
|
|
exports.authenticate = function(options) {
|
|
|
|
return form.run([
|
|
|
|
{
|
|
|
|
message: 'Email:',
|
|
|
|
name: 'email',
|
|
|
|
type: 'input',
|
|
|
|
validate: validation.validateEmail
|
|
|
|
}, {
|
|
|
|
message: 'Password:',
|
|
|
|
name: 'password',
|
|
|
|
type: 'password'
|
|
|
|
}
|
|
|
|
], {
|
|
|
|
override: options
|
|
|
|
}).then(resin.auth.login).then(resin.auth.twoFactor.isPassed).then(function(isTwoFactorAuthPassed) {
|
|
|
|
if (isTwoFactorAuthPassed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return form.ask({
|
|
|
|
message: 'Two factor auth challenge:',
|
|
|
|
name: 'code',
|
|
|
|
type: 'input'
|
|
|
|
}).then(resin.auth.twoFactor.challenge)["catch"](function(error) {
|
|
|
|
return resin.auth.logout().then(function() {
|
|
|
|
if (error.name === 'ResinRequestError' && error.statusCode === 401) {
|
|
|
|
throw new Error('Invalid two factor authentication code');
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.askLoginType = function() {
|
|
|
|
return form.ask({
|
|
|
|
message: 'How would you like to login?',
|
|
|
|
name: 'loginType',
|
|
|
|
type: 'list',
|
|
|
|
choices: [
|
2016-01-12 13:07:15 +00:00
|
|
|
{
|
2017-03-22 09:46:06 +00:00
|
|
|
name: 'Web authorization (recommended)',
|
|
|
|
value: 'web'
|
2016-01-12 13:07:15 +00:00
|
|
|
}, {
|
2017-03-22 09:46:06 +00:00
|
|
|
name: 'Credentials',
|
|
|
|
value: 'credentials'
|
|
|
|
}, {
|
|
|
|
name: 'Authentication token',
|
|
|
|
value: 'token'
|
|
|
|
}, {
|
|
|
|
name: 'I don\'t have a Resin account!',
|
|
|
|
value: 'register'
|
2016-01-12 13:07:15 +00:00
|
|
|
}
|
2017-03-22 09:46:06 +00:00
|
|
|
]
|
|
|
|
});
|
|
|
|
};
|
2016-01-12 13:07:15 +00:00
|
|
|
|
2017-03-22 09:46:06 +00:00
|
|
|
exports.selectDeviceType = function() {
|
|
|
|
return resin.models.device.getSupportedDeviceTypes().then(function(deviceTypes) {
|
2016-01-12 13:07:15 +00:00
|
|
|
return form.ask({
|
2017-03-22 09:46:06 +00:00
|
|
|
message: 'Device Type',
|
2016-01-12 13:07:15 +00:00
|
|
|
type: 'list',
|
2017-03-22 09:46:06 +00:00
|
|
|
choices: deviceTypes
|
2016-01-12 13:07:15 +00:00
|
|
|
});
|
2017-03-22 09:46:06 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-06-09 09:32:28 +00:00
|
|
|
exports.confirm = function(yesOption, message, yesMessage) {
|
2017-03-22 09:46:06 +00:00
|
|
|
return Promise["try"](function() {
|
|
|
|
if (yesOption) {
|
2017-06-09 09:32:28 +00:00
|
|
|
if (yesMessage) {
|
|
|
|
console.log(yesMessage);
|
|
|
|
}
|
2017-03-22 09:46:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return form.ask({
|
|
|
|
message: message,
|
|
|
|
type: 'confirm',
|
|
|
|
"default": false
|
2015-08-20 19:54:42 +00:00
|
|
|
});
|
2017-03-22 09:46:06 +00:00
|
|
|
}).then(function(confirmed) {
|
|
|
|
if (!confirmed) {
|
|
|
|
throw new Error('Aborted');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.selectApplication = function(filter) {
|
|
|
|
return resin.models.application.hasAny().then(function(hasAnyApplications) {
|
|
|
|
if (!hasAnyApplications) {
|
|
|
|
throw new Error('You don\'t have any applications');
|
|
|
|
}
|
|
|
|
return resin.models.application.getAll();
|
|
|
|
}).filter(filter || _.constant(true)).then(function(applications) {
|
|
|
|
return form.ask({
|
|
|
|
message: 'Select an application',
|
|
|
|
type: 'list',
|
|
|
|
choices: _.map(applications, function(application) {
|
|
|
|
return {
|
|
|
|
name: application.app_name + " (" + application.device_type + ")",
|
|
|
|
value: application.app_name
|
|
|
|
};
|
|
|
|
})
|
2015-08-20 19:54:42 +00:00
|
|
|
});
|
2017-03-22 09:46:06 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.selectOrCreateApplication = function() {
|
|
|
|
return resin.models.application.hasAny().then(function(hasAnyApplications) {
|
|
|
|
if (!hasAnyApplications) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return resin.models.application.getAll().then(function(applications) {
|
|
|
|
applications = _.map(applications, function(application) {
|
|
|
|
return {
|
|
|
|
name: application.app_name + " (" + application.device_type + ")",
|
|
|
|
value: application.app_name
|
|
|
|
};
|
2015-09-29 15:10:33 +00:00
|
|
|
});
|
2017-03-22 09:46:06 +00:00
|
|
|
applications.unshift({
|
|
|
|
name: 'Create a new application',
|
|
|
|
value: null
|
2015-08-20 19:54:42 +00:00
|
|
|
});
|
|
|
|
return form.ask({
|
2017-03-22 09:46:06 +00:00
|
|
|
message: 'Select an application',
|
|
|
|
type: 'list',
|
|
|
|
choices: applications
|
2015-08-20 19:54:42 +00:00
|
|
|
});
|
|
|
|
});
|
2017-03-22 09:46:06 +00:00
|
|
|
}).then(function(application) {
|
|
|
|
if (application != null) {
|
|
|
|
return application;
|
|
|
|
}
|
|
|
|
return form.ask({
|
|
|
|
message: 'Choose a Name for your new application',
|
|
|
|
type: 'input',
|
|
|
|
validate: validation.validateApplicationName
|
2015-08-20 19:54:42 +00:00
|
|
|
});
|
2017-03-22 09:46:06 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.awaitDevice = function(uuid) {
|
|
|
|
return resin.models.device.getName(uuid).then(function(deviceName) {
|
|
|
|
var poll, spinner;
|
|
|
|
spinner = new visuals.Spinner("Waiting for " + deviceName + " to come online");
|
|
|
|
poll = function() {
|
|
|
|
return resin.models.device.isOnline(uuid).then(function(isOnline) {
|
|
|
|
if (isOnline) {
|
|
|
|
spinner.stop();
|
|
|
|
console.info("The device **" + deviceName + "** is online!");
|
|
|
|
} else {
|
|
|
|
spinner.start();
|
|
|
|
return Promise.delay(3000).then(poll);
|
|
|
|
}
|
2016-04-25 12:32:58 +00:00
|
|
|
});
|
2017-03-22 09:46:06 +00:00
|
|
|
};
|
|
|
|
console.info("Waiting for " + deviceName + " to connect to resin...");
|
|
|
|
return poll()["return"](uuid);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.inferOrSelectDevice = function(preferredUuid) {
|
|
|
|
return resin.models.device.getAll().filter(function(device) {
|
|
|
|
return device.is_online;
|
|
|
|
}).then(function(onlineDevices) {
|
|
|
|
if (_.isEmpty(onlineDevices)) {
|
|
|
|
throw new Error('You don\'t have any devices online');
|
|
|
|
}
|
|
|
|
return form.ask({
|
|
|
|
message: 'Select a device',
|
|
|
|
type: 'list',
|
|
|
|
"default": indexOf.call(_.map(onlineDevices, 'uuid'), preferredUuid) >= 0 ? preferredUuid : onlineDevices[0].uuid,
|
|
|
|
choices: _.map(onlineDevices, function(device) {
|
|
|
|
return {
|
|
|
|
name: (device.name || 'Untitled') + " (" + (device.uuid.slice(0, 7)) + ")",
|
|
|
|
value: device.uuid
|
|
|
|
};
|
|
|
|
})
|
2016-04-25 12:32:58 +00:00
|
|
|
});
|
2017-03-22 09:46:06 +00:00
|
|
|
});
|
|
|
|
};
|
2015-10-06 22:51:17 +00:00
|
|
|
|
2017-03-22 09:46:06 +00:00
|
|
|
exports.printErrorMessage = function(message) {
|
|
|
|
console.error(chalk.red(message));
|
|
|
|
return console.error(chalk.red("\n" + messages.getHelp + "\n"));
|
|
|
|
};
|