<commit_before>/* global process, Promise */

/**
 * Module dependencies
 */

var chalk = require('chalk')
var Table = require('cli-table')

/**
 * Client commands
 */

function registerClient (cli, options, done) {
  var clientCmd = cli.command('client')

  /**
   * API Error Response Handler
   */

  function apiError (err) {
    if (err.statusCode && [401, 403].indexOf(err.statusCode) !== -1) {
      cli.log.error('Please login to the issuer.')
    } else {
      console.log(err)
    }

    process.exit(1)
  }

  /**
   * Display Field
   */

  function displayField (key, value, format) {
    if (typeof value !== 'undefined' && !(Array.isArray(value) && value.length === 0)) {
      // determine the number of tab characters to use
      var tabs = '\t'
      var diff = 24 - key.length
      if (diff > 8) { tabs += '\t' }
      if (diff > 16) { tabs += '\t' }

      // format the value
      if (Array.isArray(value)) {
        value = value.map(function (item) {
          return (format) ? chalk[format](item) : item
        }).join('\n\t\t\t')
      } else {
        value = (format) ? chalk[format](value) : value
      }

      cli.log(key + tabs + value)
    }
  }

  /**
   * Display Client
   */

  function displayClient (data) {
    cli.log()
    displayField('Name', data.client_name, 'bold')
    displayField('Client ID', data._id, 'yellow')
    displayField('Client Secret', data.client_secret, 'yellow')
    displayField('Client URI', data.client_uri, 'cyan')
    displayField('Application type', data.application_type)
    displayField('Response types', data.response_types)
    displayField('Grant types', data.grant_types)
    displayField('Token auth method', data.token_endpoint_auth_method)
    displayField('Default max age', data.default_max_age)
    displayField('Redirect URIs', data.redirect_uris, 'cyan')
    displayField('Logout Redirect URIs', data.post_logout_redirect_uris, 'cyan')
    displayField('User ID', data.userId, 'yellow')
    displayField('Trusted', data.trusted || false, (data.trusted) ? 'green' : 'gray')
    displayField('Scopes', data.scopes)
    displayField('Created', data.created && new Date(data.created))
    displayField('Modified', data.modified && new Date(data.modified))
  }

  clientCmd
    .handler(function (data, flags, done) {
      done()
    })

  /**
   * Client registration
   */

  clientCmd
    .task('register')
    .handler(function (data, flags, done) {
      cli.issuers.prompt(flags['issuer'] || flags.i, function (err, issuer) {
        if (err) {
          cli.log.error(err)
          process.exit(1)
        }

        try {
          var anvil = cli.client.create(issuer)
        } catch (e) {
          cli.log.error(e)
          process.exit(1)
        }

        anvil.discover()
          .then(function (configuration) {
            cli.prompt([
              {
                type: 'confirm',
                name: 'trusted',<commit_msg>feat(clients): add usage to default client command<commit_after>/* global process, Promise */

/**
 * Module dependencies
 */

var chalk = require('chalk')
var Table = require('cli-table')

/**
 * Client commands
 */

function registerClient (cli, options, done) {
  var clientCmd = cli.command('client')

  /**
   * API Error Response Handler
   */

  function apiError (err) {
    if (err.statusCode && [401, 403].indexOf(err.statusCode) !== -1) {
      cli.log.error('Please login to the issuer.')
    } else {
      console.log(err)
    }

    process.exit(1)
  }

  /**
   * Display Field
   */

  function displayField (key, value, format) {
    if (typeof value !== 'undefined' && !(Array.isArray(value) && value.length === 0)) {
      // determine the number of tab characters to use
      var tabs = '\t'
      var diff = 24 - key.length
      if (diff > 8) { tabs += '\t' }
      if (diff > 16) { tabs += '\t' }

      // format the value
      if (Array.isArray(value)) {
        value = value.map(function (item) {
          return (format) ? chalk[format](item) : item
        }).join('\n\t\t\t')
      } else {
        value = (format) ? chalk[format](value) : value
      }

      cli.log(key + tabs + value)
    }
  }

  /**
   * Display Client
   */

  function displayClient (data) {
    cli.log()
    displayField('Name', data.client_name, 'bold')
    displayField('Client ID', data._id, 'yellow')
    displayField('Client Secret', data.client_secret, 'yellow')
    displayField('Client URI', data.client_uri, 'cyan')
    displayField('Application type', data.application_type)
    displayField('Response types', data.response_types)
    displayField('Grant types', data.grant_types)
    displayField('Token auth method', data.token_endpoint_auth_method)
    displayField('Default max age', data.default_max_age)
    displayField('Redirect URIs', data.redirect_uris, 'cyan')
    displayField('Logout Redirect URIs', data.post_logout_redirect_uris, 'cyan')
    displayField('User ID', data.userId, 'yellow')
    displayField('Trusted', data.trusted || false, (data.trusted) ? 'green' : 'gray')
    displayField('Scopes', data.scopes)
    displayField('Created', data.created && new Date(data.created))
    displayField('Modified', data.modified && new Date(data.modified))
  }

  clientCmd
    .handler(function (data, flags, done) {

      cli.log('Usage:')
      cli.log(
        '  nvl client:register [--issuer | -i <issuer id>] [--trusted | -t]' +
        '[--name | -n <name>] [--uri | -u <uri>]\n\t' +
        '[--logo-uri | -l <logo uri>] [--application-type | -a <app type>]\n\t' +
        '[--response-type | -r <response type>] [--grant-type | -g <grant type>]\n\t' +
        '[--default-max-age | -d <seconds>] [--redirect-uri | -s <redirect uri>]\n\t' +
        '[--post-logout-redirect-uri | -p <post logout redirect uri>]'
      )

      cli.log('  nvl client:list [--issuer | -i <issuer id>]')

      cli.log('  nvl client:info [<id>] [--issuer | -i <issuer id>]')

      cli.log(
        '  nvl client:update [<id>] [--issuer | -i <issuer id>] [--trusted | -t]\n\t' +
        '[--name | -n <name>] [--uri | -u <uri>]\n\t' +
        '[--logo-uri | -l <logo uri>] [--application-type | -a <app type>]\n\t' +
        '[--response-type | -r <response type>] [--grant-type | -g <grant type>]\n\t' +
        '[--default-max-age | -d <seconds>] [--redirect-uri | -s <redirect uri>]\n\t' +
        '[--post-logout-redirect-uri | -p <post logout redirect uri>]'
      )

      cli.log('  nvl client:delete [<id>] [--issuer | -i <issuer id>]')

      done()
    })

  /**
   * Client registration
   */

  clientCmd
    .task('register')
    .handler(function (data, flags, done) {
      cli.issuers.prompt(flags['issuer'] || flags.i, function (err, issuer) {
        if (err) {
          cli.log.error(err)
          process.exit(1)
        }

        try {
          var anvil = cli.client.create(issuer)
        } catch (e) {
          cli.log.error(e)
          process.exit(1)
        }

        anvil.discover()
          .then(function (configuration) {
            cli.prompt([
              {
                type: 'confirm',
                name: 'trusted',
{'fraction_non_alphanumeric': 0.16874736989760133, 'fraction_numerical': 0.004488708093701782, 'mean_word_length': 2.3807491702228543, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 33, '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': '4474666', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2297, 'n_words': 534}