Files
acmeclient/bin/acme
2015-12-20 21:49:23 +01:00

112 lines
2.4 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
use Auryn\Injector;
use Bramus\Monolog\Formatter\ColoredLineFormatter;
use Kelunik\AcmeClient\LoggerColorScheme;
use League\CLImate\CLImate;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
require __DIR__ . "/../vendor/autoload.php";
$help = <<<EOT
____ __________ ___ ___
/ __ `/ ___/ __ `__ \/ _ \
/ /_/ / /__/ / / / / / __/
\__,_/\___/_/ /_/ /_/\___/
Usage: bin/acme command --args
Available Commands:
bin/acme setup
bin/acme issue
bin/acme revoke
bin/acme renew
Get more help by appending --help to specific commands.
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!");
exit(1);
}
if (count($argv) === 1 || $argv[1] === "-h" || $argv[1] === "--help" || $argv[1] === "help") {
print $help;
exit(0);
}
if (!in_array($argv[1], $commands)) {
$climate->error("Unknown command: '{$argv[1]}'");
exit(1);
}
$class = \Kelunik\AcmeClient\commandToClass($argv[1]);
$definition = $class::getDefinition();
try {
$climate->arguments->add($definition);
$climate->arguments->parse();
} catch (Exception $e) {
$climate->usage(["bin/acme {$argv[1]}"]);
$climate->br();
if (count($argv) !== 3 || !in_array($argv[2], ["-h", "--help", "help"])) {
$climate->error($e->getMessage());
}
exit(1);
}
if (posix_geteuid() !== 0) {
$climate->error("Please run this script as root!");
exit(1);
}
$handler = new StreamHandler("php://stdout", Logger::DEBUG);
$handler->setFormatter(new ColoredLineFormatter(new LoggerColorScheme, null, null, true, true));
$logger = new Logger("ACME");
$logger->pushHandler($handler);
$injector->alias(LoggerInterface::class, Logger::class);
$injector->share($logger);
$command = $injector->make($class);
Amp\run(function () use ($command, $climate, $logger) {
try {
yield $command->execute($climate->arguments);
} catch (Throwable $e) {
$error = (string) $e;
$lines = explode("\n", $error);
$lines = array_filter($lines, function ($line) {
return strlen($line) && $line[0] !== "#" && $line !== "Stack trace:";
});
foreach ($lines as $line) {
$logger->error($line);
}
exit(1);
}
Amp\stop();
});