<commit_before>#!/usr/bin/env node

var data = require('level-userdb')
var dnodeClient = require('level-userdb-dnode').client
var editor = require('editor')
var fs = require('fs')
var mktemp = require('mktemp')
var ui = require('optimist')
  .usage('Usage: $0 -d [database] -r [server:port] -e [email] -p [password] <COMMAND>')
  .alias('d', 'database')
  .describe('d', 'Path to database location')
  .alias('e', 'email')
  .describe('e', 'email address')
  .alias('n', 'new-email')
  .describe('n', 'new email address')
  .alias('p', 'password')
  .describe('p', 'password')
  .alias('r', 'remote')


var oh = ui.help()
ui.help = help

var commands = {
  create: {desc:"Create a new user with email & password"},
  delete: {desc:"Delete a user by email"},
  edit: {desc:"Edit a user's data field with $EDITOR"},
  email: {desc:"Update a user's email address"},
  list: {desc:"List all users"},
  password: {desc:"<PASSWORD>'s password by email"},
  retrieve: {desc:"Fetch an existing user by email"},
  verify: {desc:"Check password"},
}

function help() {
  var msg = ""
  Object.keys(commands).forEach(function(k) {
    var cmd = k
    var desc = commands[cmd].desc
    if (cmd.length < 6) cmd += "\t"
    msg += "  " + cmd + "\t " + desc + "\n"
  })
  var h = oh + "\n" +
    "Commands:\n" +
    msg
  return h

}

var argv = ui.argv

function bail() {
  console.error("Error: You must specify a command\n")
  ui.showHelp()
  process.exit(1)
}

if (!argv.r && !argv.d) {
  console.log('Error: You must specify a database via -d (filesystem) or -r (remote)')
  ui.showHelp()
  process.exit(1)
}

if (argv.r && argv.d) {
  console.log('Error: You cannot specify BOTH a database via -d (filesystem) and -r (remote)')
  ui.showHelp()
  process.exit(1)
}


if (argv._.length < 1) bail()

argv._.forEach(function(cmd) {
  if (Object.keys(commands).indexOf(cmd) === -1) {
    bail()
  }
  if (argv.database) {
    handleCmd(cmd, data(argv.database), function() {})
  }
  if (argv.remote) {
    var host = argv.remote.split(':')[0]
    var port = 9998
    try {
      port = parseInt(argv.remote.split(':')[1])
    } catch(e) {
    }
    console.log("connecting to %s:%d", host, port)
    dnodeClient({host:host, port:port}, function(r, c) {
      console.log("connected to %s:%d", host, port)
      handleCmd(cmd, r, function() {
        c.end()
        process.exit(0)
      })
    })

  }
})

