/**
  * Get argument on command line
  * @return Getopt 
  */
 public function getGetopt()
 {
     if (!$this->getopt) {
         $this->getopt = @new Getopt($this->config, null, array(Getopt::CONFIG_FREEFORM_FLAGS => true));
         // let use of more argument
         $this->getopt->parse();
     }
     return $this->getopt;
 }
 /**
  * @see	\wcf\system\cli\command\ICLICommand::execute()
  */
 public function execute(array $parameters)
 {
     $this->argv->setArguments($parameters);
     $this->argv->parse();
     $args = $this->argv->getRemainingArgs();
     if (count($args) != 1 || $args[0] != 'execute') {
         throw new ArgvException('', $this->getUsage());
     }
     CronjobScheduler::getInstance()->executeCronjobs();
 }
Example #3
0
 /**
  * Calls all init functions of the WCF class.
  */
 public function __construct()
 {
     // add autoload directory
     self::$autoloadDirectories['wcf'] = WCF_DIR . 'lib/';
     // define tmp directory
     if (!defined('TMP_DIR')) {
         define('TMP_DIR', FileUtil::getTempFolder());
     }
     // register additional autoloaders
     require_once WCF_DIR . 'lib/system/api/phpline/phpline.phar';
     require_once WCF_DIR . 'lib/system/api/zend/Loader/StandardAutoloader.php';
     $zendLoader = new ZendLoader(array(ZendLoader::AUTOREGISTER_ZF => true));
     $zendLoader->register();
     $argv = new ArgvParser(array('packageID=i' => ''));
     $argv->setOption(ArgvParser::CONFIG_FREEFORM_FLAGS, true);
     $argv->parse();
     define('PACKAGE_ID', $argv->packageID ?: 1);
     // disable benchmark
     define('ENABLE_BENCHMARK', 0);
     // start initialization
     $this->initDB();
     $this->loadOptions();
     $this->initSession();
     $this->initLanguage();
     $this->initTPL();
     $this->initCoreObjects();
     $this->initApplications();
     // the destructor registered in core.functions.php will only call the destructor of the parent class
     register_shutdown_function(array('wcf\\system\\CLIWCF', 'destruct'));
     $this->initArgv();
     $this->initPHPLine();
     $this->initAuth();
     $this->checkForUpdates();
     $this->initCommands();
 }
