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) # `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 `--no-networks` and `--no-ports` options to avoid rendering networks and ports
* Add the `--background` option to set the graph's background color * Add the `--background` option to set the graph's background color
* Versions correctly merged and checked * 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)); 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'); $inputFile = $input->getArgument('input-file');
$inputFileExtension = pathinfo($inputFile, PATHINFO_EXTENSION); $inputFileExtension = pathinfo($inputFile, PATHINFO_EXTENSION);
$overrideFile = dirname($inputFile).DIRECTORY_SEPARATOR.basename($inputFile, '.'.$inputFileExtension).'.'.$input->getOption('override').'.'.$inputFileExtension; $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); $configuration = readConfiguration($inputFile);
$configurationVersion = (string) ($configuration['version'] ?? 1); $configurationVersion = (string) ($configuration['version'] ?? 1);
if (!$input->getOption('ignore-override') && file_exists($overrideFile)) { if (!$input->getOption('ignore-override') && file_exists($overrideFile)) {
$logger(sprintf('Reading <comment>override</comment> from <info>"%s"</info>', $overrideFile));
$override = readConfiguration($overrideFile); $override = readConfiguration($overrideFile);
$overrideVersion = (string) ($override['version'] ?? 1); $overrideVersion = (string) ($override['version'] ?? 1);
@ -64,14 +67,26 @@ $application->register('render')
} }
$configuration = array_merge_recursive($configuration, $override); $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; $configuration['version'] = $configurationVersion;
} }
$logger('Fetching <comment>services</comment>');
$services = fetchServices($configuration); $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); $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); $networks = fetchNetworks($configuration);
$logger(sprintf('Found <info>%d</info> <comment>networks</comment>', count($networks)), Console\Output\OutputInterface::VERBOSITY_VERY_VERBOSE);
if ([] !== $onlyServices) { if ([] !== $onlyServices) {
$logger(sprintf('Only <info>%s</info> <comment>services</comment> will be displayed', implode(', ', $onlyServices)));
$intersect = array_intersect($onlyServices, array_keys($services)); $intersect = array_intersect($onlyServices, array_keys($services));
if ($intersect !== $onlyServices) { if ($intersect !== $onlyServices) {
@ -89,17 +104,24 @@ $application->register('render')
$flags = 0; $flags = 0;
if ($input->getOption('no-volumes') === true) { if ($input->getOption('no-volumes') === true) {
$logger('<comment>Volumes</comment> will not be displayed');
$flags |= WITHOUT_VOLUMES; $flags |= WITHOUT_VOLUMES;
} }
if ($input->getOption('no-networks') === true) { if ($input->getOption('no-networks') === true) {
$logger('<comment>Networks</comment> will not be displayed');
$flags |= WITHOUT_NETWORKS; $flags |= WITHOUT_NETWORKS;
} }
if ($input->getOption('no-ports') === true) { if ($input->getOption('no-ports') === true) {
$logger('<comment>Ports</comment> will not be displayed');
$flags |= WITHOUT_PORTS; $flags |= WITHOUT_PORTS;
} }
$logger('Rendering <comment>graph</comment>');
$graph = applyGraphvizStyle( $graph = applyGraphvizStyle(
createGraph($services, $volumes, $networks, $inputFile, $flags), createGraph($services, $volumes, $networks, $inputFile, $flags),
$input->getOption('horizontal'), $input->getOption('horizontal'),

View file

@ -5,6 +5,7 @@ namespace PMSIpilot\DockerComposeViz;
use Fhaculty\Graph\Edge; use Fhaculty\Graph\Edge;
use Fhaculty\Graph\Graph; use Fhaculty\Graph\Graph;
use Fhaculty\Graph\Vertex; use Fhaculty\Graph\Vertex;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
@ -12,6 +13,20 @@ const WITHOUT_VOLUMES = 1;
const WITHOUT_NETWORKS = 2; const WITHOUT_NETWORKS = 2;
const WITHOUT_PORTS = 4; 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 * @public
* *