diff --git a/lib/actions/app.coffee b/lib/actions/app.coffee index ce23e242..f103d6be 100644 --- a/lib/actions/app.coffee +++ b/lib/actions/app.coffee @@ -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 diff --git a/lib/actions/device.coffee b/lib/actions/device.coffee index 6b56ec86..9b026f6b 100644 --- a/lib/actions/device.coffee +++ b/lib/actions/device.coffee @@ -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 diff --git a/lib/actions/environment-variables.coffee b/lib/actions/environment-variables.coffee index 0fdb260e..afd0f66c 100644 --- a/lib/actions/environment-variables.coffee +++ b/lib/actions/environment-variables.coffee @@ -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 diff --git a/lib/actions/keys.coffee b/lib/actions/keys.coffee index 550d83a8..8cf553fb 100644 --- a/lib/actions/keys.coffee +++ b/lib/actions/keys.coffee @@ -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) diff --git a/lib/actions/logs.coffee b/lib/actions/logs.coffee index 8361978f..4f7e7b9f 100644 --- a/lib/actions/logs.coffee +++ b/lib/actions/logs.coffee @@ -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')) diff --git a/lib/actions/os.coffee b/lib/actions/os.coffee index d72fb64d..b1bcd284 100644 --- a/lib/actions/os.coffee +++ b/lib/actions/os.coffee @@ -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 [ diff --git a/lib/app.coffee b/lib/app.coffee index 9f35dda1..0ac22f0e 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -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 ' description: 'specify a type when creating an application' -resin.cli.addOption +cli.addOption option: '-n, --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 ' description: 'application id' coerce: _.parseInt -resin.cli.addOption +cli.addOption option: '-w, --network ' description: 'network type when downloading OS' -resin.cli.addOption +cli.addOption option: '-s, --wifi-ssid ' description: 'wifi ssid, if network is wifi' -resin.cli.addOption +cli.addOption option: '-k, --wifi-key ' description: 'wifi key, if network is wifi' -resin.cli.addOption +cli.addOption option: '-o, --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 ' 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 ' 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 ' description: 'show device logs' action: actions.logs.logs permission: 'user' # ---------- OS Module ---------- -resin.cli.addCommand +cli.addCommand command: 'os:download ' 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) diff --git a/lib/resin/cli/cli-permissions.coffee b/lib/cli/cli-permissions.coffee similarity index 76% rename from lib/resin/cli/cli-permissions.coffee rename to lib/cli/cli-permissions.coffee index 4917e8ad..93fd144a 100644 --- a/lib/resin/cli/cli-permissions.coffee +++ b/lib/cli/cli-permissions.coffee @@ -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') diff --git a/lib/resin/cli/cli-permissions.spec.coffee b/lib/cli/cli-permissions.spec.coffee similarity index 80% rename from lib/resin/cli/cli-permissions.spec.coffee rename to lib/cli/cli-permissions.spec.coffee index 65cc31bd..fa07d639 100644 --- a/lib/resin/cli/cli-permissions.spec.coffee +++ b/lib/cli/cli-permissions.spec.coffee @@ -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' ] diff --git a/lib/resin/cli/cli.coffee b/lib/cli/cli.coffee similarity index 95% rename from lib/resin/cli/cli.coffee rename to lib/cli/cli.coffee index 8b936a7f..a1ec3844 100644 --- a/lib/resin/cli/cli.coffee +++ b/lib/cli/cli.coffee @@ -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) diff --git a/lib/resin/index.coffee b/lib/resin/index.coffee index 2e14a1ea..f1040c13 100644 --- a/lib/resin/index.coffee +++ b/lib/resin/index.coffee @@ -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')