Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update registry commands, unpublish, add some nicer messages #794

Merged
merged 9 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var config = {
require('../src/commands/share'),
require('../src/commands/status'),
require('../src/commands/sync'),
require('../src/commands/unpublish'),
require('../src/commands/auth/register'),
require('../src/commands/auth/whoami'),
require('../src/commands/auth/logout'),
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"dat-link-resolve": "^1.0.0",
"dat-log": "^1.1.0",
"dat-node": "^3.3.2",
"dat-registry": "^2.1.2",
"dat-registry": "^3.0.2",
"debug": "^2.6.6",
"hyperdrive-http": "^4.1.0",
"neat-log": "^1.1.0",
Expand Down
74 changes: 56 additions & 18 deletions src/commands/auth/login.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,82 @@
var prompt = require('prompt')
var output = require('neat-log/output')
var chalk = require('chalk')
var Registry = require('../../registry')

module.exports = {
name: 'login',
command: login,
options: []
help: [
'Login to a Dat registry server',
'Usage: dat login [<registry>]',
'',
'Publish your dats so other users can discovery them.',
'Please register before trying to login.'
].join('\n'),
options: [
{
name: 'server',
help: 'Your Dat registry server (must be registered to login).'
}
]
}

function login (opts) {
if (opts.email && opts.password) return makeRequest(opts)
if (opts._[0]) opts.server = opts._[0]
var welcome = output`
Welcome to ${chalk.green(`dat`)} program!
Login to publish your dats.

`
console.log(welcome)

var schema = {
properties: {
server: {
description: chalk.magenta('Dat registry'),
default: opts.server || 'datproject.org',
required: true
},
email: {
description: chalk.magenta('Email'),
message: 'Email required',
required: true
},
password: {
description: chalk.magenta('Password'),
message: 'Password required',
required: true,
hidden: true,
replace: '*'
}
}
}

prompt.override = opts
prompt.message = ''
prompt.colors = false
prompt.start()
prompt.get([{
name: 'email',
description: 'Email',
required: true
},
{
name: 'password',
description: 'Password',
required: true,
hidden: true,
replace: '*'
}], function (err, results) {
if (err) return console.log(err.message)
prompt.get(schema, function (err, results) {
if (err) return exitErr(err)
opts.server = results.server
makeRequest(results)
})

function makeRequest (user) {
var client = Registry(opts)

client.login({
email: user.email,
password: user.password
}, function (err, resp, body) {
if (err && err.message) return exitErr(err.message)
else if (err) return exitErr(err.toString())
console.log('Logged in successfully.')

console.log(output`
Logged you in to ${chalk.green(opts.server)}!

Now you can publish dats and share:
* Run ${chalk.green(`dat publish`)} to publish a dat!
* View & Share your dats at ${opts.server}
`)
process.exit(0)
})
}
Expand Down
22 changes: 19 additions & 3 deletions src/commands/auth/logout.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
var chalk = require('chalk')
var Registry = require('../../registry')

module.exports = {
name: 'logout',
command: logout,
options: []
help: [
'Logout from current Dat registry server',
'Usage: dat logout [<registry>]',
'',
'Specify server if you want to from non-active other server.',
'Check active server with `dat whoami`.'
].join('\n'),
options: [
{
name: 'server',
help: 'Server to log out of. Defaults to active login.'
}
]
}

function logout (opts) {
if (opts._[0]) opts.server = opts._[0]

var client = Registry(opts)

if (!client.whoami().token) return exitErr('Not logged in.')
var whoami = client.whoami()
if (!whoami || !whoami.token) return exitErr('Not currently logged in to that server.')
client.logout(function (err) {
if (err) return exitErr(err)
console.log('Logged out.')
console.log(`Logged out of ${chalk.green(whoami.server)}`)
process.exit(0)
})
}
Expand Down
80 changes: 58 additions & 22 deletions src/commands/auth/register.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,68 @@
var prompt = require('prompt')
var output = require('neat-log/output')
var chalk = require('chalk')
var Registry = require('../../registry')

module.exports = {
name: 'register',
command: register,
options: []
help: [
'Register with a public Dat registry',
'Usage: dat register [<registry>]',
'',
'Register with datproject.org or other registries to publish your dats.'
].join('\n'),
options: [
{
name: 'server',
help: 'Your Dat registry.'
}
]
}

function register (opts) {
if (opts.email && opts.username && opts.password) return makeRequest(opts)
// TODO: check if logged in?
if (opts._[0]) opts.server = opts._[0]
var welcome = output`
Welcome to ${chalk.green(`dat`)} program!
Create a new account with a Dat registry.

`
console.log(welcome)

var schema = {
properties: {
server: {
description: chalk.magenta('Dat registry'),
default: opts.server || 'datproject.org',
required: true
},
username: {
description: chalk.magenta('Username'),
message: 'Username required',
required: true
},
email: {
description: chalk.magenta('Email'),
message: 'Email required',
required: true
},
password: {
description: chalk.magenta('Password'),
message: 'Password required',
required: true,
hidden: true,
replace: '*'
}
}
}

prompt.override = opts
prompt.message = ''
prompt.colors = false
prompt.start()
prompt.get([{
name: 'email',
description: 'Email',
required: true
},
{
name: 'username',
description: 'Username',
required: true
},
{
name: 'password',
description: 'Password',
required: true,
hidden: true,
replace: '*'
}], function (err, results) {
if (err) return console.log(err.message)
prompt.get(schema, function (err, results) {
if (err) return exitErr(err)
opts.server = results.server
makeRequest(results)
})

Expand All @@ -44,7 +76,11 @@ function register (opts) {
}, function (err) {
if (err && err.message) return exitErr(err.message)
else if (err) return exitErr(err.toString())
console.log('Registered successfully.')
console.log(output`
Created account on ${chalk.green(opts.server)}!

Login to start publishing: ${chalk.green(`dat login`)}
`)
process.exit(0)
})
}
Expand Down
35 changes: 32 additions & 3 deletions src/commands/auth/whoami.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
var output = require('neat-log/output')
var chalk = require('chalk')
var Registry = require('../../registry')

module.exports = {
name: 'whoami',
command: whoami,
options: []
help: [
'Get login information',
'Usage: dat login [<registry>]',
'',
'Get information for active registry or specify your registry.'
].join('\n'),
options: [
{
name: 'server',
help: 'Server to get login information for. Defaults to active login.'
}
]
}

function whoami (opts) {
if (opts._[0]) opts.server = opts._[0]

var client = Registry(opts)
var login = client.whoami()
if (!login.token) return exitErr('Not logged in.')
console.log(login.email)
if (!login || !login.token) {
if (!opts.server) return exitErr('No login information found.')
return exitErr('No login information found for that server.')
}
console.log(output`
Your active Dat registry information:

---
${chalk.green(login.server)}
Email: ${login.email}
Username: ${login.username}
---

Change your registry by logging in again:
${chalk.dim.green('dat login <registry-url>')}
`)
process.exit(0)
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ function create (opts) {
}

prompt.override = { title: opts.title, description: opts.description }
prompt.message = chalk.green('> ')
prompt.delimiter = ''
prompt.message = '' // chalk.green('> ')
// prompt.delimiter = ''
prompt.start()
prompt.get(schema, writeDatJson)

Expand Down
Loading