feat: Add a logger and enable and options

Closes #26
This commit is contained in:
jubianchi 2018-01-20 10:18:40 +01:00
parent deac235afe
commit 96ee45edc9
No known key found for this signature in database
GPG key ID: 5D9C896D2AA9E390
3 changed files with 41 additions and 3 deletions

View file

@ -1,5 +1,6 @@
# `1.2.0` (unreleased)
* Add a logger and enable `-v` and `-vv` options
* Add the `--no-networks` and `--no-ports` options to avoid rendering networks and ports
* Add the `--background` option to set the graph's background color
* Versions correctly merged and checked

View file

@ -30,6 +30,7 @@ $application->register('render')
throw new Console\Exception\InvalidArgumentException(sprintf('Invalid background color "%s". It must be a valid hex color or "transparent".', $backgroundColor));
}
$logger = logger($output);
$inputFile = $input->getArgument('input-file');
$inputFileExtension = pathinfo($inputFile, PATHINFO_EXTENSION);
$overrideFile = dirname($inputFile).DIRECTORY_SEPARATOR.basename($inputFile, '.'.$inputFileExtension).'.'.$input->getOption('override').'.'.$inputFileExtension;
@ -52,10 +53,12 @@ $application->register('render')
}
}
$logger(sprintf('Reading <comment>configuration</comment> from <info>"%s"</info>', $inputFile));
$configuration = readConfiguration($inputFile);
$configurationVersion = (string) ($configuration['version'] ?? 1);
if (!$input->getOption('ignore-override') && file_exists($overrideFile)) {
$logger(sprintf('Reading <comment>override</comment> from <info>"%s"</info>', $overrideFile));
$override = readConfiguration($overrideFile);
$overrideVersion = (string) ($override['version'] ?? 1);
@ -64,14 +67,26 @@ $application->register('render')
}
$configuration = array_merge_recursive($configuration, $override);
$logger(sprintf('Configuration <comment>version</comment> is <info>"%s"</info>', $configurationVersion), Console\Output\OutputInterface::VERBOSITY_VERY_VERBOSE);
$configuration['version'] = $configurationVersion;
}
$logger('Fetching <comment>services</comment>');
$services = fetchServices($configuration);
$logger(sprintf('Found <info>%d</info> <comment>services</comment>', count($services)), Console\Output\OutputInterface::VERBOSITY_VERY_VERBOSE);
$logger('Fetching <comment>volumes</comment>');
$volumes = fetchVolumes($configuration);
$logger(sprintf('Found <info>%d</info> <comment>volumes</comment>', count($volumes)), Console\Output\OutputInterface::VERBOSITY_VERY_VERBOSE);
$logger('Fetching <comment>networks</comment>');
$networks = fetchNetworks($configuration);
$logger(sprintf('Found <info>%d</info> <comment>networks</comment>', count($networks)), Console\Output\OutputInterface::VERBOSITY_VERY_VERBOSE);
if ([] !== $onlyServices) {
$logger(sprintf('Only <info>%s</info> <comment>services</comment> will be displayed', implode(', ', $onlyServices)));
$intersect = array_intersect($onlyServices, array_keys($services));
if ($intersect !== $onlyServices) {
@ -89,17 +104,24 @@ $application->register('render')
$flags = 0;
if ($input->getOption('no-volumes') === true) {
$logger('<comment>Volumes</comment> will not be displayed');
$flags |= WITHOUT_VOLUMES;
}
if ($input->getOption('no-networks') === true) {
$logger('<comment>Networks</comment> will not be displayed');
$flags |= WITHOUT_NETWORKS;
}
if ($input->getOption('no-ports') === true) {
$logger('<comment>Ports</comment> will not be displayed');
$flags |= WITHOUT_PORTS;
}
$logger('Rendering <comment>graph</comment>');
$graph = applyGraphvizStyle(
createGraph($services, $volumes, $networks, $inputFile, $flags),
$input->getOption('horizontal'),

View file

@ -5,6 +5,7 @@ namespace PMSIpilot\DockerComposeViz;
use Fhaculty\Graph\Edge;
use Fhaculty\Graph\Graph;
use Fhaculty\Graph\Vertex;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
@ -12,6 +13,20 @@ const WITHOUT_VOLUMES = 1;
const WITHOUT_NETWORKS = 2;
const WITHOUT_PORTS = 4;
/**
* @internal
*
* @param OutputInterface $output
*
* @return callable
*/
function logger(OutputInterface $output) : callable
{
return function (string $message, int $verbosity = null) use ($output) {
$output->writeln(sprintf('[%s] %s', date(DATE_ISO8601), $message), $verbosity ?: OutputInterface::VERBOSITY_VERBOSE);
};
}
/**
* @public
*
@ -216,7 +231,7 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr
if (((bool) ($flags & WITHOUT_NETWORKS)) === false) {
foreach ($networks as $network => $definition) {
addNetwork(
$graph, 'net: ' . $network,
$graph, 'net: '.$network,
isset($definition['external']) && $definition['external'] === true ? 'external_network' : 'network'
);
}
@ -318,7 +333,7 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr
list($host, $container, $proto) = explodeMapping($port);
addRelation(
addPort($graph, (int)$host, $proto),
addPort($graph, (int) $host, $proto),
$graph->getVertex($service),
'ports',
$host !== $container ? $container : null
@ -334,7 +349,7 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr
addRelation(
$graph->getVertex($service),
addNetwork($graph, 'net: ' . $network),
addNetwork($graph, 'net: '.$network),
'networks',
count($aliases) > 0 ? implode(', ', $aliases) : null
);