mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-19 05:37:51 +00:00
Move cli outside of resin module
This commit is contained in:
parent
08e8cfbb09
commit
e7495fa72d
@ -1,12 +1,13 @@
|
||||
_ = require('lodash')
|
||||
async = require('async')
|
||||
resin = require('../resin')
|
||||
cli = require('../cli/cli')
|
||||
|
||||
exports.create = (name) ->
|
||||
async.waterfall [
|
||||
|
||||
(callback) ->
|
||||
deviceType = resin.cli.getArgument('type')
|
||||
deviceType = cli.getArgument('type')
|
||||
|
||||
if deviceType?
|
||||
return callback(null, deviceType)
|
||||
@ -53,7 +54,7 @@ exports.restart = (id) ->
|
||||
resin.errors.handle(error) if error?
|
||||
|
||||
exports.remove = (id) ->
|
||||
confirmArgument = resin.cli.getArgument('yes')
|
||||
confirmArgument = cli.getArgument('yes')
|
||||
resin.ui.patterns.remove 'application', confirmArgument, (callback) ->
|
||||
resin.models.application.remove(id, callback)
|
||||
, resin.errors.handle
|
||||
|
@ -1,4 +1,5 @@
|
||||
resin = require('../resin')
|
||||
cli = require('../cli/cli')
|
||||
|
||||
exports.list = (applicationId) ->
|
||||
resin.models.device.getAllByApplication applicationId, (error, devices) ->
|
||||
@ -39,7 +40,7 @@ exports.info = (deviceId) ->
|
||||
]
|
||||
|
||||
exports.remove = (id) ->
|
||||
confirmArgument = resin.cli.getArgument('yes')
|
||||
confirmArgument = cli.getArgument('yes')
|
||||
resin.ui.patterns.remove 'device', confirmArgument, (callback) ->
|
||||
resin.models.device.remove(id, callback)
|
||||
, resin.errors.handle
|
||||
|
@ -1,5 +1,6 @@
|
||||
_ = require('lodash')
|
||||
resin = require('../resin')
|
||||
cli = require('../cli/cli')
|
||||
|
||||
SYSTEM_VAR_REGEX = /^RESIN_/
|
||||
|
||||
@ -7,7 +8,7 @@ isSystemVariable = (environmentVariable) ->
|
||||
SYSTEM_VAR_REGEX.test(environmentVariable.name)
|
||||
|
||||
exports.list = ->
|
||||
applicationId = resin.cli.getArgument('application')
|
||||
applicationId = cli.getArgument('application')
|
||||
|
||||
if not applicationId?
|
||||
resin.errors.handle(new Error('You have to specify an application'))
|
||||
@ -15,13 +16,13 @@ exports.list = ->
|
||||
resin.models.environmentVariables.getAll applicationId, (error, environmentVariables) ->
|
||||
resin.errors.handle(error) if error?
|
||||
|
||||
if not resin.cli.getArgument('verbose')?
|
||||
if not cli.getArgument('verbose')?
|
||||
environmentVariables = _.reject(environmentVariables, isSystemVariable)
|
||||
|
||||
resin.log.out(resin.ui.widgets.table.horizontal(environmentVariables))
|
||||
|
||||
exports.remove = (id) ->
|
||||
confirmArgument = resin.cli.getArgument('yes')
|
||||
confirmArgument = cli.getArgument('yes')
|
||||
resin.ui.patterns.remove 'environment variable', confirmArgument, (callback) ->
|
||||
resin.models.environmentVariables.remove(id, callback)
|
||||
, resin.errors.handle
|
||||
|
@ -1,6 +1,7 @@
|
||||
_ = require('lodash')
|
||||
resin = require('../resin')
|
||||
helpers = require('../helpers/helpers')
|
||||
cli = require('../cli/cli')
|
||||
|
||||
exports.list = ->
|
||||
resin.server.get resin.settings.get('urls.keys'), (error, response, keys) ->
|
||||
@ -26,7 +27,7 @@ exports.info = (id) ->
|
||||
resin.log.out(resin.ui.widgets.table.vertical(key, _.identity, [ 'ID', 'Title', 'Public Key' ]))
|
||||
|
||||
exports.remove = (id) ->
|
||||
confirmArgument = resin.cli.getArgument('yes')
|
||||
confirmArgument = cli.getArgument('yes')
|
||||
resin.ui.patterns.remove 'key', confirmArgument, (callback) ->
|
||||
url = _.template(resin.settings.get('urls.sshKey'), { id })
|
||||
resin.server.delete(url, callback)
|
||||
|
@ -2,6 +2,7 @@ _ = require('lodash')
|
||||
PubNub = require('pubnub')
|
||||
resin = require('../resin')
|
||||
helpers = require('../helpers/helpers')
|
||||
cli = require('../cli/cli')
|
||||
|
||||
LOGS_HISTORY_COUNT = 200
|
||||
|
||||
@ -15,8 +16,8 @@ printLogs = (logs, number) ->
|
||||
resin.log.array(logs, resin.log.out)
|
||||
|
||||
exports.logs = (uuid) ->
|
||||
numberOfLines = resin.cli.getArgument('num', _.parseInt)
|
||||
tailOutput = resin.cli.getArgument('tail') or false
|
||||
numberOfLines = cli.getArgument('num', _.parseInt)
|
||||
tailOutput = cli.getArgument('tail') or false
|
||||
|
||||
if numberOfLines? and not _.isNumber(numberOfLines)
|
||||
resin.errors.handle(new Error('n/num should be a number'))
|
||||
|
@ -5,15 +5,16 @@ mkdirp = require('mkdirp')
|
||||
url = require('url')
|
||||
resin = require('../resin')
|
||||
connection = require('../connection/connection')
|
||||
cli = require('../cli/cli')
|
||||
|
||||
exports.download = (id) ->
|
||||
params =
|
||||
network: resin.cli.getArgument('network')
|
||||
wifiSsid: resin.cli.getArgument('wifiSsid')
|
||||
wifiKey: resin.cli.getArgument('wifiKey')
|
||||
network: cli.getArgument('network')
|
||||
wifiSsid: cli.getArgument('wifiSsid')
|
||||
wifiKey: cli.getArgument('wifiKey')
|
||||
|
||||
fileName = resin.os.generateCacheName(id, params)
|
||||
outputFile = resin.cli.getArgument('output') or path.join(resin.settings.get('directories.os'), fileName)
|
||||
outputFile = cli.getArgument('output') or path.join(resin.settings.get('directories.os'), fileName)
|
||||
|
||||
async.waterfall [
|
||||
|
||||
|
@ -3,130 +3,131 @@ resin = require('./resin')
|
||||
packageJSON = require('../package.json')
|
||||
actions = require('./actions')
|
||||
pluginLoader = require('./plugin-loader/plugin-loader')
|
||||
cli = require('./cli/cli')
|
||||
|
||||
resin.cli.setVersion(packageJSON.version)
|
||||
cli.setVersion(packageJSON.version)
|
||||
|
||||
# ---------- Options ----------
|
||||
resin.cli.addOption
|
||||
cli.addOption
|
||||
option: '-y, --yes'
|
||||
description: 'confirm non interactively'
|
||||
|
||||
resin.cli.addOption
|
||||
cli.addOption
|
||||
option: '-v, --verbose'
|
||||
description: 'increase verbosity'
|
||||
|
||||
resin.cli.addOption
|
||||
cli.addOption
|
||||
option: '-q, --quiet'
|
||||
description: 'quiet (no output)'
|
||||
|
||||
resin.cli.addOption
|
||||
cli.addOption
|
||||
option: '-t, --type <type>'
|
||||
description: 'specify a type when creating an application'
|
||||
|
||||
resin.cli.addOption
|
||||
cli.addOption
|
||||
option: '-n, --num <num>'
|
||||
description: 'number of lines to display'
|
||||
|
||||
resin.cli.addOption
|
||||
cli.addOption
|
||||
option: '--tail'
|
||||
description: 'continuously stream output'
|
||||
|
||||
# TODO: I have to use 'application' instead of 'app' here
|
||||
# as Commander gets confused with the app command
|
||||
resin.cli.addOption
|
||||
cli.addOption
|
||||
option: '-a, --application <app>'
|
||||
description: 'application id'
|
||||
coerce: _.parseInt
|
||||
|
||||
resin.cli.addOption
|
||||
cli.addOption
|
||||
option: '-w, --network <network>'
|
||||
description: 'network type when downloading OS'
|
||||
|
||||
resin.cli.addOption
|
||||
cli.addOption
|
||||
option: '-s, --wifi-ssid <wifiSsid>'
|
||||
description: 'wifi ssid, if network is wifi'
|
||||
|
||||
resin.cli.addOption
|
||||
cli.addOption
|
||||
option: '-k, --wifi-key <wifiKey>'
|
||||
description: 'wifi key, if network is wifi'
|
||||
|
||||
resin.cli.addOption
|
||||
cli.addOption
|
||||
option: '-o, --output <output>'
|
||||
description: 'output file'
|
||||
|
||||
# ---------- Auth Module ----------
|
||||
resin.cli.addCommand
|
||||
cli.addCommand
|
||||
command: 'login [username:password]'
|
||||
description: 'login to resin.io'
|
||||
action: actions.auth.login
|
||||
|
||||
resin.cli.addCommand
|
||||
cli.addCommand
|
||||
command: 'logout'
|
||||
description: 'logout from resin.io'
|
||||
action: actions.auth.logout
|
||||
permission: 'user'
|
||||
|
||||
resin.cli.addCommand
|
||||
cli.addCommand
|
||||
command: 'signup'
|
||||
description: 'signup to resin.io'
|
||||
action: actions.auth.signup
|
||||
|
||||
# ---------- App Module ----------
|
||||
resin.cli.addResource
|
||||
cli.addResource
|
||||
name: 'app'
|
||||
displayName: 'application'
|
||||
actions: actions.app
|
||||
permission: 'user'
|
||||
|
||||
resin.cli.addCommand
|
||||
cli.addCommand
|
||||
command: 'app:restart <id>'
|
||||
description: 'restart an application'
|
||||
action: actions.app.restart
|
||||
permission: 'user'
|
||||
|
||||
# ---------- Device Module ----------
|
||||
resin.cli.addResource
|
||||
cli.addResource
|
||||
name: 'device'
|
||||
displayName: 'device'
|
||||
actions: actions.device
|
||||
permission: 'user'
|
||||
|
||||
resin.cli.addCommand
|
||||
cli.addCommand
|
||||
command: 'device:identify <uuid>'
|
||||
description: 'identify a device with a UUID'
|
||||
action: actions.device.identify
|
||||
permission: 'user'
|
||||
|
||||
# ---------- Preferences Module ----------
|
||||
resin.cli.addCommand
|
||||
cli.addCommand
|
||||
command: 'preferences'
|
||||
description: 'open preferences form'
|
||||
action: actions.preferences.preferences
|
||||
permission: 'user'
|
||||
|
||||
# ---------- Keys Module ----------
|
||||
resin.cli.addResource
|
||||
cli.addResource
|
||||
name: 'key'
|
||||
displayName: 'ssh key'
|
||||
actions: actions.keys
|
||||
permission: 'user'
|
||||
|
||||
# ---------- Env Module ----------
|
||||
resin.cli.addResource
|
||||
cli.addResource
|
||||
name: 'env'
|
||||
displayName: 'environment variable'
|
||||
actions: actions.env
|
||||
permission: 'user'
|
||||
|
||||
# ---------- Logs Module ----------
|
||||
resin.cli.addCommand
|
||||
cli.addCommand
|
||||
command: 'logs <uuid>'
|
||||
description: 'show device logs'
|
||||
action: actions.logs.logs
|
||||
permission: 'user'
|
||||
|
||||
# ---------- OS Module ----------
|
||||
resin.cli.addCommand
|
||||
cli.addCommand
|
||||
command: 'os:download <id>'
|
||||
description: 'download device OS'
|
||||
action: actions.os.download
|
||||
@ -135,7 +136,7 @@ resin.cli.addCommand
|
||||
resin.data.prefix.set resin.settings.get('dataPrefix'), (error) ->
|
||||
resin.errors.handle(error) if error?
|
||||
|
||||
resin.cli.parse(process.argv)
|
||||
cli.parse(process.argv)
|
||||
|
||||
quiet = resin.cli.getArgument('quiet')
|
||||
quiet = cli.getArgument('quiet')
|
||||
resin.log.setQuiet(quiet)
|
||||
|
@ -1,9 +1,9 @@
|
||||
auth = require('../auth/auth')
|
||||
resin = require('../resin')
|
||||
|
||||
exports.user = (fn, onError) ->
|
||||
return ->
|
||||
args = arguments
|
||||
auth.isLoggedIn (isLoggedIn) ->
|
||||
resin.auth.isLoggedIn (isLoggedIn) ->
|
||||
|
||||
if not isLoggedIn
|
||||
error = new Error('You have to log in')
|
@ -2,12 +2,10 @@ _ = require('lodash')
|
||||
nock = require('nock')
|
||||
sinon = require('sinon')
|
||||
expect = require('chai').expect
|
||||
data = require('../data/data')
|
||||
auth = require('../auth/auth')
|
||||
settings = require('../settings')
|
||||
resin = require('../resin')
|
||||
cliPermissions = require('./cli-permissions')
|
||||
johnDoeFixture = require('../../../tests/fixtures/johndoe')
|
||||
mock = require('../../../tests/utils/mock')
|
||||
johnDoeFixture = require('../../tests/fixtures/johndoe')
|
||||
mock = require('../../tests/utils/mock')
|
||||
|
||||
describe 'CLI Permissions:', ->
|
||||
|
||||
@ -21,7 +19,7 @@ describe 'CLI Permissions:', ->
|
||||
|
||||
beforeEach (done) ->
|
||||
mock.fs.init()
|
||||
data.prefix.set(settings.get('dataPrefix'), done)
|
||||
resin.data.prefix.set(resin.settings.get('dataPrefix'), done)
|
||||
|
||||
afterEach ->
|
||||
mock.fs.restore()
|
||||
@ -29,7 +27,7 @@ describe 'CLI Permissions:', ->
|
||||
describe 'if not logged in', ->
|
||||
|
||||
beforeEach (done) ->
|
||||
auth.logout(done)
|
||||
resin.auth.logout(done)
|
||||
|
||||
it 'should not call the function', (done) ->
|
||||
spy = sinon.spy()
|
||||
@ -47,7 +45,7 @@ describe 'CLI Permissions:', ->
|
||||
|
||||
# TODO: expect(func).to.throw(Error) doesn't catches
|
||||
# the error as it's being thrown inside an async function
|
||||
# (auth.isLoggedIn). A try/catch works, but it still results
|
||||
# (resin.auth.isLoggedIn). A try/catch works, but it still results
|
||||
# in the error being printed in Mocha reporter.
|
||||
xit 'should throw an error if no error handler function', ->
|
||||
func = cliPermissions.user(_.noop)
|
||||
@ -59,11 +57,11 @@ describe 'CLI Permissions:', ->
|
||||
describe 'if logged in', ->
|
||||
|
||||
beforeEach (done) ->
|
||||
nock(settings.get('remoteUrl'))
|
||||
nock(resin.settings.get('remoteUrl'))
|
||||
.post('/login_', johnDoeFixture.credentials)
|
||||
.reply(200, johnDoeFixture.token)
|
||||
|
||||
auth.login(johnDoeFixture.credentials, done)
|
||||
resin.auth.login(johnDoeFixture.credentials, done)
|
||||
|
||||
it 'should call the function with the correct arguments', (done) ->
|
||||
args = [ 1, 2, 3, 'foo', 'bar' ]
|
@ -2,8 +2,7 @@ _ = require('lodash')
|
||||
program = require('commander')
|
||||
pluralize = require('pluralize')
|
||||
indefiniteArticle = require('indefinite-article')
|
||||
log = require('../log/log')
|
||||
errors = require('../errors/errors')
|
||||
resin = require('../resin')
|
||||
cliPermissions = require('./cli-permissions')
|
||||
|
||||
exports.getArgument = (name, coerceFunction) ->
|
||||
@ -23,7 +22,7 @@ exports.setVersion = (version) ->
|
||||
command: 'version'
|
||||
description: 'show version'
|
||||
action: ->
|
||||
log.out(version)
|
||||
resin.log.out(version)
|
||||
|
||||
applyPermissions = (permission, action, onError) ->
|
||||
permissionFunction = cliPermissions[permission]
|
||||
@ -34,7 +33,7 @@ applyPermissions = (permission, action, onError) ->
|
||||
exports.addCommand = (options = {}) ->
|
||||
|
||||
_.defaults options,
|
||||
onError: errors.handle
|
||||
onError: resin.errors.handle
|
||||
|
||||
if options.permission?
|
||||
action = applyPermissions(options.permission, options.action, options.onError)
|
@ -8,6 +8,5 @@ module.exports =
|
||||
auth: require('./auth/auth')
|
||||
device: require('./device/device')
|
||||
ui: require('./ui')
|
||||
cli: require('./cli/cli')
|
||||
os: require('./os/os')
|
||||
settings: require('./settings')
|
||||
|
Loading…
Reference in New Issue
Block a user