From 96ee45edc96db7f335c63e2ebeda9ce427987de8 Mon Sep 17 00:00:00 2001 From: jubianchi Date: Sat, 20 Jan 2018 10:18:40 +0100 Subject: [PATCH] feat: Add a logger and enable and options Closes #26 --- CHANGELOG.md | 1 + src/application.php | 22 ++++++++++++++++++++++ src/functions.php | 21 ++++++++++++++++++--- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b0beb8..03457b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/application.php b/src/application.php index 1c402f1..db3dbb2 100644 --- a/src/application.php +++ b/src/application.php @@ -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 configuration from "%s"', $inputFile)); $configuration = readConfiguration($inputFile); $configurationVersion = (string) ($configuration['version'] ?? 1); if (!$input->getOption('ignore-override') && file_exists($overrideFile)) { + $logger(sprintf('Reading override from "%s"', $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 version is "%s"', $configurationVersion), Console\Output\OutputInterface::VERBOSITY_VERY_VERBOSE); $configuration['version'] = $configurationVersion; } + $logger('Fetching services'); $services = fetchServices($configuration); + $logger(sprintf('Found %d services', count($services)), Console\Output\OutputInterface::VERBOSITY_VERY_VERBOSE); + + $logger('Fetching volumes'); $volumes = fetchVolumes($configuration); + $logger(sprintf('Found %d volumes', count($volumes)), Console\Output\OutputInterface::VERBOSITY_VERY_VERBOSE); + + $logger('Fetching networks'); $networks = fetchNetworks($configuration); + $logger(sprintf('Found %d networks', count($networks)), Console\Output\OutputInterface::VERBOSITY_VERY_VERBOSE); if ([] !== $onlyServices) { + $logger(sprintf('Only %s services 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('Volumes will not be displayed'); + $flags |= WITHOUT_VOLUMES; } if ($input->getOption('no-networks') === true) { + $logger('Networks will not be displayed'); + $flags |= WITHOUT_NETWORKS; } if ($input->getOption('no-ports') === true) { + $logger('Ports will not be displayed'); + $flags |= WITHOUT_PORTS; } + $logger('Rendering graph'); $graph = applyGraphvizStyle( createGraph($services, $volumes, $networks, $inputFile, $flags), $input->getOption('horizontal'), diff --git a/src/functions.php b/src/functions.php index 4be8e12..6ca659f 100644 --- a/src/functions.php +++ b/src/functions.php @@ -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 );