function handleCmd(cmd, db, cb) {
  if (cmd === 'create') {
    console.log("Creating user")
    db.addUser(argv.email, argv.password, function(err) {
      if (err) throw err
      console.log("ok")
      cb(null)
    })
  } else if (cmd === 'retrieve') {
    db.findUser(argv.email, function(err, userObj) {
      if (err) {
        console.log("Error: %s not found", argv.email)
        process.exit(1)
      }
      console.log("User email: %s", argv.email)
      console.log("==============================")
      console.log("Created:\t %s", userObj.createdDate)
      console.log("Modified:\t %s", userObj.modifiedDate)
      console.log("Password hash:\t %s", userObj.password)
      console.log("Arbitrary data:\t %s", JSON.stringify(userObj.data, null, '\t'))
      cb(null)
    })
  } else if (cmd === 'verify') {
    console.log("verify")
    db.checkPassword(argv.email, argv.password, function(err, res) {
      console.log("User email: %s", argv.email)
      console.log("==============================")
      if (err) {
        console.log("Bad password")
        process.exit(1)
<commit_msg>make verify nicer<commit_after>#!/usr/bin/env node

var data = require('level-userdb')
var dnodeClient = require('level-userdb-dnode').client
var editor = require('editor')
var fs = require('fs')
var mktemp = require('mktemp')
var ui = require('optimist')
  .usage('Usage: $0 -d [database] -r [server:port] -e [email] -p [password] <COMMAND>')
  .alias('d', 'database')
  .describe('d', 'Path to database location')
  .alias('e', 'email')
  .describe('e', 'email address')
  .alias('n', 'new-email')
  .describe('n', 'new email address')
  .alias('p', 'password')
  .describe('p', 'password')
  .alias('r', 'remote')


var oh = ui.help()
ui.help = help

var commands = {
  create: {desc:"Create a new user with email & password"},
  delete: {desc:"Delete a user by email"},
  edit: {desc:"Edit a user's data field with $EDITOR"},
  email: {desc:"Update a user's email address"},
  list: {desc:"List all users"},
  password: {desc:"Update a user's password by email"},
  retrieve: {desc:"Fetch an existing user by email"},
  verify: {desc:"Check password"},
}

function help() {
  var msg = ""
  Object.keys(commands).forEach(function(k) {
    var cmd = k
    var desc = commands[cmd].desc
    if (cmd.length < 6) cmd += "\t"
    msg += "  " + cmd + "\t " + desc + "\n"
  })
  var h = oh + "\n" +
    "Commands:\n" +
    msg
  return h

}

var argv = ui.argv

function bail() {
  console.error("Error: You must specify a command\n")
  ui.showHelp()
  process.exit(1)
}

if (!argv.r && !argv.d) {
  console.log('Error: You must specify a database via -d (filesystem) or -r (remote)')
  ui.showHelp()
  process.exit(1)
}

if (argv.r && argv.d) {
  console.log('Error: You cannot specify BOTH a database via -d (filesystem) and -r (remote)')
  ui.showHelp()
  process.exit(1)
}


if (argv._.length < 1) bail()

argv._.forEach(function(cmd) {
  if (Object.keys(commands).indexOf(cmd) === -1) {
    bail()
  }
  if (argv.database) {
    handleCmd(cmd, data(argv.database), function() {})
  }
  if (argv.remote) {
    var host = argv.remote.split(':')[0]
    var port = 9998
    try {
      port = parseInt(argv.remote.split(':')[1])
    } catch(e) {
    }
    console.log("connecting to %s:%d", host, port)
    dnodeClient({host:host, port:port}, function(r, c) {
      console.log("connected to %s:%d", host, port)
      handleCmd(cmd, r, function() {
        c.end()
        process.exit(0)
      })
    })

  }
})

function handleCmd(cmd, db, cb) {
  if (cmd === 'create') {
    console.log("Creating user")
    db.addUser(argv.email, argv.password, function(err) {
      if (err) throw err
      console.log("ok")
      cb(null)
    })
  } else if (cmd === 'retrieve') {
    db.findUser(argv.email, function(err, userObj) {
      if (err) {
        console.log("Error: %s not found", argv.email)
        process.exit(1)
      }
      console.log("User email: %s", argv.email)
      console.log("==============================")
      console.log("Created:\t %s", userObj.createdDate)
      console.log("Modified:\t %s", userObj.modifiedDate)
      console.log("Password hash:\t %s", userObj.password)
      console.log("Arbitrary data:\t %s", JSON.stringify(userObj.data, null, '\t'))
      cb(null)
    })
  } else if (cmd === 'verify') {
    db.checkPassword(argv.email, argv.password, function(err, res) {
      console.log("User email: %s", argv.email)
      console.log("==============================")
      if (err) {
        console.log("Bad password: %s", err)
        process.exit(1)
{'fraction_non_alphanumeric': 0.20300214438884917, 'fraction_numerical': 0.0045746962115796996, 'mean_word_length': 3.139644970414201, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3411821', 'n_tokens_mistral': 2462, 'n_tokens_neox': 2425, 'n_words': 617}