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)
* 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

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('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')
);

View file

@ -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,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
*/
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);
}
}
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'
);
}
}
foreach ($services as $service => $definition) {
addService($graph, $service);
@ -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,17 +313,20 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr
}
}
if (((bool) ($flags & WITHOUT_PORTS)) === false) {
foreach ($definition['ports'] ?? [] as $port) {
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
);
}
}
if (((bool) ($flags & WITHOUT_NETWORKS)) === false) {
foreach ($definition['networks'] ?? [] as $network => $config) {
$network = is_int($network) ? $config : $network;
$config = is_int($network) ? [] : $config;
@ -325,12 +334,13 @@ 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
);
}
}
}
return $graph;
}