From deac235afeefc4eab0781830f9c274b768ec176c Mon Sep 17 00:00:00 2001 From: jubianchi Date: Sat, 20 Jan 2018 09:50:37 +0100 Subject: [PATCH] feat: Add the and options to avoid rendering networks and ports Closes #24 --- CHANGELOG.md | 1 + src/application.php | 17 +++++++++++- src/functions.php | 68 ++++++++++++++++++++++++++------------------- 3 files changed, 56 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cc6a31..6b0beb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # `1.2.0` (unreleased) +* 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 31ab97b..1c402f1 100644 --- a/src/application.php +++ b/src/application.php @@ -17,6 +17,8 @@ $application->register('render') ->addOption('force', 'f', Console\Input\InputOption::VALUE_NONE, 'Overwrites output file if it already exists') ->addOption('no-volumes', null, Console\Input\InputOption::VALUE_NONE, 'Do not display volumes') + ->addOption('no-networks', null, Console\Input\InputOption::VALUE_NONE, 'Do not display networks') + ->addOption('no-ports', null, Console\Input\InputOption::VALUE_NONE, 'Do not display ports') ->addOption('horizontal', 'r', Console\Input\InputOption::VALUE_NONE, 'Display a horizontal graph') ->addOption('ignore-override', null, Console\Input\InputOption::VALUE_NONE, 'Ignore override file') ->addOption('background', null, Console\Input\InputOption::VALUE_REQUIRED, 'Set the graph background color', '#ffffff') @@ -85,8 +87,21 @@ $application->register('render') ); } + $flags = 0; + if ($input->getOption('no-volumes') === true) { + $flags |= WITHOUT_VOLUMES; + } + + if ($input->getOption('no-networks') === true) { + $flags |= WITHOUT_NETWORKS; + } + + if ($input->getOption('no-ports') === true) { + $flags |= WITHOUT_PORTS; + } + $graph = applyGraphvizStyle( - createGraph($services, $volumes, $networks, $input->getOption('no-volumes') === false, $inputFile), + createGraph($services, $volumes, $networks, $inputFile, $flags), $input->getOption('horizontal'), $input->getOption('background') ); diff --git a/src/functions.php b/src/functions.php index 87bab07..4be8e12 100644 --- a/src/functions.php +++ b/src/functions.php @@ -8,6 +8,10 @@ use Fhaculty\Graph\Vertex; use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Yaml; +const WITHOUT_VOLUMES = 1; +const WITHOUT_NETWORKS = 2; +const WITHOUT_PORTS = 4; + /** * @public * @@ -87,9 +91,9 @@ function fetchNetworks(array $configuration) : array * * @return Graph The complete graph for the given list of services */ -function createGraph(array $services, array $volumes, array $networks, bool $withVolumes, string $path) : Graph +function createGraph(array $services, array $volumes, array $networks, string $path, int $flags) : Graph { - return makeVerticesAndEdges(new Graph(), $services, $volumes, $networks, $withVolumes, $path); + return makeVerticesAndEdges(new Graph(), $services, $volumes, $networks, $path, $flags); } /** @@ -201,19 +205,21 @@ function applyGraphvizStyle(Graph $graph, bool $horizontal, string $background) * * @return Graph A copy of the input graph with vertices and edges for services */ -function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, array $networks, bool $withVolumes, $path) : Graph +function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, array $networks, string $path, int $flags) : Graph { - if ($withVolumes === true) { + if (((bool) ($flags & WITHOUT_VOLUMES)) === false) { foreach (array_keys($volumes) as $volume) { addVolume($graph, 'named: '.$volume); } } - foreach ($networks as $network => $definition) { - addNetwork( - $graph, 'net: '.$network, - isset($definition['external']) && $definition['external'] === true ? 'external_network' : 'network' - ); + if (((bool) ($flags & WITHOUT_NETWORKS)) === false) { + foreach ($networks as $network => $definition) { + addNetwork( + $graph, 'net: ' . $network, + isset($definition['external']) && $definition['external'] === true ? 'external_network' : 'network' + ); + } } foreach ($services as $service => $definition) { @@ -225,7 +231,7 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr $extendedVolumes = fetchVolumes($configuration); $extendedNetworks = fetchVolumes($configuration); - $graph = makeVerticesAndEdges($graph, $extendedServices, $extendedVolumes, $extendedNetworks, $withVolumes, dirname($path).DIRECTORY_SEPARATOR.$definition['extends']['file']); + $graph = makeVerticesAndEdges($graph, $extendedServices, $extendedVolumes, $extendedNetworks, dirname($path).DIRECTORY_SEPARATOR.$definition['extends']['file'], $flags); addRelation( addService($graph, $definition['extends']['service']), @@ -281,7 +287,7 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr ); } - if ($withVolumes === true) { + if (((bool) ($flags & WITHOUT_VOLUMES)) === false) { $serviceVolumes = []; foreach ($definition['volumes'] ?? [] as $volume) { @@ -307,28 +313,32 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr } } - foreach ($definition['ports'] ?? [] as $port) { - list($host, $container, $proto) = explodeMapping($port); + if (((bool) ($flags & WITHOUT_PORTS)) === false) { + foreach ($definition['ports'] ?? [] as $port) { + list($host, $container, $proto) = explodeMapping($port); - addRelation( - addPort($graph, (int) $host, $proto), - $graph->getVertex($service), - 'ports', - $host !== $container ? $container : null - ); + addRelation( + addPort($graph, (int)$host, $proto), + $graph->getVertex($service), + 'ports', + $host !== $container ? $container : null + ); + } } - foreach ($definition['networks'] ?? [] as $network => $config) { - $network = is_int($network) ? $config : $network; - $config = is_int($network) ? [] : $config; - $aliases = $config['aliases'] ?? []; + if (((bool) ($flags & WITHOUT_NETWORKS)) === false) { + foreach ($definition['networks'] ?? [] as $network => $config) { + $network = is_int($network) ? $config : $network; + $config = is_int($network) ? [] : $config; + $aliases = $config['aliases'] ?? []; - addRelation( - $graph->getVertex($service), - addNetwork($graph, 'net: '.$network), - 'networks', - count($aliases) > 0 ? implode(', ', $aliases) : null - ); + addRelation( + $graph->getVertex($service), + addNetwork($graph, 'net: ' . $network), + 'networks', + count($aliases) > 0 ? implode(', ', $aliases) : null + ); + } } }