Skip to content

Commit

Permalink
Updated CLI.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Parker committed Oct 10, 2016
1 parent f5fda78 commit 19bbe65
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 94 deletions.
180 changes: 117 additions & 63 deletions app/src/Core/CLI/ETSIS-CLI/etsis_CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,47 @@
*
* @package etsis-cli
*/
class ETSIS_CLI {
class ETSIS_CLI
{

private static $commands = array();

/**
* Splits a string into positional and associative arguments.
*
* @param string
* @return array
*/
static function parse_args( $arguments ) {
public static function parse_args($arguments)
{
$regular_args = array();
$assoc_args = array();

foreach ( $arguments as $arg ) {
if ( preg_match( '|^--([^=]+)$|', $arg, $matches ) ) {
$assoc_args[ $matches[1] ] = true;
} elseif ( preg_match( '|^--([^=]+)=(.+)|', $arg, $matches ) ) {
$assoc_args[ $matches[1] ] = $matches[2];
foreach ($arguments as $arg) {
if (preg_match('|^--([^=]+)$|', $arg, $matches)) {
$assoc_args[$matches[1]] = true;
} elseif (preg_match('|^--([^=]+)=(.+)|', $arg, $matches)) {
$assoc_args[$matches[1]] = $matches[2];
} else {
$regular_args[] = $arg;
}
}

return array( $regular_args, $assoc_args );
return array($regular_args, $assoc_args);
}

/**
* Load dependencies or exit with an error message if not found
*
*/
static function load_dependencies() {
public static function load_dependencies()
{
$has_autoload = false;

if ( file_exists( ETSIS_CLI_ROOT . '/src/php/vendor/autoload.php' ) ) {
if (file_exists(ETSIS_CLI_ROOT . '/src/php/vendor/autoload.php')) {
require_once ETSIS_CLI_ROOT . '/src/php/vendor/autoload.php';
} else {
fputs( STDERR, "Error: Can't find required libraries. Install using Composer.\n" );
fputs(STDERR, "Error: Can't find required libraries. Install using Composer.\n");
exit(1);
}
}
Expand All @@ -53,72 +56,77 @@ static function load_dependencies() {
* @param array $arguments
* @param array $assoc_args
*/
static function run_command( $arguments, $assoc_args ) {
if ( empty( $arguments ) ) {
public static function run_command($arguments, $assoc_args)
{
if (empty($arguments)) {
$command = 'help';
} else {
$command = array_shift( $arguments );
$command = array_shift($arguments);
}

define( 'ETSIS_CLI_COMMAND', $command );

$implementation = self::load_command( $command );
$instance = new $implementation( $arguments, $assoc_args );
define('ETSIS_CLI_COMMAND', $command);

$implementation = self::load_command($command);
$instance = new $implementation($arguments, $assoc_args);
}

/**
* Load a command, prior to running it the first time
*
* @param string $command
* @return
*/
static function load_command( $command ) {
if ( !isset( ETSIS_CLI::$commands[$command] ) ) {
public static function load_command($command)
{
if (!isset(ETSIS_CLI::$commands[$command])) {
$path = ETSIS_CLI_ROOT . "/php/commands/$command.php";
if ( is_readable( $path ) ) {
if (is_readable($path)) {
include $path;
}
}
if ( !isset( ETSIS_CLI::$commands[$command] ) ) {
ETSIS_CLI::error( "'$command' is not a registered etsis command. See 'etsis help'." );

if (!isset(ETSIS_CLI::$commands[$command])) {
ETSIS_CLI::error("'$command' is not a registered etsis command. See 'etsis help'.");
exit(1);
}

return ETSIS_CLI::$commands[$command];
}

static function load_all_commands() {
foreach ( glob( ETSIS_CLI_ROOT . "/php/commands/*.php" ) as $filename ) {
$command = substr( basename( $filename ), 0, -4 );
public static function load_all_commands()
{
foreach (glob(ETSIS_CLI_ROOT . "/php/commands/*.php") as $filename) {
$command = substr(basename($filename), 0, -4);

if ( isset( self::$commands[ $command ] ) )
if (isset(self::$commands[$command]))
continue;

include $filename;
}

return self::$commands;
}
return self::$commands;
}


/**
* Add a command to the list of commands
*
* @param string $name The name of the command that will be used in the cli
* @param string $class The class to manage the command
*/
static function add_command( $name, $class ) {
self::$commands[ $name ] = $class;
public static function add_command($name, $class)
{
self::$commands[$name] = $class;
}

/**
* Display a message in the cli
*
* @param string $message
*/
static function out( $message ) {
if ( ETSIS_CLI_QUIET ) return;
public static function out($message)
{
if (ETSIS_CLI_QUIET)
return;
\cli\out($message);
}

Expand All @@ -127,8 +135,10 @@ static function out( $message ) {
*
* @param string $message
*/
static function line( $message = '' ) {
if ( ETSIS_CLI_QUIET ) return;
public static function line($message = '')
{
if (ETSIS_CLI_QUIET)
return;
\cli\line($message);
}

Expand All @@ -138,8 +148,9 @@ static function line( $message = '' ) {
* @param string $message
* @param string $label
*/
static function error( $message, $label = 'Error' ) {
\cli\err( '%R' . $label . ': %n' . $message );
public static function error($message, $label = 'Error')
{
\cli\err('%R' . $label . ': %n' . $message);
exit(1);
}

Expand All @@ -149,10 +160,11 @@ static function error( $message, $label = 'Error' ) {
* @param string $message
* @param string $label
*/
static function success( $message, $label = 'Success' ) {
if ( ETSIS_CLI_QUIET )
public static function success($message, $label = 'Success')
{
if (ETSIS_CLI_QUIET)
return;
\cli\line( '%G' . $label . ': %n' . $message );
\cli\line('%G' . $label . ': %n' . $message);
}

/**
Expand All @@ -161,10 +173,11 @@ static function success( $message, $label = 'Success' ) {
* @param string $message
* @param string $label
*/
static function warning( $message, $label = 'Warning' ) {
if ( ETSIS_CLI_QUIET )
public static function warning($message, $label = 'Warning')
{
if (ETSIS_CLI_QUIET)
return;
\cli\err( '%C' . $label . ': %n' . $message );
\cli\err('%C' . $label . ': %n' . $message);
}

/**
Expand All @@ -173,40 +186,81 @@ static function warning( $message, $label = 'Warning' ) {
* @param array List of required arg names
* @param array Passed args
*/
static function check_required_args( $required, $assoc_args ) {
public static function check_required_args($required, $assoc_args)
{
$missing = false;

foreach ( $required as $arg ) {
if ( !isset( $assoc_args[ $arg ] ) ) {
ETSIS_CLI::warning( "--$arg parameter is missing" );
foreach ($required as $arg) {
if (!isset($assoc_args[$arg])) {
ETSIS_CLI::warning("--$arg parameter is missing");
$missing = true;
} elseif ( true === $assoc_args[ $arg ] ) {
} elseif (true === $assoc_args[$arg]) {
// passed as a flag
ETSIS_CLI::warning( "--$arg needs to have a value" );
ETSIS_CLI::warning("--$arg needs to have a value");
$missing = true;
}
}

if ( $missing )
if ($missing)
exit(1);
}

static function get_numeric_arg( $args, $index, $name ) {
if ( ! isset( $args[$index] ) ) {
ETSIS_CLI::error( "$name required" );
public static function get_numeric_arg($args, $index, $name)
{
if (!isset($args[$index])) {
ETSIS_CLI::error("$name required");
}

if ( ! is_numeric( $args[$index] ) ) {
ETSIS_CLI::error( "$name must be numeric" );
if (!is_numeric($args[$index])) {
ETSIS_CLI::error("$name must be numeric");
}

return $args[$index];
}

static function progress_bar( $message, $count ) {
if ( \cli\Shell::isPiped() )
public static function progress_bar($message, $count)
{
if (\cli\Shell::isPiped())
return new \ETSIS_CLI\NoOp;

return new \cli\progress\Bar( $message, $count );
return new \cli\progress\Bar($message, $count);
}

public static function getCurrentRelease()
{
$update = new \VisualAppeal\AutoUpdate('app/tmp', 'app/tmp', 1800);
$update->setCurrentVersion(trim(file_get_contents('RELEASE')));
$update->setUpdateUrl('http://etsis.s3.amazonaws.com/core/1.1/update-check');
$update->addLogHandler(new Monolog\Handler\StreamHandler('app/tmp/logs/core-update.' . date('m-d-Y') . '.txt'));
$update->setCache(new Desarrolla2\Cache\Adapter\File('app/tmp/cache'), 3600);
if ($update->checkUpdate() !== false) {
if ($update->newVersionAvailable()) {
return $update->getLatestVersion();
}
}
}

public static function checkExternalFile($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // this will follow redirects
curl_exec($ch);
$retCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

return $retCode;
}

public static function getDownload($release, $url)
{
$fh = fopen($release, 'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // this will follow redirects
curl_exec($ch);
curl_close($ch);
fclose($fh);
}
}
4 changes: 2 additions & 2 deletions app/src/Core/CLI/ETSIS-CLI/etsis_CLI_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct( $args, $assoc_args ) {
}
}

static function describe_command( $class, $command ) {
public static function describe_command( $class, $command ) {
if ( method_exists( $class, 'help' ) ) {
$class::help();
return;
Expand All @@ -59,7 +59,7 @@ static function describe_command( $class, $command ) {
* @param string $class
* @return array The list of methods
*/
static function get_subcommands( $class ) {
public static function get_subcommands( $class ) {
$reflection = new ReflectionClass( $class );

$methods = array();
Expand Down
6 changes: 4 additions & 2 deletions app/src/Core/CLI/etsis-cli.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
header('Access-Control-Allow-Origin: *');

if ( 'cli' !== PHP_SAPI ) {
echo "This is CLI only.\n";
Expand All @@ -15,10 +16,10 @@
error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', 'Off');
ini_set('log_errors', 'On');
ini_set('error_log', 'app/tmp/logs/cli.' . date('m-d-Y') . '.txt');
ini_set('error_log', 'app/tmp/logs/etsis-cli.' . date('m-d-Y') . '.txt');

define( 'ETSIS_CLI_ROOT', __DIR__ );
define( 'ETSIS_CLI_VERSION', '1.0.0-beta' );
define( 'ETSIS_CLI_VERSION', '1.0.0' );
// Constant that can be used to check if we are running etsis-cli or not
define( 'ETSIS_CLI', true );

Expand All @@ -30,6 +31,7 @@
require_once ETSIS_CLI_ROOT . '/ETSIS-CLI/etsis_CLI_Command.php';
require_once ETSIS_CLI_ROOT . '/php/Mysqldump.php';
require_once ETSIS_CLI_ROOT . '/php/notify.php';
require_once 'app/src/vendor/autoload.php';

// Load dependencies
ETSIS_CLI::load_dependencies();
Expand Down
10 changes: 0 additions & 10 deletions app/src/Core/CLI/php/arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@
}
}

// Global parameter : --dir
if ( isset( $assoc_args['dir'] ) ) {
if( file_exists( $assoc_args['dir'] ) ) {
define( 'ETSIS_DIR', rtrim( $assoc_args['dir'], '/' ) );
unset( $assoc_args['dir'] );
} else {
ETSIS_CLI::error( sprintf( 'The directory "%s" does not exist. It must be created first.', $assoc_args['dir'] ) ) ;
}
}

// Global parameter : --path
if ( !empty( $assoc_args['path'] ) ) {
define( 'ETSIS_ROOT', rtrim( $assoc_args['path'], '/' ) );
Expand Down
Loading

0 comments on commit 19bbe65

Please sign in to comment.