<commit_before>/*
 * jitsu.js: Top-level include for the jitsu module.
 *
 * (C) 2010, Nodejitsu Inc.
 *
 */
 
require.paths.unshift(__dirname);

var jitsu = exports,
    auth = false,
    started = false;

// Failure HTTP Response codes based
// off of `/lib/broodmother/slave/service.js`
var failCodes = jitsu.failCodes = {
  400: "Bad Request",
  403: "Not Authorized",
  404: "Item not found",
  500: "Internal Server Error"
};

// Success HTTP Response codes based
// off of `/lib/broodmother/slave/service.js`
var successCodes = jitsu.successCodes = {
  200: "OK",
  201: "Created"
};

jitsu.utils         = require('jitsu/utils');
jitsu.log           = require('jitsu/log');
jitsu.api           = {};
jitsu.api.Client    = require('jitsu/api/client').Client;
jitsu.api.Apps      = require('jitsu/api/apps').Apps;
jitsu.api.Snapshots = require('jitsu/api/snapshots').Snapshots;
jitsu.api.Users     = require('jitsu/api/users').Users;
jitsu.prompt        = require('jitsu/prompt');
jitsu.config        = require('jitsu/config');
jitsu.commands      = require('jitsu/commands');

var eyes = require('eyes'),
    winston = require('winston'),
    colors = require('colors');

//
// ### function start (command, callback)
// #### @command {string} Command to execute once started
// #### @callback {function} Continuation to pass control to when complete.
// Starts the jitsu CLI and runs the specified command.
//
jitsu.start = function (command, callback) {
  // Setup the initial prompt but don't leave it open
  jitsu.prompt.start().pause();
  
  winston.info('Welcome to ' + 'Nodejitsu'.grey);
  winston.info('It worked if it ends with ' + 'Nodejitsu'.grey + ' ok'.yellow.bold);
  
  jitsu.config.load(function (err) {
    if (!jitsu.config.username) {
      return jitsu.setupUser(function (err) {
        return err ? callback(err) : jitsu.exec(command, callback);
      });
    }
    
    return err ? callback(err) : jitsu.exec(command, callback);
  });
};

//
// ### function exec (command, callback)
// #### @command {string} Command to execute
// #### @callback {function} Continuation to pass control to when complete.
// Runs the specified command in the jitsu CLI.
//
jitsu.exec = function (command, callback) {
  if (!command) {
    winston.error('No command supplied');
    return jitsu.showError(command, {}, true, true);
  }
  
  function execCommand (err) {
    if (err) {
      return callback(err);
    }
    
    winston.info('Executing command ' + jitsu.commands.parse(command).splice(0, 2).join(' ').magenta);
    jitsu.commands.run(command, function (err, shallow) {
      if (err) {
        return jitsu.showError(command, err, shallow);
      }

      //
      // TODO (indexzero): Something here
      //
      callback();
    });
  }
  
  return !started ? jitsu.setup(execCommand) : execCommand();
};

//
// ### function setup (callback)
// #### @callback {function} Continuation to pass control to when complete.
// Sets up the instances of the Resource clients for jitsu and tests
// the username/password combo for the authenticated user.
//
jitsu.setup = function (callback) {
  if (started) {
    return;
  }
  
  jitsu.users = new jitsu.api.Users(jitsu.config.settings);
  jitsu.apps = new jitsu.api.Apps(jitsu.config.settings);
  jitsu.snapshots = new jitsu.api.Snapshots(jitsu.config.settings);
  started = true;
  callback();
};

