balena-cli/lib/actions/app.coffee

207 lines
5.2 KiB
CoffeeScript
Raw Normal View History

2015-03-09 13:14:39 +00:00
path = require('path')
_ = require('lodash-contrib')
2014-11-21 14:12:03 +00:00
async = require('async')
2015-01-08 12:04:37 +00:00
resin = require('resin-sdk')
visuals = require('resin-cli-visuals')
commandOptions = require('./command-options')
2015-03-10 14:03:49 +00:00
gitwrap = require('gitwrap')
exports.create =
signature: 'app create <name>'
description: 'create an application'
help: '''
Use this command to create a new resin.io application.
You can specify the application type with the `--type` option.
Otherwise, an interactive dropdown will be shown for you to select from.
You can see a list of supported device types with
$ resin devices supported
Examples:
$ resin app create MyApp
$ resin app create MyApp --type raspberry-pi
'''
options: [
{
signature: 'type'
parameter: 'type'
description: 'application type'
alias: 't'
}
]
permission: 'user'
action: (params, options, done) ->
async.waterfall([
(callback) ->
return callback(null, options.type) if options.type?
resin.models.device.getSupportedDeviceTypes (error, deviceTypes) ->
return callback(error) if error?
visuals.widgets.select('Select a type', deviceTypes, callback)
(type, callback) ->
resin.models.application.create(params.name, type, callback)
], done)
exports.list =
signature: 'apps'
description: 'list all applications'
help: '''
Use this command to list all your applications.
Notice this command only shows the most important bits of information for each app.
If you want detailed information, use resin app <id> instead.
Examples:
$ resin apps
'''
permission: 'user'
action: (params, options, done) ->
resin.models.application.getAll (error, applications) ->
return done(error) if error?
console.log visuals.widgets.table.horizontal applications, [
2015-01-22 17:06:02 +00:00
'id'
'app_name'
2015-01-22 17:20:20 +00:00
'device_type'
2015-01-22 17:06:02 +00:00
'online_devices'
'devices_length'
]
return done()
exports.info =
signature: 'app <id>'
description: 'list a single application'
help: '''
Use this command to show detailed information for a single application.
Examples:
$ resin app 91
'''
permission: 'user'
action: (params, options, done) ->
resin.models.application.get params.id, (error, application) ->
return done(error) if error?
console.log visuals.widgets.table.vertical application, [
2015-01-22 17:06:02 +00:00
'id'
'app_name'
2015-01-22 17:20:20 +00:00
'device_type'
2015-01-22 17:06:02 +00:00
'git_repository'
'commit'
]
return done()
exports.restart =
signature: 'app restart <id>'
description: 'restart an application'
help: '''
Use this command to restart all devices that belongs to a certain application.
Examples:
$ resin app restart 91
'''
permission: 'user'
action: (params, options, done) ->
resin.models.application.restart(params.id, done)
exports.remove =
signature: 'app rm <id>'
description: 'remove an application'
help: '''
Use this command to remove a resin.io application.
Notice this command asks for confirmation interactively.
You can avoid this by passing the `--yes` boolean option.
Examples:
$ resin app rm 91
$ resin app rm 91 --yes
'''
options: [ commandOptions.yes ]
permission: 'user'
action: (params, options, done) ->
visuals.patterns.remove 'application', options.yes, (callback) ->
resin.models.application.remove(params.id, callback)
, done
2015-03-09 13:14:39 +00:00
exports.associate =
signature: 'app associate <id>'
description: 'associate a resin project'
help: '''
2015-03-09 13:14:39 +00:00
Use this command to associate a project directory with a resin application.
2015-03-09 13:14:39 +00:00
This command adds a 'resin' git remote to the directory and runs git init if necessary.
Examples:
2015-03-09 13:14:39 +00:00
$ resin app associate 91
$ resin app associate 91 --project my/app/directory
'''
permission: 'user'
action: (params, options, done) ->
2015-03-10 14:03:49 +00:00
git = gitwrap.create(process.cwd())
2015-03-09 13:14:39 +00:00
async.waterfall([
(callback) ->
2015-03-10 14:03:49 +00:00
git.isGitRepository (isGitDirectory) ->
return callback(null, isGitDirectory)
2015-03-09 13:14:39 +00:00
(isGitDirectory, callback) ->
return callback() if isGitDirectory
2015-03-10 14:03:49 +00:00
git.execute('init', _.unary(callback))
(callback) ->
resin.models.application.get(params.id, callback)
(application, callback) ->
2015-03-10 14:03:49 +00:00
git.execute "remote add resin #{application.git_repository}", (error) ->
2015-03-09 13:14:39 +00:00
return callback(error) if error?
console.info("git repository added: #{application.git_repository}")
return callback(null, application.git_repository)
], done)
exports.init =
signature: 'init'
description: 'init an application'
help: '''
Use this command to initialise a directory as a resin application.
This command performs the following steps:
- Create a resin.io application.
- Initialize the current directory as a git repository.
- Add the corresponding git remote to the application.
Examples:
2015-03-09 13:14:39 +00:00
$ resin init
$ resin init --project my/app/directory
'''
permission: 'user'
action: (params, options, done) ->
currentDirectory = process.cwd()
async.waterfall([
(callback) ->
currentDirectoryBasename = path.basename(currentDirectory)
visuals.widgets.ask('What is the name of your application?', currentDirectoryBasename, callback)
(applicationName, callback) ->
exports.create.action(name: applicationName, options, callback)
(applicationId, callback) ->
exports.associate.action(id: applicationId, options, callback)
], done)