hotfix: Added aliases, implemented new runners

This commit is contained in:
2016-12-11 22:36:27 +01:00
parent 8e8cb05674
commit 302e5a50ce
23 changed files with 597 additions and 225 deletions

View File

@ -12,8 +12,10 @@ use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use NoccyLabs\Hotfix\Hotfix\Loader;
use NoccyLabs\Hotfix\Hotfix\Hotfix;
use NoccyLabs\Hotfix\System\Facts;
use NoccyLabs\Hotfix\Runner\RunnerFactory;
use NoccyLabs\Hotfix\Hotfix\HotfixLoader;
class ApplyCommand extends Command
{
@ -26,7 +28,7 @@ class ApplyCommand extends Command
$this->addOption("insecure", "I", InputOption::VALUE_NONE, "Disable signature checks. Don't use unless you know what you are doing!");
$this->addOption("preview", "p", InputOption::VALUE_NONE, "Only preview the hotfix script, don't apply anything");
$this->addArgument("hotfix", InputArgument::OPTIONAL, "The identifier, path or filename of the hotfix");
$this->addArgument("hotfix", InputArgument::OPTIONAL, "The identifier or filename of the hotfix");
}
protected function execute(InputInterface $input, OutputInterface $output)
@ -34,69 +36,45 @@ class ApplyCommand extends Command
$this->output = $output;
$fix = $input->getArgument("hotfix");
$insecure = $input->getOption("insecure");
$loader = new Loader();
if (!$fix) {
$loaders = $loader->getLoaders();
$output->writeln("Supported loaders:\n");
foreach ($loaders as $loader) {
$output->writeln(" * ".$loader->getInfo());
}
$output->writeln("No hotfix specified.");
return;
}
//$loader = new Loader();
$output->writeln("Reading hotfix <comment>{$fix}</comment>...");
try {
$hotfix = $loader->load($fix, $insecure);
$hotfix = HotfixLoader::load($fix);
//$hotfix = $loader->load($fix, $insecure);
} catch (\Exception $e) {
$output->writeln("<error>Error: ".$e->getMessage()."</error>");
return;
}
if (!$hotfix) {
$output->writeln("<error>Could not load hotfix</error>");
$output->writeln("");
$header = $hotfix->getHeader();
$output->writeln(" Hotfix: <fg=yellow;options=bold>".$header->getName()."</fg=yellow;options=bold>");
$output->writeln(" Author: <comment>".$header->getAuthor()."</comment>");
$info = explode("\n",wordwrap(trim($header->getInfo()), 60));
$info = "<comment>".join("</comment>\n <comment>", $info)."</comment>";
$output->writeln(" Info: ".$info);
$output->writeln("");
if (!$this->checkSignature($hotfix, $output)) {
if (!$input->getOption('insecure')) {
$output->writeln("<error>Hotfix can not be authenticated. Aborting!</error>");
return;
}
}
if (!$this->checkRequirements($hotfix, $output)) {
$output->writeln("<error>Error: This hotfix it not compatible with the current distribution</>");
return;
}
$output->writeln("");
if (($signer = $hotfix->getSignedBy())) {
$keyid = $hotfix->getKeyId();
$output->writeln("Hotfix has good signature from <fg=green;options=bold>{$signer}</fg=green;options=bold> (keyid <info>{$keyid}</info>)");
} else {
$output->writeln("<fg=red;options=bold>Warning: Hotfix is not signed or signature not valid!</fg=red;options=bold>");
}
$requires = $hotfix->getRequirements();
if (count($requires)>0) {
$engine = new ExpressionLanguage();
$facts = Facts::getSystemFacts()->getFacts();
while (true) {
$expr = array_shift($requires);
$ret = $engine->evaluate($expr, $facts);
if ($ret) {
//$output->writeln("<fg=green;options=bold>Hotfix is compatible with the current distribution</>");
break;
}
if (count($requires)==0) {
$output->writeln("<error>Error: This hotfix it not compatible with the current distribution</>");
return false;
}
}
//} else {
//$output->writeln("<fg=green;options=bold>Hotfix indicates universal compatibility.</>");
}
$output->writeln("");
$output->writeln(" Hotfix: <fg=yellow;options=bold>".$hotfix->getName()."</fg=yellow;options=bold>");
$output->writeln(" Author: <comment>".$hotfix->getAuthor()."</comment>");
$info = explode("\n",wordwrap(trim($hotfix->getInfo()), 60));
$info = "<comment>".join("</comment>\n <comment>", $info)."</comment>";
$output->writeln(" Info: ".$info);
$output->writeln("");
if ($input->getOption("preview")) {
$output->writeln("This is the script that will be executed:");
$body = $hotfix->getBody();
@ -111,6 +89,7 @@ class ApplyCommand extends Command
if (!$helper->ask($input, $output, $question)) {
return;
}
$output->writeln("\n<info>Applying hotfix...</info>\n");
try {
@ -121,4 +100,39 @@ class ApplyCommand extends Command
$output->writeln("\n<info>Hotfix applied successfully</info>");
}
private function checkSignature(Hotfix $hotfix, OutputInterface $output)
{
$signature = $hotfix->getSignature();
if ($signature->isValid()) {
$keyid = $signature->getKeyId();
$signer = $signature->getSigner();
$output->writeln("Hotfix has good signature from <fg=green;options=bold>{$signer}</fg=green;options=bold> (keyid <info>{$keyid}</info>)");
} else {
$error = $signature->getError();
$output->writeln("<fg=red;options=bold>Warning: {$error}</fg=red;options=bold>");
}
}
private function checkRequirements(Hotfix $hotfix, OutputInterface $output)
{
$requires = $hotfix->getHeader()->getRequirements();
if (count($requires)>0) {
$engine = new ExpressionLanguage();
$facts = Facts::getSystemFacts()->getFacts();
while (true) {
$expr = array_shift($requires);
$ret = $engine->evaluate($expr, $facts);
if ($ret) {
//$output->writeln("<fg=green;options=bold>Hotfix is compatible with the current distribution</>");
break;
}
if (count($requires)==0) {
return false;
}
}
}
return true;
}
}