hotfix: Updated facts, added facts subcommand

This commit is contained in:
Chris 2016-12-11 16:13:33 +01:00
parent ac07dd3752
commit 2f1628b7d8
3 changed files with 77 additions and 4 deletions

View File

@ -0,0 +1,33 @@
<?php
namespace NoccyLabs\Hotfix\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
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\System\Facts;
class FactsCommand extends Command
{
protected function configure()
{
$this->setName("facts");
$this->setDescription("List the facts as collected on this system");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$facts = Facts::getSystemFacts()->getFlat();
print_r($facts);
}
}

View File

@ -11,6 +11,7 @@ class HotfixApplication extends Application
parent::__construct("Hotfix", APP_VERSION.(defined('PHAR_BUILD_DATE')?" (".PHAR_BUILD_DATE.")":""));
$this->add(new Command\ApplyCommand());
$this->add(new Command\SignCommand());
$this->add(new Command\FactsCommand());
}

View File

@ -21,10 +21,19 @@ class Facts
public function read()
{
$facts = [];
$facts['lsb'] = $this->readLsbInfo();
$facts['system'] = $this->readSystemInfo();
$this->facts = $facts;
}
private function readLsbInfo()
{
$has_lsb_release = exec("which lsb_release");
if ($has_lsb_release) {
exec("lsb_release -idrcs", $output);
$facts['lsb'] = (object)[
$facts = (object)[
'id' => strtolower($output[0]),
'description' => $output[1],
'release' => $output[2],
@ -32,15 +41,26 @@ class Facts
];
} elseif (file_exists("/etc/os-release")) {
$ini = parse_ini_file("/etc/os-release");
$facts['lsb'] = (object)[
$facts = (object)[
'id' => strtolower(@$ini['ID']),
'description' => @$ini['PRETTY_NAME'],
'release' => @$ini['VERSION_ID'],
'codename' => @$ini['VERSION_CODENAME']
];
}
$this->facts = $facts;
return $facts;
}
private function readSystemInfo()
{
$facts = (object)[
'bits' => trim(exec('getconf LONG_BIT')),
'arch' => trim(exec('uname -m')),
];
return $facts;
}
public function getFacts()
@ -48,4 +68,23 @@ class Facts
return $this->facts;
}
public function getFlat()
{
return $this->flatten($this->facts, []);
}
private function flatten($facts, array $path)
{
$ret = [];
foreach ((array)$facts as $fact=>$value) {
$sub = array_merge($path, [ $fact ]);
if (is_object($value)) {
$ret = array_merge($ret, $this->flatten($value, $sub));
} else {
$ret[join(".",$sub)] = $value;
}
}
return $ret;
}
}