Skip to content

Commit

Permalink
Add debug-schema command to show the internal representation (#174)
Browse files Browse the repository at this point in the history
Introduced a new `debug-schema` command in CSV Blueprint that shows the
internal representation of the scheme with presets taken into account.
  • Loading branch information
SmetDenis committed Apr 18, 2024
1 parent 7c3449c commit 9735b89
Show file tree
Hide file tree
Showing 5 changed files with 423 additions and 2 deletions.
48 changes: 46 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -960,10 +960,10 @@ In fact, this can be considered as partial inheritance.
**If something went wrong**

If you're having trouble working with presets and don't understand how the CSV Blueprint under the hood understands it,
just add `--dump-schema` to see it. Also, there is a separate CLI command for validating schema:
just add `--dump-schema` to see it. Also, there is a separate CLI command to dump schema:

```shell
./csv-blueprint validate:schema --dump-schema --schema=./your/schema.yml
./csv-blueprint debug-schema -s ./your/schema.yml
```


Expand Down Expand Up @@ -1542,6 +1542,49 @@ Options:
</details>


`./csv-blueprint dump-schema --help`

<details>
<summary>CLICK to see debug-schema help messege</summary>

<!-- auto-update:debug-schema-help -->
```
Description:
Show the internal representation of the schema taking into account presets.
Usage:
debug-schema [options]
Options:
-s, --schema=SCHEMA Specify the path to a schema file, supporting YAML, JSON, or PHP formats.
Examples: /full/path/name.yml; p/file.yml
-d, --hide-defaults Hide default values in the output.
--no-progress Disable progress bar animation for logs. It will be used only for text output format.
--mute-errors Mute any sort of errors. So exit code will be always "0" (if it's possible).
It has major priority then --non-zero-on-error. It's on your own risk!
--stdout-only For any errors messages application will use StdOut instead of StdErr. It's on your own risk!
--non-zero-on-error None-zero exit code on any StdErr message.
--timestamp Show timestamp at the beginning of each message.It will be used only for text output format.
--profile Display timing and memory usage information.
--output-mode=OUTPUT-MODE Output format. Available options:
text - Default text output format, userfriendly and easy to read.
cron - Shortcut for crontab. It's basically focused on human-readable logs output.
It's combination of --timestamp --profile --stdout-only --no-progress -vv.
logstash - Logstash output format, for integration with ELK stack.
[default: "text"]
--cron Alias for --output-mode=cron. Deprecated!
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
```
<!-- auto-update:/debug-schema-help -->

</details>


`./csv-blueprint create:schema --help`
It's beta. Work in progress.

Expand Down Expand Up @@ -1599,6 +1642,7 @@ Options:

</details>


## Report examples

The validation process culminates in a human-readable report detailing any errors identified within the CSV file. While
Expand Down
65 changes: 65 additions & 0 deletions src/Commands/DebugSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Commands;

use JBZoo\Cli\CliCommand;
use JBZoo\CsvBlueprint\Schema;
use Symfony\Component\Console\Input\InputOption;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class DebugSchema extends CliCommand
{
protected function configure(): void
{
$this
->setName('debug-schema')
->setDescription('Show the internal representation of the schema taking into account presets.')
->addOption(
'schema',
's',
InputOption::VALUE_REQUIRED,
\implode("\n", [
'Specify the path to a schema file, supporting YAML, JSON, or PHP formats. ',
'Examples: <info>/full/path/name.yml</info>; <info>p/file.yml</info>',
]),
)
->addOption(
'hide-defaults',
'd',
InputOption::VALUE_NONE,
'Hide default values in the output.',
);

parent::configure();
}

protected function executeAction(): int
{
$decorated = $this->outputMode->getOutput()->isDecorated();

$schemaFilename = $this->getOptString('schema');
if (!\file_exists($schemaFilename)) {
throw new Exception("Schema file not found: {$schemaFilename}");
}

$this->_((new Schema($schemaFilename))->dumpAsYamlString($this->getOptBool('hide-defaults'), $decorated));

return self::SUCCESS;
}
}

0 comments on commit 9735b89

Please sign in to comment.