From 40d4feb59f5d542af24784867005fa31123652ca Mon Sep 17 00:00:00 2001 From: jubianchi Date: Wed, 14 Jun 2017 21:26:27 +0200 Subject: [PATCH] Automatically load override file if it exists or ignore it using `--ignore-override` Closes #16 --- CHANGELOG.md | 6 +++++- README.md | 5 ++++- src/application.php | 14 +++++++++++++- src/functions.php | 14 ++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0035e26..faabed3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 * Display extended services as components with inverted arrows diff --git a/README.md b/README.md index 98de447..304d7cf 100644 --- a/README.md +++ b/README.md @@ -45,18 +45,21 @@ bin/dcv ## Usage ``` -render [options] [--] [] +Usage: + render [options] [--] [] Arguments: input-file Path to a docker compose file [default: "./docker-compose.yml"] 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"] -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) -f, --force Overwrites output file if it already exists --no-volumes Do not display volumes -r, --horizontal Display a horizontal graph + --ignore-override Ignore override file ``` ## How to read the graph diff --git a/src/application.php b/src/application.php index 2acc025..fc2bc34 100644 --- a/src/application.php +++ b/src/application.php @@ -16,6 +16,7 @@ $application = new Console\Application(); $application->register('render') ->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-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') @@ -23,15 +24,19 @@ $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('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) { $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'); $outputFile = $input->getOption('output-file') ?: getcwd().DIRECTORY_SEPARATOR.'docker-compose.'.($outputFormat === 'dot' ? $outputFormat : 'png'); $onlyServices = $input->getOption('only'); 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') { @@ -45,6 +50,13 @@ $application->register('render') } $configuration = readConfiguration($inputFile); + + if (!$input->getOption('ignore-override') && file_exists($overrideFile)) { + $override = readConfiguration($overrideFile); + + $configuration = array_merge_recursive($configuration, $override); + } + $services = fetchServices($configuration); $volumes = fetchVolumes($configuration); $networks = fetchNetworks($configuration); diff --git a/src/functions.php b/src/functions.php index 9b27ce7..3ac1fa8 100644 --- a/src/functions.php +++ b/src/functions.php @@ -228,9 +228,15 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr ); } + $serviceLinks = []; + foreach ($definition['links'] ?? [] as $link) { list($target, $alias) = explodeMapping($link); + $serviceLinks[$alias] = $target; + } + + foreach ($serviceLinks as $alias => $target) { addRelation( addService($graph, $target), $graph->getVertex($service), @@ -267,9 +273,17 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr } if ($withVolumes === true) { + $serviceVolumes = []; + foreach ($definition['volumes'] ?? [] as $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) { $host = 'named: '.$host; }