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