2015-03-13 12:53:31 +00:00
|
|
|
open = require('open')
|
2015-01-09 15:22:18 +00:00
|
|
|
_ = require('lodash-contrib')
|
2014-12-01 14:11:00 +00:00
|
|
|
url = require('url')
|
2014-11-18 16:15:40 +00:00
|
|
|
async = require('async')
|
2015-01-08 12:04:37 +00:00
|
|
|
resin = require('resin-sdk')
|
2015-04-08 12:25:27 +00:00
|
|
|
settings = require('resin-settings-client')
|
2015-07-27 12:08:55 +00:00
|
|
|
form = require('resin-cli-form')
|
2015-07-30 11:48:03 +00:00
|
|
|
visuals = require('resin-cli-visuals')
|
2014-11-18 12:41:13 +00:00
|
|
|
|
2015-06-02 17:21:59 +00:00
|
|
|
TOKEN_URL = url.resolve(settings.get('dashboardUrl'), '/preferences')
|
2015-03-13 12:53:31 +00:00
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
exports.login =
|
2015-03-11 18:37:24 +00:00
|
|
|
signature: 'login [token]'
|
2015-01-15 17:10:14 +00:00
|
|
|
description: 'login to resin.io'
|
2015-03-13 12:53:31 +00:00
|
|
|
help: """
|
2015-01-15 17:10:14 +00:00
|
|
|
Use this command to login to your resin.io account.
|
2014-11-18 16:15:40 +00:00
|
|
|
|
2015-03-11 18:37:24 +00:00
|
|
|
To login, you need your token, which is accesible from the preferences page:
|
|
|
|
|
2015-03-13 12:53:31 +00:00
|
|
|
#{TOKEN_URL}
|
2014-11-18 16:15:40 +00:00
|
|
|
|
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 login
|
2015-03-11 18:37:24 +00:00
|
|
|
$ resin login "eyJ0eXAiOiJKV1Qi..."
|
2015-03-13 12:53:31 +00:00
|
|
|
"""
|
2015-01-15 17:10:14 +00:00
|
|
|
action: (params, options, done) ->
|
2015-03-13 12:53:31 +00:00
|
|
|
|
2015-07-27 12:08:55 +00:00
|
|
|
async.waterfall [
|
2014-11-18 16:15:40 +00:00
|
|
|
|
2015-03-13 12:53:31 +00:00
|
|
|
(callback) ->
|
2015-05-11 12:42:25 +00:00
|
|
|
return callback(null, params.token) if params.token?
|
|
|
|
|
|
|
|
console.info """
|
|
|
|
To login to the Resin CLI, you need your unique token, which is accesible from
|
|
|
|
the preferences page at #{TOKEN_URL}
|
|
|
|
|
|
|
|
Attempting to open a browser at that location...
|
|
|
|
"""
|
|
|
|
|
2015-03-13 12:53:31 +00:00
|
|
|
open TOKEN_URL, (error) ->
|
|
|
|
if error?
|
|
|
|
console.error """
|
|
|
|
Unable to open a web browser in the current environment.
|
|
|
|
Please visit #{TOKEN_URL} manually.
|
|
|
|
"""
|
|
|
|
|
2015-07-27 12:08:55 +00:00
|
|
|
form.ask
|
|
|
|
message: 'What\'s your token? (visible in the preferences page)'
|
|
|
|
type: 'input'
|
|
|
|
.nodeify(callback)
|
2014-11-18 15:37:29 +00:00
|
|
|
|
2015-03-11 18:37:24 +00:00
|
|
|
(token, callback) ->
|
2015-07-22 22:06:53 +00:00
|
|
|
resin.auth.loginWithToken(token).nodeify(callback)
|
2015-06-02 15:57:52 +00:00
|
|
|
|
|
|
|
(callback) ->
|
2015-07-22 22:06:53 +00:00
|
|
|
resin.auth.whoami().nodeify(callback)
|
2015-06-02 15:57:52 +00:00
|
|
|
|
|
|
|
(username, callback) ->
|
|
|
|
console.info("Successfully logged in as: #{username}")
|
|
|
|
return callback()
|
2014-11-18 15:48:05 +00:00
|
|
|
|
2015-07-27 12:08:55 +00:00
|
|
|
], done
|
2014-12-24 15:14:30 +00:00
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
exports.logout =
|
|
|
|
signature: 'logout'
|
|
|
|
description: 'logout from resin.io'
|
|
|
|
help: '''
|
|
|
|
Use this command to logout from your resin.io account.o
|
2014-12-24 15:14:30 +00:00
|
|
|
|
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 logout
|
|
|
|
'''
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-07-22 22:06:53 +00:00
|
|
|
resin.auth.logout().nodeify(done)
|
2014-12-24 15:14:30 +00:00
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
exports.signup =
|
|
|
|
signature: 'signup'
|
|
|
|
description: 'signup to resin.io'
|
|
|
|
help: '''
|
|
|
|
Use this command to signup for a resin.io account.
|
2014-12-24 15:14:30 +00:00
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
If signup is successful, you'll be logged in to your new user automatically.
|
2014-12-12 14:25:32 +00:00
|
|
|
|
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 signup
|
|
|
|
Email: me@mycompany.com
|
|
|
|
Username: johndoe
|
|
|
|
Password: ***********
|
2014-12-12 14:25:32 +00:00
|
|
|
|
2015-02-09 15:43:10 +00:00
|
|
|
$ resin signup --email me@mycompany.com --username johndoe --password ***********
|
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
$ resin whoami
|
|
|
|
johndoe
|
|
|
|
'''
|
2015-02-09 15:43:10 +00:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
signature: 'email'
|
|
|
|
parameter: 'email'
|
|
|
|
description: 'user email'
|
|
|
|
alias: 'e'
|
|
|
|
}
|
|
|
|
{
|
|
|
|
signature: 'username'
|
|
|
|
parameter: 'username'
|
|
|
|
description: 'user name'
|
|
|
|
alias: 'u'
|
|
|
|
}
|
|
|
|
{
|
|
|
|
signature: 'password'
|
|
|
|
parameter: 'user password'
|
|
|
|
description: 'user password'
|
|
|
|
alias: 'p'
|
|
|
|
}
|
|
|
|
]
|
2015-01-15 17:10:14 +00:00
|
|
|
action: (params, options, done) ->
|
2015-02-09 15:43:10 +00:00
|
|
|
|
|
|
|
hasOptionCredentials = not _.isEmpty(options)
|
|
|
|
|
|
|
|
if hasOptionCredentials
|
|
|
|
|
|
|
|
if not options.email?
|
|
|
|
return done(new Error('Missing email'))
|
|
|
|
|
|
|
|
if not options.username?
|
|
|
|
return done(new Error('Missing username'))
|
|
|
|
|
|
|
|
if not options.password?
|
|
|
|
return done(new Error('Missing password'))
|
|
|
|
|
2015-07-27 12:08:55 +00:00
|
|
|
async.waterfall [
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
(callback) ->
|
2015-02-09 15:43:10 +00:00
|
|
|
return callback(null, options) if hasOptionCredentials
|
2015-07-27 12:08:55 +00:00
|
|
|
form.run [
|
|
|
|
message: 'Email:'
|
|
|
|
name: 'email'
|
|
|
|
type: 'input'
|
|
|
|
,
|
|
|
|
message: 'Username:'
|
|
|
|
name: 'username'
|
|
|
|
type: 'input'
|
|
|
|
,
|
|
|
|
message: 'Password:'
|
|
|
|
name: 'password'
|
|
|
|
type: 'password',
|
|
|
|
validate: (input) ->
|
|
|
|
if input.length < 8
|
|
|
|
return 'Password should be 8 characters long'
|
|
|
|
|
|
|
|
return true
|
|
|
|
]
|
|
|
|
.nodeify(callback)
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
(credentials, callback) ->
|
2015-07-22 22:06:53 +00:00
|
|
|
resin.auth.register(credentials).return(credentials).nodeify(callback)
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
(credentials, callback) ->
|
2015-07-22 22:06:53 +00:00
|
|
|
resin.auth.login(credentials).nodeify(callback)
|
2015-01-15 17:10:14 +00:00
|
|
|
|
2015-07-27 12:08:55 +00:00
|
|
|
], done
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
exports.whoami =
|
|
|
|
signature: 'whoami'
|
|
|
|
description: 'get current username'
|
|
|
|
help: '''
|
|
|
|
Use this command to find out the current logged in username.
|
|
|
|
|
|
|
|
Examples:
|
2015-03-03 14:14:16 +00:00
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
$ resin whoami
|
|
|
|
'''
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-07-22 22:06:53 +00:00
|
|
|
resin.auth.whoami().then (username) ->
|
2015-01-15 17:10:14 +00:00
|
|
|
if not username?
|
2015-07-22 22:06:53 +00:00
|
|
|
throw new Error('Username not found')
|
2015-07-29 17:46:37 +00:00
|
|
|
resin.auth.getEmail().then (email) ->
|
2015-07-30 11:48:03 +00:00
|
|
|
console.log visuals.table.vertical { username, email }, [
|
|
|
|
'$account information$'
|
|
|
|
'username'
|
|
|
|
'email'
|
|
|
|
]
|
2015-07-22 22:06:53 +00:00
|
|
|
.nodeify(done)
|