2015-03-09 13:14:39 +00:00
|
|
|
path = require('path')
|
2015-01-09 15:22:18 +00:00
|
|
|
_ = 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')
|
2015-01-21 13:50:19 +00:00
|
|
|
visuals = require('resin-cli-visuals')
|
2015-01-15 17:10:14 +00:00
|
|
|
commandOptions = require('./command-options')
|
2015-03-12 15:24:36 +00:00
|
|
|
vcs = require('resin-vcs')
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
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
|
2015-03-03 14:14:16 +00:00
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
$ resin devices supported
|
|
|
|
|
|
|
|
Examples:
|
2015-03-03 14:14:16 +00:00
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
$ resin app create MyApp
|
|
|
|
$ resin app create MyApp --type raspberry-pi
|
|
|
|
'''
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
signature: 'type'
|
|
|
|
parameter: 'type'
|
|
|
|
description: 'application type'
|
|
|
|
alias: 't'
|
|
|
|
}
|
|
|
|
]
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-01-15 17:10:14 +00:00
|
|
|
async.waterfall([
|
|
|
|
|
|
|
|
(callback) ->
|
2015-04-28 13:18:21 +00:00
|
|
|
resin.models.application.has(params.name, callback)
|
|
|
|
|
|
|
|
(hasApplication, callback) ->
|
|
|
|
if hasApplication
|
|
|
|
return callback(new Error('You already have an application with that name!'))
|
|
|
|
|
2015-02-03 18:53:48 +00:00
|
|
|
return callback(null, options.type) if options.type?
|
2015-02-11 14:07:53 +00:00
|
|
|
|
|
|
|
resin.models.device.getSupportedDeviceTypes (error, deviceTypes) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
visuals.widgets.select('Select a type', deviceTypes, callback)
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
(type, callback) ->
|
2015-06-02 17:04:08 +00:00
|
|
|
options.type = type
|
|
|
|
resin.models.application.create(params.name, options.type, callback)
|
|
|
|
|
|
|
|
(applicationId, callback) ->
|
|
|
|
console.info("Application created: #{params.name} (#{options.type}, id #{applicationId})")
|
|
|
|
return callback()
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
], 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.
|
2015-03-23 12:17:55 +00:00
|
|
|
If you want detailed information, use resin app <name> instead.
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
Examples:
|
2015-03-03 14:14:16 +00:00
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
$ resin apps
|
|
|
|
'''
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-01-15 17:10:14 +00:00
|
|
|
resin.models.application.getAll (error, applications) ->
|
|
|
|
return done(error) if error?
|
2015-01-21 13:50:19 +00:00
|
|
|
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'
|
2015-01-15 17:10:14 +00:00
|
|
|
]
|
|
|
|
return done()
|
|
|
|
|
|
|
|
exports.info =
|
2015-03-23 12:17:55 +00:00
|
|
|
signature: 'app <name>'
|
2015-01-15 17:10:14 +00:00
|
|
|
description: 'list a single application'
|
|
|
|
help: '''
|
|
|
|
Use this command to show detailed information for a single application.
|
|
|
|
|
|
|
|
Examples:
|
2015-03-03 14:14:16 +00:00
|
|
|
|
2015-03-23 12:17:55 +00:00
|
|
|
$ resin app MyApp
|
2015-01-15 17:10:14 +00:00
|
|
|
'''
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-03-23 12:17:55 +00:00
|
|
|
resin.models.application.get params.name, (error, application) ->
|
2015-01-15 17:10:14 +00:00
|
|
|
return done(error) if error?
|
2015-01-21 13:50:19 +00:00
|
|
|
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'
|
2015-01-15 17:10:14 +00:00
|
|
|
]
|
|
|
|
return done()
|
|
|
|
|
|
|
|
exports.restart =
|
2015-03-23 12:17:55 +00:00
|
|
|
signature: 'app restart <name>'
|
2015-01-15 17:10:14 +00:00
|
|
|
description: 'restart an application'
|
|
|
|
help: '''
|
|
|
|
Use this command to restart all devices that belongs to a certain application.
|
|
|
|
|
|
|
|
Examples:
|
2015-03-03 14:14:16 +00:00
|
|
|
|
2015-03-23 12:17:55 +00:00
|
|
|
$ resin app restart MyApp
|
2015-01-15 17:10:14 +00:00
|
|
|
'''
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-03-23 12:17:55 +00:00
|
|
|
resin.models.application.restart(params.name, done)
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
exports.remove =
|
2015-03-23 12:17:55 +00:00
|
|
|
signature: 'app rm <name>'
|
2015-01-15 17:10:14 +00:00
|
|
|
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:
|
2015-03-03 14:14:16 +00:00
|
|
|
|
2015-03-23 12:17:55 +00:00
|
|
|
$ resin app rm MyApp
|
|
|
|
$ resin app rm MyApp --yes
|
2015-01-15 17:10:14 +00:00
|
|
|
'''
|
|
|
|
options: [ commandOptions.yes ]
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-01-21 13:50:19 +00:00
|
|
|
visuals.patterns.remove 'application', options.yes, (callback) ->
|
2015-03-23 12:17:55 +00:00
|
|
|
resin.models.application.remove(params.name, callback)
|
2015-01-15 17:10:14 +00:00
|
|
|
, done
|
|
|
|
|
2015-03-09 13:14:39 +00:00
|
|
|
exports.associate =
|
2015-03-23 12:17:55 +00:00
|
|
|
signature: 'app associate <name>'
|
2015-03-09 13:14:39 +00:00
|
|
|
description: 'associate a resin project'
|
2015-01-15 17:10:14 +00:00
|
|
|
help: '''
|
2015-03-09 13:14:39 +00:00
|
|
|
Use this command to associate a project directory with a resin application.
|
2015-01-15 17:10:14 +00:00
|
|
|
|
2015-03-09 13:14:39 +00:00
|
|
|
This command adds a 'resin' git remote to the directory and runs git init if necessary.
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
Examples:
|
2015-03-03 14:14:16 +00:00
|
|
|
|
2015-03-23 12:17:55 +00:00
|
|
|
$ resin app associate MyApp
|
|
|
|
$ resin app associate MyApp --project my/app/directory
|
2015-01-15 17:10:14 +00:00
|
|
|
'''
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-03-12 15:24:36 +00:00
|
|
|
currentDirectory = process.cwd()
|
2015-01-15 17:10:14 +00:00
|
|
|
|
2015-03-12 15:24:36 +00:00
|
|
|
async.waterfall [
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
(callback) ->
|
2015-03-12 15:24:36 +00:00
|
|
|
vcs.initialize(currentDirectory, callback)
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
(callback) ->
|
2015-03-23 12:17:55 +00:00
|
|
|
resin.models.application.get(params.name, callback)
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
(application, callback) ->
|
2015-03-12 15:24:36 +00:00
|
|
|
vcs.addRemote(currentDirectory, application.git_repository, callback)
|
2015-03-09 13:14:39 +00:00
|
|
|
|
2015-03-12 15:24:36 +00:00
|
|
|
], (error, remoteUrl) ->
|
|
|
|
return done(error) if error?
|
|
|
|
console.info("git repository added: #{remoteUrl}")
|
|
|
|
return done(null, remoteUrl)
|
2015-03-09 13:14:39 +00:00
|
|
|
|
|
|
|
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-01-15 17:10:14 +00:00
|
|
|
|
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) ->
|
|
|
|
|
2015-03-23 12:17:55 +00:00
|
|
|
# TODO: Make resin.models.application.create return
|
|
|
|
# the whole application instead of just the id
|
|
|
|
exports.create.action name: applicationName, options, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
return callback(null, applicationName)
|
|
|
|
|
|
|
|
(applicationName, callback) ->
|
|
|
|
exports.associate.action(name: applicationName, options, callback)
|
2015-03-09 13:14:39 +00:00
|
|
|
|
|
|
|
], done)
|