mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-25 00:11:04 +00:00
e08c3752f9
Some CLI commans prompt to select an existing application, presending a dropdown with all the application names, however it's hard to remember which application belon to which device type, which makes it easier to select the wrong application.
92 lines
2.5 KiB
CoffeeScript
92 lines
2.5 KiB
CoffeeScript
_ = require('lodash')
|
|
Promise = require('bluebird')
|
|
form = require('resin-cli-form')
|
|
visuals = require('resin-cli-visuals')
|
|
resin = require('resin-sdk')
|
|
chalk = require('chalk')
|
|
validation = require('./validation')
|
|
|
|
exports.selectDeviceType = ->
|
|
resin.models.device.getSupportedDeviceTypes().then (deviceTypes) ->
|
|
return form.ask
|
|
message: 'Device Type'
|
|
type: 'list'
|
|
choices: deviceTypes
|
|
|
|
exports.confirm = (yesOption, message) ->
|
|
Promise.try ->
|
|
return true if yesOption
|
|
return form.ask
|
|
message: message
|
|
type: 'confirm'
|
|
default: false
|
|
.then (confirmed) ->
|
|
if not confirmed
|
|
throw new Error('Aborted')
|
|
|
|
exports.selectApplication = (filter) ->
|
|
resin.models.application.hasAny().then (hasAnyApplications) ->
|
|
if not hasAnyApplications
|
|
throw new Error('You don\'t have any applications')
|
|
|
|
return resin.models.application.getAll()
|
|
.filter(filter or _.constant(true))
|
|
.then (applications) ->
|
|
return form.ask
|
|
message: 'Select an application'
|
|
type: 'list'
|
|
choices: _.map applications, (application) ->
|
|
return {
|
|
name: "#{application.app_name} (#{application.device_type})"
|
|
value: application.app_name
|
|
}
|
|
|
|
exports.selectOrCreateApplication = ->
|
|
resin.models.application.hasAny().then (hasAnyApplications) ->
|
|
return if not hasAnyApplications
|
|
resin.models.application.getAll().then (applications) ->
|
|
applications = _.map applications, (application) ->
|
|
return {
|
|
name: "#{application.app_name} (#{application.device_type})"
|
|
value: application.app_name
|
|
}
|
|
|
|
applications.unshift
|
|
name: 'Create a new application'
|
|
value: null
|
|
|
|
return form.ask
|
|
message: 'Select an application'
|
|
type: 'list'
|
|
choices: applications
|
|
.then (application) ->
|
|
return application if application?
|
|
form.ask
|
|
message: 'Choose a Name for your new application'
|
|
type: 'input'
|
|
validate: validation.validateApplicationName
|
|
|
|
exports.awaitDevice = (uuid) ->
|
|
resin.models.device.getName(uuid).then (deviceName) ->
|
|
spinner = new visuals.Spinner("Waiting for #{deviceName} to come online")
|
|
|
|
poll = ->
|
|
resin.models.device.isOnline(uuid).then (isOnline) ->
|
|
if isOnline
|
|
spinner.stop()
|
|
console.info("Device became online: #{deviceName}")
|
|
return
|
|
else
|
|
|
|
# Spinner implementation is smart enough to
|
|
# not start again if it was already started
|
|
spinner.start()
|
|
|
|
return Promise.delay(3000).then(poll)
|
|
|
|
console.info("Waiting for #{deviceName} to connect to resin...")
|
|
poll().return(uuid)
|
|
|
|
exports.printErrorMessage = (message) ->
|
|
console.error(chalk.red(message))
|