balena-cli/build/actions/app.js

140 lines
5.8 KiB
JavaScript
Raw Normal View History

2015-02-26 15:47:56 +00:00
(function() {
var _, commandOptions, events, helpers, patterns, resin, vcs, visuals;
2015-02-26 15:47:56 +00:00
_ = require('lodash');
2015-02-26 15:47:56 +00:00
resin = require('resin-sdk');
visuals = require('resin-cli-visuals');
commandOptions = require('./command-options');
vcs = require('resin-vcs');
2015-03-09 13:14:39 +00:00
2015-08-31 21:39:48 +00:00
events = require('resin-cli-events');
helpers = require('../utils/helpers');
2015-07-27 12:08:55 +00:00
patterns = require('../utils/patterns');
2015-02-26 15:47:56 +00:00
exports.create = {
signature: 'app create <name>',
description: 'create an application',
help: 'Use this command to create a new resin.io application.\n\nYou can specify the application type with the `--type` option.\nOtherwise, an interactive dropdown will be shown for you to select from.\n\nYou can see a list of supported device types with\n\n $ resin devices supported\n\nExamples:\n\n $ resin app create MyApp\n $ resin app create MyApp --type raspberry-pi',
2015-02-26 15:47:56 +00:00
options: [
{
signature: 'type',
parameter: 'type',
description: 'application type',
alias: 't'
}
],
permission: 'user',
primary: true,
2015-02-26 15:47:56 +00:00
action: function(params, options, done) {
return resin.models.application.has(params.name).then(function(hasApplication) {
if (hasApplication) {
throw new Error('You already have an application with that name!');
2015-02-26 15:47:56 +00:00
}
}).then(patterns.selectDeviceType).then(function(deviceType) {
return resin.models.application.create(params.name, deviceType);
}).then(function(application) {
2015-08-31 21:39:48 +00:00
console.info("Application created: " + application.app_name + " (" + application.device_type + ", id " + application.id + ")");
return events.send('application.create', {
application: application.id
});
}).nodeify(done);
2015-02-26 15:47:56 +00:00
}
};
exports.list = {
signature: 'apps',
description: 'list all applications',
help: 'Use this command to list all your applications.\n\nNotice this command only shows the most important bits of information for each app.\nIf you want detailed information, use resin app <name> instead.\n\nExamples:\n\n $ resin apps',
2015-02-26 15:47:56 +00:00
permission: 'user',
primary: true,
2015-02-26 15:47:56 +00:00
action: function(params, options, done) {
return resin.models.application.getAll().then(function(applications) {
return console.log(visuals.table.horizontal(applications, ['id', 'app_name', 'device_type', 'online_devices', 'devices_length']));
}).nodeify(done);
2015-02-26 15:47:56 +00:00
}
};
exports.info = {
signature: 'app <name>',
2015-02-26 15:47:56 +00:00
description: 'list a single application',
help: 'Use this command to show detailed information for a single application.\n\nExamples:\n\n $ resin app MyApp',
2015-02-26 15:47:56 +00:00
permission: 'user',
primary: true,
2015-02-26 15:47:56 +00:00
action: function(params, options, done) {
return resin.models.application.get(params.name).then(function(application) {
2015-08-31 21:39:48 +00:00
console.log(visuals.table.vertical(application, ["$" + application.app_name + "$", 'id', 'device_type', 'git_repository', 'commit']));
return events.send('application.open', {
application: application.id
});
}).nodeify(done);
2015-02-26 15:47:56 +00:00
}
};
exports.restart = {
signature: 'app restart <name>',
2015-02-26 15:47:56 +00:00
description: 'restart an application',
help: 'Use this command to restart all devices that belongs to a certain application.\n\nExamples:\n\n $ resin app restart MyApp',
2015-02-26 15:47:56 +00:00
permission: 'user',
action: function(params, options, done) {
return resin.models.application.restart(params.name).nodeify(done);
2015-02-26 15:47:56 +00:00
}
};
exports.remove = {
signature: 'app rm <name>',
2015-02-26 15:47:56 +00:00
description: 'remove an application',
help: 'Use this command to remove a resin.io application.\n\nNotice this command asks for confirmation interactively.\nYou can avoid this by passing the `--yes` boolean option.\n\nExamples:\n\n $ resin app rm MyApp\n $ resin app rm MyApp --yes',
2015-02-26 15:47:56 +00:00
options: [commandOptions.yes],
permission: 'user',
action: function(params, options, done) {
return patterns.confirm(options.yes, 'Are you sure you want to delete the application?').then(function() {
return resin.models.application.remove(params.name);
2015-08-31 21:39:48 +00:00
}).tap(function() {
return resin.models.application.get(params.name).then(function(application) {
return events.send('application.delete', {
application: application.id
});
});
}).nodeify(done);
2015-02-26 15:47:56 +00:00
}
};
2015-03-09 13:14:39 +00:00
exports.associate = {
signature: 'app associate <name>',
2015-03-09 13:14:39 +00:00
description: 'associate a resin project',
2015-08-12 12:17:46 +00:00
help: 'Use this command to associate a project directory with a resin application.\n\nThis command adds a \'resin\' git remote to the directory and runs git init if necessary.\n\nNotice this command asks for confirmation interactively.\nYou can avoid this by passing the `--yes` boolean option.\n\nExamples:\n\n $ resin app associate MyApp',
options: [commandOptions.yes],
2015-02-26 15:47:56 +00:00
permission: 'user',
action: function(params, options, done) {
var currentDirectory;
currentDirectory = process.cwd();
console.info("Associating " + params.name + " with " + currentDirectory);
return resin.models.application.has(params.name).then(function(hasApplication) {
if (!hasApplication) {
throw new Error("Invalid application: " + params.name);
}
}).then(function() {
var message;
message = "Are you sure you want to associate " + currentDirectory + " with " + params.name + "?";
return patterns.confirm(options.yes, message);
}).then(function() {
return resin.models.application.get(params.name).get('git_repository').then(function(gitRepository) {
return vcs.initialize(currentDirectory).then(function() {
return vcs.associate(currentDirectory, gitRepository);
}).then(function() {
console.info("git repository added: " + gitRepository);
return gitRepository;
});
});
}).nodeify(done);
2015-03-09 13:14:39 +00:00
}
};
2015-02-26 15:47:56 +00:00
}).call(this);