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,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 * @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);
} }
} }
foreach ($networks as $network => $definition) { if (((bool) ($flags & WITHOUT_NETWORKS)) === false) {
addNetwork( foreach ($networks as $network => $definition) {
$graph, 'net: '.$network, addNetwork(
isset($definition['external']) && $definition['external'] === true ? 'external_network' : 'network' $graph, 'net: ' . $network,
); isset($definition['external']) && $definition['external'] === true ? 'external_network' : 'network'
);
}
} }
foreach ($services as $service => $definition) { foreach ($services as $service => $definition) {
@ -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,28 +313,32 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr
} }
} }
foreach ($definition['ports'] ?? [] as $port) { if (((bool) ($flags & WITHOUT_PORTS)) === false) {
list($host, $container, $proto) = explodeMapping($port); foreach ($definition['ports'] ?? [] as $port) {
list($host, $container, $proto) = explodeMapping($port);
addRelation( addRelation(
addPort($graph, (int) $host, $proto), addPort($graph, (int)$host, $proto),
$graph->getVertex($service), $graph->getVertex($service),
'ports', 'ports',
$host !== $container ? $container : null $host !== $container ? $container : null
); );
}
} }
foreach ($definition['networks'] ?? [] as $network => $config) { if (((bool) ($flags & WITHOUT_NETWORKS)) === false) {
$network = is_int($network) ? $config : $network; foreach ($definition['networks'] ?? [] as $network => $config) {
$config = is_int($network) ? [] : $config; $network = is_int($network) ? $config : $network;
$aliases = $config['aliases'] ?? []; $config = is_int($network) ? [] : $config;
$aliases = $config['aliases'] ?? [];
addRelation( addRelation(
$graph->getVertex($service), $graph->getVertex($service),
addNetwork($graph, 'net: '.$network), addNetwork($graph, 'net: ' . $network),
'networks', 'networks',
count($aliases) > 0 ? implode(', ', $aliases) : null count($aliases) > 0 ? implode(', ', $aliases) : null
); );
}
} }
} }