Fix user requirement in setup, cleanup.

This commit is contained in:
Niklas Keller
2015-12-21 00:01:25 +01:00
parent 5a4ab4a410
commit 37d054975c
6 changed files with 58 additions and 86 deletions

View File

@@ -11,6 +11,17 @@ use Psr\Log\LoggerInterface;
require __DIR__ . "/../vendor/autoload.php";
$commands = [
"setup",
"issue",
"renew",
"revoke",
];
$help = implode("\n ", array_map(function($command) {
return "bin/acme {$command}";
}, $commands));
$help = <<<EOT
____ __________ ___ ___
@@ -21,10 +32,7 @@ $help = <<<EOT
Usage: bin/acme command --args
Available Commands:
bin/acme setup
bin/acme issue
bin/acme revoke
bin/acme renew
{$help}
Get more help by appending --help to specific commands.
@@ -33,48 +41,44 @@ EOT;
$climate = new CLImate;
$injector = new Injector;
$commands = [
"setup",
"revoke",
"issue",
"renew",
];
if (PHP_SAPI !== "phpdbg" && PHP_SAPI !== "cli") {
$climate->error("Please run this script as command line script!");
if (!in_array(PHP_SAPI, ["cli", "phpdbg"], true)) {
$climate->error("Please run this script via CLI!");
exit(1);
}
if (count($argv) === 1 || $argv[1] === "-h" || $argv[1] === "--help" || $argv[1] === "help") {
if (count($argv) === 1 || in_array($argv[1], ["h", "-h", "help", "--help"], true)) {
print $help;
exit(0);
}
if (!in_array($argv[1], $commands)) {
$climate->error("Unknown command: '{$argv[1]}'");
$climate->error("Unknown command '{$argv[1]}', use --help for a list of available commands.");
exit(1);
}
$class = \Kelunik\AcmeClient\commandToClass($argv[1]);
/** @var \Kelunik\AcmeClient\Commands\Command $class */
$class = "Kelunik\\AcmeClient\\Commands\\" . ucfirst($argv[1]);
$definition = $class::getDefinition();
try {
$args = $argv;
unset($args[1]);
$climate->arguments->add($definition);
$climate->arguments->parse();
$climate->arguments->parse(array_values($args));
} catch (Exception $e) {
$climate->usage(["bin/acme {$argv[1]}"]);
$climate->br();
if (count($argv) !== 3 || !in_array($argv[2], ["-h", "--help", "help"])) {
if (count($argv) === 3 && in_array($argv[2], ["h", "-h", "--help", "help"], true)) {
$climate->usage(["bin/acme {$argv[1]}"]);
exit(0);
} else {
$climate->error($e->getMessage());
exit(1);
}
exit(1);
}
if (posix_geteuid() !== 0) {
$climate->error("Please run this script as root!");
if (posix_geteuid() !== 0) { // TODO: Windows?
$climate->error("Please run this script as root.");
exit(1);
}
@@ -91,9 +95,7 @@ $injector->share($logger);
$command = $injector->make($class);
Amp\run(function () use ($command, $climate, $logger) {
try {
yield $command->execute($climate->arguments);
} catch (Throwable $e) {
$handler = function($e) use ($logger) {
$error = (string) $e;
$lines = explode("\n", $error);
$lines = array_filter($lines, function ($line) {
@@ -105,6 +107,14 @@ Amp\run(function () use ($command, $climate, $logger) {
}
exit(1);
};
try {
yield $command->execute($climate->arguments);
} catch (Throwable $e) {
$handler($e);
} catch (Exception $e) {
$handler($e);
}
Amp\stop();