Fixed some mistakes while processing -d

This commit is contained in:
Anthony Axenov 2023-08-04 09:01:40 +08:00
parent 3b94911895
commit aa583e7d43
Signed by: anthony
GPG Key ID: EA9EC32FF7CCD4EC
2 changed files with 14 additions and 13 deletions

View File

@ -65,13 +65,13 @@ class FileSystem
{ {
$path = static::normalizePath($path); $path = static::normalizePath($path);
if (!file_exists($path)) { if (!file_exists($path)) {
throw new DirectoryNotExistsException("output directory is not exist: $path"); throw new DirectoryNotExistsException("directory does not exist: $path");
} }
if (!is_readable($path)) { if (!is_readable($path)) {
throw new DirectoryIsNotReadableException("output directory permissions are not valid: $path"); throw new DirectoryIsNotReadableException("directory permissions are not valid: $path");
} }
if (!is_writable($path)) { if (!is_writable($path)) {
throw new DirectoryIsNotWriteableException("output directory permissions are not valid: $path"); throw new DirectoryIsNotWriteableException("directory permissions are not valid: $path");
} }
return true; return true;
} }

View File

@ -27,7 +27,7 @@ class Processor
/** /**
* @var string[] Paths to collection files * @var string[] Paths to collection files
*/ */
protected array $collectionPaths; protected array $collectionPaths = [];
/** /**
* @var string Output path where to put results in * @var string Output path where to put results in
@ -52,7 +52,7 @@ class Processor
/** /**
* @var object[] Collections that will be converted into choosen formats * @var object[] Collections that will be converted into choosen formats
*/ */
protected array $collections; protected array $collections = [];
/** /**
* Constructor * Constructor
@ -80,7 +80,7 @@ class Processor
switch ($arg) { switch ($arg) {
case '-f': case '-f':
case '--file': case '--file':
$path = $this->argv[$idx + 1]; $path = FileSystem::normalizePath($this->argv[$idx + 1]);
if (empty($path) || !str_ends_with($path, '.json') || !file_exists($path) || !is_readable($path)) { if (empty($path) || !str_ends_with($path, '.json') || !file_exists($path) || !is_readable($path)) {
throw new InvalidArgumentException('a valid json-file path is expected for -f (--file)'); throw new InvalidArgumentException('a valid json-file path is expected for -f (--file)');
} }
@ -99,13 +99,11 @@ class Processor
throw new InvalidArgumentException('a directory path is expected for -d (--dir)'); throw new InvalidArgumentException('a directory path is expected for -d (--dir)');
} }
$path = $this->argv[$idx + 1]; $path = $this->argv[$idx + 1];
if (FileSystem::checkDir($path)) { $files = array_filter(
$files = array_filter( FileSystem::dirContents($path),
FileSystem::dirContents($path), static fn($filename) => str_ends_with($filename, '.json')
static fn($filename) => str_ends_with($filename, '.json') );
); $this->collectionPaths = array_unique(array_merge($this?->collectionPaths ?? [], $files));
$this->collectionPaths = array_unique(array_merge($this?->collectionPaths ?? [], $files));
}
break; break;
case '-p': case '-p':
case '--preserve': case '--preserve':
@ -128,6 +126,9 @@ class Processor
die(implode(PHP_EOL, $this->usage()) . PHP_EOL); die(implode(PHP_EOL, $this->usage()) . PHP_EOL);
} }
} }
if (empty($this->collectionPaths)) {
throw new InvalidArgumentException('there are no collections to convert');
}
if (empty($this->formats)) { if (empty($this->formats)) {
$this->formats = [ConvertFormat::Http->name => ConvertFormat::Http]; $this->formats = [ConvertFormat::Http->name => ConvertFormat::Http];
} }