Automatically load override file if it exists

or ignore it using `--ignore-override`

Closes #16
This commit is contained in:
jubianchi 2017-06-14 21:26:27 +02:00
parent 0f197f1251
commit 40d4feb59f
No known key found for this signature in database
GPG key ID: 5D9C896D2AA9E390
4 changed files with 36 additions and 3 deletions

View file

@ -1,4 +1,8 @@
# `1.0.0` (unreleased) # `1.1.0` (unreleased)
* Automatically load override file if it exists or ignore it using `--ignore-override`
# `1.0.0`
* Avoid duplicating edges when there is multiple extended services * Avoid duplicating edges when there is multiple extended services
* Display extended services as components with inverted arrows * Display extended services as components with inverted arrows

View file

@ -45,18 +45,21 @@ bin/dcv
## Usage ## Usage
``` ```
render [options] [--] [<input-file>] Usage:
render [options] [--] [<input-file>]
Arguments: Arguments:
input-file Path to a docker compose file [default: "./docker-compose.yml"] input-file Path to a docker compose file [default: "./docker-compose.yml"]
Options: Options:
--override=OVERRIDE Tag of the override file to use [default: "override"]
-o, --output-file=OUTPUT-FILE Path to a output file (Only for "dot" and "image" output format) [default: "./docker-compose.dot" or "./docker-compose.png"] -o, --output-file=OUTPUT-FILE Path to a output file (Only for "dot" and "image" output format) [default: "./docker-compose.dot" or "./docker-compose.png"]
-m, --output-format=OUTPUT-FORMAT Output format (one of: "dot", "image", "display") [default: "display"] -m, --output-format=OUTPUT-FORMAT Output format (one of: "dot", "image", "display") [default: "display"]
--only=ONLY Display a graph only for a given services (multiple values allowed) --only=ONLY Display a graph only for a given services (multiple values allowed)
-f, --force Overwrites output file if it already exists -f, --force Overwrites output file if it already exists
--no-volumes Do not display volumes --no-volumes Do not display volumes
-r, --horizontal Display a horizontal graph -r, --horizontal Display a horizontal graph
--ignore-override Ignore override file
``` ```
## How to read the graph ## How to read the graph

View file

@ -16,6 +16,7 @@ $application = new Console\Application();
$application->register('render') $application->register('render')
->addArgument('input-file', Console\Input\InputArgument::OPTIONAL, 'Path to a docker compose file', getcwd().DIRECTORY_SEPARATOR.'docker-compose.yml') ->addArgument('input-file', Console\Input\InputArgument::OPTIONAL, 'Path to a docker compose file', getcwd().DIRECTORY_SEPARATOR.'docker-compose.yml')
->addOption('override', null, Console\Input\InputOption::VALUE_REQUIRED, 'Tag of the override file to use', 'override')
->addOption('output-file', 'o', Console\Input\InputOption::VALUE_REQUIRED, 'Path to a output file (Only for "dot" and "image" output format)') ->addOption('output-file', 'o', Console\Input\InputOption::VALUE_REQUIRED, 'Path to a output file (Only for "dot" and "image" output format)')
->addOption('output-format', 'm', Console\Input\InputOption::VALUE_REQUIRED, 'Output format (one of: "dot", "image", "display")', 'display') ->addOption('output-format', 'm', Console\Input\InputOption::VALUE_REQUIRED, 'Output format (one of: "dot", "image", "display")', 'display')
->addOption('only', null, Console\Input\InputOption::VALUE_IS_ARRAY | Console\Input\InputOption::VALUE_REQUIRED, 'Display a graph only for a given services') ->addOption('only', null, Console\Input\InputOption::VALUE_IS_ARRAY | Console\Input\InputOption::VALUE_REQUIRED, 'Display a graph only for a given services')
@ -23,15 +24,19 @@ $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('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')
->setCode(function (Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { ->setCode(function (Console\Input\InputInterface $input, Console\Output\OutputInterface $output) {
$inputFile = $input->getArgument('input-file'); $inputFile = $input->getArgument('input-file');
$inputFileExtension = pathinfo($inputFile, PATHINFO_EXTENSION);
$overrideFile = dirname($inputFile).DIRECTORY_SEPARATOR.basename($inputFile, '.'.$inputFileExtension).'.'.$input->getOption('override').'.'.$inputFileExtension;
$outputFormat = $input->getOption('output-format'); $outputFormat = $input->getOption('output-format');
$outputFile = $input->getOption('output-file') ?: getcwd().DIRECTORY_SEPARATOR.'docker-compose.'.($outputFormat === 'dot' ? $outputFormat : 'png'); $outputFile = $input->getOption('output-file') ?: getcwd().DIRECTORY_SEPARATOR.'docker-compose.'.($outputFormat === 'dot' ? $outputFormat : 'png');
$onlyServices = $input->getOption('only'); $onlyServices = $input->getOption('only');
if (in_array($outputFormat, ['dot', 'image', 'display']) === false) { if (in_array($outputFormat, ['dot', 'image', 'display']) === false) {
throw new Console\Exception\InvalidArgumentException(sprintf('Invalid output format "%s". It must be one of "dot", "png" or "display".', $outputFormat)); throw new Console\Exception\InvalidArgumentException(sprintf('Invalid output format "%s". It must be one of "dot", "image" or "display".', $outputFormat));
} }
if ($outputFormat === 'display') { if ($outputFormat === 'display') {
@ -45,6 +50,13 @@ $application->register('render')
} }
$configuration = readConfiguration($inputFile); $configuration = readConfiguration($inputFile);
if (!$input->getOption('ignore-override') && file_exists($overrideFile)) {
$override = readConfiguration($overrideFile);
$configuration = array_merge_recursive($configuration, $override);
}
$services = fetchServices($configuration); $services = fetchServices($configuration);
$volumes = fetchVolumes($configuration); $volumes = fetchVolumes($configuration);
$networks = fetchNetworks($configuration); $networks = fetchNetworks($configuration);

View file

@ -228,9 +228,15 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr
); );
} }
$serviceLinks = [];
foreach ($definition['links'] ?? [] as $link) { foreach ($definition['links'] ?? [] as $link) {
list($target, $alias) = explodeMapping($link); list($target, $alias) = explodeMapping($link);
$serviceLinks[$alias] = $target;
}
foreach ($serviceLinks as $alias => $target) {
addRelation( addRelation(
addService($graph, $target), addService($graph, $target),
$graph->getVertex($service), $graph->getVertex($service),
@ -267,9 +273,17 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr
} }
if ($withVolumes === true) { if ($withVolumes === true) {
$serviceVolumes = [];
foreach ($definition['volumes'] ?? [] as $volume) { foreach ($definition['volumes'] ?? [] as $volume) {
list($host, $container, $attr) = explodeMapping($volume); list($host, $container, $attr) = explodeMapping($volume);
$serviceVolumes[$container] = [$host, $attr];
}
foreach ($serviceVolumes as $container => $volume) {
list($host, $attr) = $volume;
if ($host[0] !== '.' && $host[0] !== DIRECTORY_SEPARATOR) { if ($host[0] !== '.' && $host[0] !== DIRECTORY_SEPARATOR) {
$host = 'named: '.$host; $host = 'named: '.$host;
} }