//
// ### function auth (callback)
// #### @callback {function} Continuation to pass control to when complete.
// Attempts to authenicate the user identified in `jitsu.config.settings.auth`
//
jitsu.auth = function (callback) {
  if (auth) {
    return callback();
  }
  
  jitsu.users.auth(function (err, success) {
    if (err || !success) {
      winston.error('Unable to Autenticate as ' + jitsu.config.username.magenta);<commit_msg>[fix] Small fix to `jitsu.setup()` to make it more succinct<commit_after>/*
 * jitsu.js: Top-level include for the jitsu module.
 *
 * (C) 2010, Nodejitsu Inc.
 *
 */
 
require.paths.unshift(__dirname);

var jitsu = exports,
    auth = false,
    started = false;

// Failure HTTP Response codes based
// off of `/lib/broodmother/slave/service.js`
var failCodes = jitsu.failCodes = {
  400: "Bad Request",
  403: "Not Authorized",
  404: "Item not found",
  500: "Internal Server Error"
};

// Success HTTP Response codes based
// off of `/lib/broodmother/slave/service.js`
var successCodes = jitsu.successCodes = {
  200: "OK",
  201: "Created"
};

jitsu.utils         = require('jitsu/utils');
jitsu.log           = require('jitsu/log');
jitsu.api           = {};
jitsu.api.Client    = require('jitsu/api/client').Client;
jitsu.api.Apps      = require('jitsu/api/apps').Apps;
jitsu.api.Snapshots = require('jitsu/api/snapshots').Snapshots;
jitsu.api.Users     = require('jitsu/api/users').Users;
jitsu.prompt        = require('jitsu/prompt');
jitsu.config        = require('jitsu/config');
jitsu.commands      = require('jitsu/commands');

var eyes = require('eyes'),
    winston = require('winston'),
    colors = require('colors');

//
// ### function start (command, callback)
// #### @command {string} Command to execute once started
// #### @callback {function} Continuation to pass control to when complete.
// Starts the jitsu CLI and runs the specified command.
//
jitsu.start = function (command, callback) {
  // Setup the initial prompt but don't leave it open
  jitsu.prompt.start().pause();
  
  winston.info('Welcome to ' + 'Nodejitsu'.grey);
  winston.info('It worked if it ends with ' + 'Nodejitsu'.grey + ' ok'.yellow.bold);
  
  jitsu.config.load(function (err) {
    if (!jitsu.config.username) {
      return jitsu.setupUser(function (err) {
        return err ? callback(err) : jitsu.exec(command, callback);
      });
    }
    
    return err ? callback(err) : jitsu.exec(command, callback);
  });
};

//
// ### function exec (command, callback)
// #### @command {string} Command to execute
// #### @callback {function} Continuation to pass control to when complete.
// Runs the specified command in the jitsu CLI.
//
jitsu.exec = function (command, callback) {
  if (!command) {
    winston.error('No command supplied');
    return jitsu.showError(command, {}, true, true);
  }
  
  function execCommand (err) {
    if (err) {
      return callback(err);
    }
    
    winston.info('Executing command ' + jitsu.commands.parse(command).splice(0, 2).join(' ').magenta);
    jitsu.commands.run(command, function (err, shallow) {
      if (err) {
        return jitsu.showError(command, err, shallow);
      }

      //
      // TODO (indexzero): Something here
      //
      callback();
    });
  }
  
  return !started ? jitsu.setup(execCommand) : execCommand();
};

//
// ### function setup (callback)
// #### @callback {function} Continuation to pass control to when complete.
// Sets up the instances of the Resource clients for jitsu and tests
// the username/password combo for the authenticated user.
//
jitsu.setup = function (callback) {
  if (!started) {
    jitsu.users = new jitsu.api.Users(jitsu.config.settings);
    jitsu.apps = new jitsu.api.Apps(jitsu.config.settings);
    jitsu.snapshots = new jitsu.api.Snapshots(jitsu.config.settings);
    started = true;
  }
  
  callback();
};

//
// ### function auth (callback)
// #### @callback {function} Continuation to pass control to when complete.
// Attempts to authenicate the user identified in `jitsu.config.settings.auth`
//
jitsu.auth = function (callback) {
  if (auth) {
    return callback();
  }
  
  jitsu.users.auth(function (err, success) {
    if (err || !success) {
      winston.error('Unable to Autenticate as ' + jitsu.config.username.magenta);
{'fraction_non_alphanumeric': 0.15311317060435975, 'fraction_numerical': 0.00626550058739068, 'mean_word_length': 3.1416216216216215, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1008775', 'n_tokens_mistral': 2592, 'n_tokens_neox': 2434, 'n_words': 635}