feat: Add the and options to avoid rendering networks and ports

Closes #24
This commit is contained in:
jubianchi 2018-01-20 09:50:37 +01:00
parent 2a744599c0
commit deac235afe
No known key found for this signature in database
GPG key ID: 5D9C896D2AA9E390
3 changed files with 56 additions and 30 deletions

View file

@ -1,5 +1,6 @@
# `1.2.0` (unreleased) # `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 * Add the `--background` option to set the graph's background color
* Versions correctly merged and checked * Versions correctly merged and checked

View file

@ -17,6 +17,8 @@ $application->register('render')
->addOption('force', 'f', Console\Input\InputOption::VALUE_NONE, 'Overwrites output file if it already exists') ->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-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('horizontal', 'r', Console\Input\InputOption::VALUE_NONE, 'Display a horizontal graph')
->addOption('ignore-override', null, Console\Input\InputOption::VALUE_NONE, 'Ignore override file') ->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') ->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( $graph = applyGraphvizStyle(
createGraph($services, $volumes, $networks, $input->getOption('no-volumes') === false, $inputFile), createGraph($services, $volumes, $networks, $inputFile, $flags),
$input->getOption('horizontal'), $input->getOption('horizontal'),
$input->getOption('background') $input->getOption('background')
); );

View file

@ -8,6 +8,10 @@ use Fhaculty\Graph\Vertex;
use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
const WITHOUT_VOLUMES = 1;
const WITHOUT_NETWORKS = 2;
const WITHOUT_PORTS = 4;
/** /**
* @public * @public
* *
@ -87,9 +91,9 @@ function fetchNetworks(array $configuration) : array
* *
* @return Graph The complete graph for the given list of services * @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,20 +205,22 @@ function applyGraphvizStyle(Graph $graph, bool $horizontal, string $background)
* *
* @return Graph A copy of the input graph with vertices and edges for services * @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) { foreach (array_keys($volumes) as $volume) {
addVolume($graph, 'named: '.$volume); addVolume($graph, 'named: '.$volume);
} }
} }
if (((bool) ($flags & WITHOUT_NETWORKS)) === false) {
foreach ($networks as $network => $definition) { foreach ($networks as $network => $definition) {
addNetwork( addNetwork(
$graph, 'net: ' . $network, $graph, 'net: ' . $network,
isset($definition['external']) && $definition['external'] === true ? 'external_network' : 'network' isset($definition['external']) && $definition['external'] === true ? 'external_network' : 'network'
); );
} }
}
foreach ($services as $service => $definition) { foreach ($services as $service => $definition) {
addService($graph, $service); addService($graph, $service);
@ -225,7 +231,7 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr
$extendedVolumes = fetchVolumes($configuration); $extendedVolumes = fetchVolumes($configuration);
$extendedNetworks = 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( addRelation(
addService($graph, $definition['extends']['service']), 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 = []; $serviceVolumes = [];
foreach ($definition['volumes'] ?? [] as $volume) { foreach ($definition['volumes'] ?? [] as $volume) {
@ -307,6 +313,7 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr
} }
} }
if (((bool) ($flags & WITHOUT_PORTS)) === false) {
foreach ($definition['ports'] ?? [] as $port) { foreach ($definition['ports'] ?? [] as $port) {
list($host, $container, $proto) = explodeMapping($port); list($host, $container, $proto) = explodeMapping($port);
@ -317,7 +324,9 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr
$host !== $container ? $container : null $host !== $container ? $container : null
); );
} }
}
if (((bool) ($flags & WITHOUT_NETWORKS)) === false) {
foreach ($definition['networks'] ?? [] as $network => $config) { foreach ($definition['networks'] ?? [] as $network => $config) {
$network = is_int($network) ? $config : $network; $network = is_int($network) ? $config : $network;
$config = is_int($network) ? [] : $config; $config = is_int($network) ? [] : $config;
@ -331,6 +340,7 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr
); );
} }
} }
}
return $graph; return $graph;
} }