Example #4
0
 /**
  * @see	\wcf\system\cli\command\ICLICommand::execute()
  */
 public function execute(array $parameters)
 {
     $this->argv->setArguments($parameters);
     $this->argv->parse();
     $args = $this->argv->getRemainingArgs();
     // validate parameters
     if (count($args) != 1) {
         throw new ArgvException('', $this->getUsage());
     }
     $commands = CLICommandHandler::getCommands();
     if (!isset($commands[$args[0]])) {
         throw new ArgvException(CLIWCF::getLanguage()->getDynamicVariable('wcf.cli.error.command.notFound', array('command' => $args[0])), $this->getUsage());
     }
     $command = $commands[$args[0]];
     if (!$command instanceof IArgumentedCLICommand) {
         throw new ArgvException(CLIWCF::getLanguage()->getDynamicVariable('wcf.cli.error.help.noArguments', array('command' => $args[0])), $this->getUsage());
     }
     CLIWCF::getReader()->println($command->getUsage());
 }
 /**
  * @see	\wcf\system\cli\command\ICLICommand::execute()
  */
 public function execute(array $parameters)
 {
     $this->argv->setArguments($parameters);
     $this->argv->parse();
     if (count($this->argv->getRemainingArgs()) !== 2) {
         throw new ArgvException('', $this->fixUsage($this->argv->getUsageMessage()));
     }
     list($action, $package) = $this->argv->getRemainingArgs();
     CLIWCF::getReader()->setHistoryEnabled(false);
     switch ($action) {
         case 'install':
             $this->install($package);
             break;
         case 'uninstall':
             $this->uninstall($package);
             break;
         default:
             throw new ArgvException('', $this->fixUsage($this->argv->getUsageMessage()));
             break;
     }
 }
        exit(2);
    }
} else {
    // Try to load StandardAutoloader from library
    if (false === (include $libPath . '/Zend/Loader/StandardAutoloader.php')) {
        echo "Unable to locate autoloader via library; aborting" . PHP_EOL;
        exit(2);
    }
}
// Setup autoloading
$loader = new StandardAutoloader(array('autoregister_zf' => true));
$loader->register();
$rules = array('help|h' => 'Get usage message', 'library|l-s' => 'Library to parse; if none provided, assumes current directory', 'output|o-s' => 'Where to write plugin map file; if not provided, assumes "plugin_classmap.php" in library directory', 'append|a' => 'Append to plugin map file if it exists', 'overwrite|w' => 'Whether or not to overwrite existing autoload file');
try {
    $opts = new Console\Getopt($rules);
    $opts->parse();
} catch (Console\Exception\RuntimeException $e) {
    echo $e->getUsageMessage();
    exit(2);
}
if ($opts->getOption('h')) {
    echo $opts->getUsageMessage();
    exit;
}
$path = $libPath;
if (array_key_exists('PWD', $_SERVER)) {
    $path = $_SERVER['PWD'];
}
if (isset($opts->l)) {
    $libraryPath = $opts->l;
    $libraryPath = rtrim($libraryPath, '/\\') . DIRECTORY_SEPARATOR;
Example #7
0
    public function testGetoptRaiseExceptionForNumericOptionsIfAneHandlerIsSpecified()
    {
        $opts = new Getopt(
            array('lines=s' => 'Lines-option'),
            array('other', 'arguments', '-5'),
            array(Getopt::CONFIG_NUMERIC_FLAGS => true)
        );

        $this->setExpectedException('\Zend\Console\Exception\RuntimeException');
        $opts->parse();
    }
Example #8
0
 /**
  * @param array $argv
  * @throws \Zend\Console\Exception\RuntimeException
  */
 public function parseGetOpt(array $argv)
 {
     $rules = array('h|help' => 'show this help', 'l|lines=i' => 'output the last N lines of a stacktrace. Default: 100', 'line-length=i' => 'maximum length of a line. Default 512', 'process-name=s' => 'name of running php processes. Default: autodetect', 'live' => 'search while running for new upcoming pid\'s', 'o|output=s' => 'output log to file');
     $opts = new Console\Getopt($rules, $argv);
     $opts->parse();
     if ($opts->help) {
         throw new Console\Exception\RuntimeException('', $opts->getUsageMessage());
     }
     if ($opts->getOption('lines')) {
         $this->getStrace()->setLines(min(1000, max(1, $opts->getOption('lines'))));
     }
     if ($opts->getOption('line-length')) {
         $this->getStrace()->setLineLength(min(1 * 1024 * 1024, max(10, $opts->getOption('line-length'))));
     }
     if ($opts->getOption('process-name')) {
         $this->getProcessStatus()->setProcessName($opts->getOption('process-name'));
     }
     if ($opts->getOption('output')) {
         $fileOutput = new FileOutput($opts->getOption('output'));
         $this->getCommandLine()->attachObserver($fileOutput, 'stdout');
         $this->getCommandLine()->attachObserver($fileOutput, 'stderr');
     }
     if ($opts->getOption('live')) {
         $this->setLive(true);
     }
 }
<?php

use Zend\Console\Getopt;
use Zend\Console\Console;
use Zend\Console\ColorInterface as Color;
use Aws\Ec2\Ec2Client;
use CbAws\Ec2\Ec2ClientHelper;
require __DIR__ . '/vendor/autoload.php';
$console = Console::getInstance();
$opt = new Getopt(array('key|O-s' => 'AWS access key', 'secret|W-s' => 'AWS secret key', 'instance|i-s' => 'Instance to backup', 'region|r-s' => 'Region of instance', 'exclude|e-s' => 'Exclude volumes/devices, comma delimited', 'dry-run|d' => 'Dry run, do not delete anything'));
$opt->parse();
try {
    $key = empty($opt->key) ? getenv('AWS_ACCESS_KEY') : $opt->key;
    $secret = empty($opt->secret) ? getenv('AWS_SECRET_KEY') : $opt->secret;
    $region = empty($opt->region) ? getenv('AWS_REGION') : $opt->region;
    $instanceId = $opt->instance;
    $exclusions = empty($opt->exclude) ? array() : array_map('trim', explode(',', $opt->exclude));
    $dryRun = (bool) $opt->{'dry-run'};
    $client = Ec2Client::factory(array('key' => $key, 'secret' => $secret, 'region' => $region));
    $clientHelper = new Ec2ClientHelper($client);
    if (empty($instanceId)) {
        $hostname = gethostname() . '.';
        $instance = $clientHelper->getInstanceByHostname($hostname);
        $instanceId = $instance['InstanceId'];
    } else {
        $instance = $clientHelper->getInstanceById($instanceId);
    }
    $instanceName = $clientHelper->resolveInstanceName($instance);
    $console->writeLine("Creating snapshots of {$instanceName}");
    $volumes = $clientHelper->getVolumesByInstance($instanceId);
    if (count($volumes) === 0) {
Example #10
0
 /**
  * @group ZF-5345
  */
 public function testUsingDashWithoutOptionNotAsLastArgumentThrowsException()
 {
     $opts = new Getopt("abp:", array("-", "file1"));
     $this->setExpectedException('\\Zend\\Console\\Exception\\RuntimeException');
     $opts->parse();
 }
Example #11
0
 /**
  * @see	\wcf\system\cli\command\ICLICommand::execute()
  */
 public function execute(array $parameters)
 {
     $this->argv->setArguments($parameters);
     $this->argv->parse();
     if ($this->argv->list) {
         CLIWCF::getReader()->println(CLIUtil::generateTable($this->generateList()));
         return;
     }
     $args = $this->argv->getRemainingArgs();
     // validate parameters
     if (count($args) != 1) {
         throw new ArgvException('', $this->getUsage());
     }
     $class = $args[0];
     // assume wcf\system\worker when no FQN is given
     if (strpos($class, '\\') === false) {
         $class = 'wcf\\system\\worker\\' . $class;
     }
     $invalid = false;
     if (!class_exists($class)) {
         $invalid = true;
     } else {
         $reflection = new \ReflectionClass($class);
         if (!$reflection->isInstantiable()) {
             $invalid = true;
         } else {
             if (!ClassUtil::isInstanceOf($class, 'wcf\\system\\worker\\IWorker')) {
                 $invalid = true;
             }
         }
     }
     if ($invalid) {
         throw new ArgvException("Invalid worker '" . $class . "' given", $this->getUsage());
     }
     // parse parameters
     $options = $this->argv->getOptions();
     $parameters = array();
     foreach ($options as $option) {
         $value = $this->argv->getOption($option);
         if ($option === 'setParameter') {
             if (!is_array($value)) {
                 $value = array($value);
             }
             foreach ($value as $parameter) {
                 list($parameterKey, $parameterValue) = explode('=', $parameter);
                 $parameters[$parameterKey] = $parameterValue;
             }
         } else {
             $parameters[$option] = $value;
         }
     }
     $worker = new $class($parameters);
     $worker->validate();
     $worker->getProgress();
     // make sure objects are counted
     // initialize progressbar
     $progressbar = new ProgressBar(new ConsoleProgressBar(array('width' => CLIWCF::getTerminal()->getWidth())));
     $progress = 0;
     for ($i = 0; $progress < 100; $i++) {
         $worker->setLoopCount($i);
         $worker->validate();
         // execute worker
         $worker->execute();
         // update progress
         $progress = $worker->getProgress();
         $progressbar->update($progress);
     }
     $progressbar->update($progress);
     $progressbar->getAdapter()->finish();
 }
Example #12
0
 /**
  * @expectedException \Zend\Console\Exception\RuntimeException
  * @expectedExceptionMessage The option x is invalid. See usage.
  */
 public function testOptionCallbackReturnsFallsAndThrowException()
 {
     $opts = new Getopt('x', array('-x'));
     $opts->setOptionCallback('x', function () {
         return false;
     });
     $opts->parse();
 }