|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -use GetOpt\GetOpt; |
4 |
| -use GetOpt\Option; |
5 |
| -use GetOpt\Command; |
6 | 3 | use GetOpt\ArgumentException;
|
7 | 4 | use GetOpt\ArgumentException\Missing;
|
| 5 | +use GetOpt\Command; |
| 6 | +use GetOpt\GetOpt; |
| 7 | +use GetOpt\Option; |
| 8 | +use OvhCli\Cli; |
| 9 | +use OvhCli\Ovh; |
8 | 10 | use Phpfastcache\CacheManager;
|
9 | 11 | use Phpfastcache\Config\Config as CacheConfig;
|
10 |
| -use Phpfastcache\Core\phpFastCache; |
11 |
| -use OvhCli\Ovh; |
12 |
| -use OvhCli\Cli; |
13 | 12 |
|
14 |
| -require_once __DIR__ . '/vendor/autoload.php'; |
| 13 | +require_once __DIR__.'/vendor/autoload.php'; |
15 | 14 |
|
16 | 15 | error_reporting(E_ALL ^ E_NOTICE);
|
17 |
| -define('CONFIG_FILE', getenv("HOME").'/.ovh-cli.config.json'); |
18 |
| -define('COMMAND_PATH', __DIR__ . '/src/Command'); |
| 16 | +define('CONFIG_FILE', getenv('HOME').'/.ovh-cli.config.json'); |
| 17 | +define('COMMAND_PATH', __DIR__.'/src/Command'); |
19 | 18 |
|
20 | 19 | // set cache
|
21 | 20 | CacheManager::setDefaultConfig(new CacheConfig([
|
22 |
| - "path" => sys_get_temp_dir(), |
23 |
| - "itemDetailedDate" => false |
| 21 | + 'path' => sys_get_temp_dir(), |
| 22 | + 'itemDetailedDate' => false, |
24 | 23 | ]));
|
25 | 24 |
|
26 | 25 | $cacheManager = CacheManager::getInstance('files');
|
27 | 26 | Ovh::setCacheManager($cacheManager);
|
28 | 27 |
|
29 | 28 | $getOpt = new GetOpt();
|
30 | 29 | $getOpt->addOptions([
|
31 |
| - Option::create('?', 'help', GetOpt::NO_ARGUMENT) |
32 |
| - ->setDescription('Show this help and quit'), |
33 |
| - Option::create('t', 'dry-run', GetOpt::NO_ARGUMENT) |
34 |
| - ->setDescription('Will fake PUT/POST/DELETE requests'), |
35 |
| - Option::create('g', 'grep', GetOpt::NO_ARGUMENT) |
36 |
| - ->setDescription('Greppable output'), |
37 |
| - Option::create('n', 'no-cache', GetOpt::NO_ARGUMENT) |
38 |
| - ->setDescription('Disable cache') |
| 30 | + Option::create('?', 'help', GetOpt::NO_ARGUMENT) |
| 31 | + ->setDescription('Show this help and quit'), |
| 32 | + Option::create('t', 'dry-run', GetOpt::NO_ARGUMENT) |
| 33 | + ->setDescription('Will fake PUT/POST/DELETE requests'), |
| 34 | + Option::create('g', 'grep', GetOpt::NO_ARGUMENT) |
| 35 | + ->setDescription('Grep-friendly output'), |
| 36 | + Option::create('n', 'no-cache', GetOpt::NO_ARGUMENT) |
| 37 | + ->setDescription('Disable cache'), |
39 | 38 | ]);
|
40 | 39 |
|
41 | 40 | $commands = [];
|
42 | 41 | // glob() doesn't work with PHAR, so use iterators
|
43 | 42 | $directory = new \RecursiveIteratorIterator(
|
44 |
| - new \RecursiveDirectoryIterator(COMMAND_PATH) |
| 43 | + new \RecursiveDirectoryIterator(COMMAND_PATH) |
45 | 44 | );
|
46 | 45 | // commands autoloading
|
47 |
| -foreach($directory as $file) { |
48 |
| - if (!preg_match('/\.php$/', $file)) { |
49 |
| - continue; |
50 |
| - } |
51 |
| - $relativePath = substr($file, strlen(COMMAND_PATH) + 1); |
52 |
| - $classSuffix = str_replace(['.php',DIRECTORY_SEPARATOR], ['','\\'], $relativePath); |
53 |
| - $class = '\\OvhCli\\Command\\' . $classSuffix; |
54 |
| - $command = new $class; |
55 |
| - $name = $command->getName(); |
56 |
| - $commands[$name] = $command; |
| 46 | +foreach ($directory as $file) { |
| 47 | + if (!preg_match('/\.php$/', $file)) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + $relativePath = substr($file, strlen(COMMAND_PATH) + 1); |
| 51 | + $classSuffix = str_replace(['.php', DIRECTORY_SEPARATOR], ['', '\\'], $relativePath); |
| 52 | + $class = '\\OvhCli\\Command\\'.$classSuffix; |
| 53 | + $command = new $class(); |
| 54 | + $name = $command->getName(); |
| 55 | + $commands[$name] = $command; |
57 | 56 | }
|
58 | 57 | // I like commands to be in alphabetical order :)
|
59 | 58 | ksort($commands);
|
60 | 59 | $getOpt->addCommands($commands);
|
61 | 60 |
|
62 | 61 | try {
|
63 |
| - try { |
64 |
| - $getOpt->process(); |
65 |
| - } catch (Missing $exception) { |
66 |
| - if (!$getOpt->getOption('help')) { |
67 |
| - throw $exception; |
68 |
| - } |
| 62 | + try { |
| 63 | + $getOpt->process(); |
| 64 | + } catch (Missing $exception) { |
| 65 | + if (!$getOpt->getOption('help')) { |
| 66 | + throw $exception; |
69 | 67 | }
|
| 68 | + } |
70 | 69 | } catch (ArgumentException $exception) {
|
71 |
| - file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL); |
72 |
| - echo PHP_EOL . $getOpt->getHelpText(); |
73 |
| - exit; |
| 70 | + file_put_contents('php://stderr', $exception->getMessage().PHP_EOL); |
| 71 | + echo PHP_EOL.$getOpt->getHelpText(); |
| 72 | + |
| 73 | + exit; |
74 | 74 | }
|
75 | 75 |
|
76 | 76 | // show help and quit
|
77 | 77 | $command = $getOpt->getCommand();
|
78 | 78 | if (!$command || $getOpt->getOption('help')) {
|
79 |
| - echo $getOpt->getHelpText(); |
80 |
| - exit; |
| 79 | + echo $getOpt->getHelpText(); |
| 80 | + |
| 81 | + exit; |
81 | 82 | }
|
82 | 83 |
|
83 | 84 | if ($getOpt->getOption('dry-run')) {
|
84 |
| - Ovh::setDryRun(true); |
85 |
| - Cli::warning("running in DRY-RUN mode"); |
| 85 | + Ovh::setDryRun(true); |
| 86 | + Cli::warning('running in DRY-RUN mode'); |
86 | 87 | }
|
87 | 88 |
|
88 | 89 | if ($getOpt->getOption('no-cache')) {
|
89 |
| - Ovh::disableCache(); |
90 |
| - Cli::warning("cache is disabled"); |
| 90 | + Ovh::disableCache(); |
| 91 | + Cli::warning('cache is disabled'); |
91 | 92 | }
|
92 | 93 |
|
93 | 94 | // call the requested command
|
|
0 commit comments