Skip to content

Commit

Permalink
Feature: scan dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rokas Mikalkėnas committed Sep 10, 2020
1 parent 31ff47d commit f798833
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 29 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"require": {
"php": "~7.2",
"symfony/console": "~3.4|~4.3|~5.0",
"symfony/finder": "~3.4|~4.3|~5.0",
"symfony/yaml": "~3.4|~4.3|~5.0"
},
"require-dev": {
Expand Down
6 changes: 3 additions & 3 deletions docs/symfony-config/yaml-sort-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ files:
- host
- username
- password
excludedSections:
0: imports
1: parameters
excludedSections:
0: imports
1: parameters

app/config/config_dev.yml:
excludedKeys:
Expand Down
96 changes: 70 additions & 26 deletions src/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,48 @@

namespace Mhujer\YamlSortChecker;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Yaml;

class CheckCommand extends \Symfony\Component\Console\Command\Command
{

protected function configure(): void
{
$this->setName('yaml-check-sort');
$this
->setName('yaml-check-sort')
->addArgument('config', InputArgument::OPTIONAL, 'Configuration filename', 'yaml-sort-checker.yml');
}

protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$output->writeln('#### YAML Sort Checker ####');

$configFilePath = realpath('yaml-sort-checker.yml');
$configFilePath = realpath($input->getArgument('config'));
if ($configFilePath === false) {
$output->writeln(sprintf('Config file "%s" not found!', 'yaml-sort-checker.yml'));
exit(1);
return 1;
}
$output->writeln(sprintf('Using config file "%s"', $configFilePath));

$configFileContents = file_get_contents($configFilePath);
if ($configFileContents === false) {
throw new \Exception(sprintf('File "%s" could not be loaded', $configFilePath));
$output->writeln(sprintf('File "%s" could not be loaded', $configFilePath));
return 1;
}
$config = Yaml::parse($configFileContents);

if (!array_key_exists('files', $config)) {
$output->writeln('There must be a key "files" in config');
exit(1);
return 1;
}

if (count($config['files']) === 0) {
$output->writeln('There must be some files in the config');
exit(1);
return 1;
}

$output->writeln('');
Expand All @@ -56,33 +61,72 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
$excludedKeys = array_key_exists('excludedKeys', $options) ? $options['excludedKeys'] : [];
$excludedSections = array_key_exists('excludedSections', $options) ? $options['excludedSections'] : [];

$output->write(sprintf('Checking %s: ', $filename));
if (realpath($filename) === false || !is_readable(realpath($filename))) {
$output->writeln('NOT READABLE!');
exit(1);
}

$sortCheckResult = $sortChecker->isSorted($filename, $depth, $excludedKeys, $excludedSections);

if ($sortCheckResult->isOk()) {
$output->writeln('OK');
} else {
$output->writeln('ERROR');
foreach ($sortCheckResult->getMessages() as $message) {
$output->writeln(' ' . $message);
}
$isOk = false;
}
try {
$files = $this->resolveYmlFiles($filename);
} catch (\InvalidArgumentException $e) {
$output->writeln($e->getMessage());
return 1;
}

foreach ($files as $file) {
$output->write(sprintf('Checking %s: ', $file));

$sortCheckResult = $sortChecker->isSorted($file, $depth, $excludedKeys, $excludedSections);

if ($sortCheckResult->isOk()) {
$output->writeln('OK');
} else {
$output->writeln('ERROR');
foreach ($sortCheckResult->getMessages() as $message) {
$output->writeln(' ' . $message);
}
$isOk = false;
}
}
}

$output->writeln('');
if (!$isOk) {
$output->writeln('Fix the YAMLs or exclude the keys in the config.');
return 1;
} else {
$output->writeln('All YAMLs are properly sorted.');
return 0;
}

$output->writeln('All YAMLs are properly sorted.');
return 0;
}

private function resolveYmlFiles(string $filename): array
{
// Check if its file
$filepath = realpath($filename);
if ($filepath !== false && is_file($filepath) && is_readable($filepath)) {
return [$filename];
}

try {
// If not try to parse it as directory
$filesIterator = (new Finder())
->in($filename)
->name(['*.yml', '*.yaml'])
->ignoreUnreadableDirs(true)
->ignoreDotFiles(true)
->ignoreVCS(true)
->getIterator();

$files = [];
foreach ($filesIterator as $fileInfo) {
$fileRealPath = $fileInfo->getRealPath();
if (!is_readable($fileRealPath)) {
throw new \InvalidArgumentException(sprintf('File %s is not readable', $fileRealPath));
}

$files[] = $fileRealPath;
}

return $files;
} catch (\Exception $e) {
throw new \InvalidArgumentException(sprintf('Unable to find files in %s', $filename));
}
}

}

0 comments on commit f798833

Please sign in to comment.