Skip to content

Commit

Permalink
Add phar building, improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dsuurlant committed May 25, 2020
1 parent c5aa315 commit 37d33c0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor/
vendor/
build/
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Supports generating an OpenAPI spec in `JSON` or `yaml`.

`composer require --dev dsuurlant/response2schema`

Or download the phar from the [Releases page](https://github.com/dsuurlant/response2schema/releases).

# Usage

Just point Response2Schema to your input json file, and tell it where to put the output OpenAPI spec.
Expand All @@ -22,6 +24,10 @@ It will automatically format it to json or yaml based on the extension of the ou

`./vendor/bin/response2schema response.json openapi.yaml`

Or when using the phar:

`./response2schema.phar response.json openapi.yaml`

# Example

Given a very simple response:
Expand Down
33 changes: 24 additions & 9 deletions bin/response2schema
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php
}
}

function showHelp() {
function showHelp(int $exit) {
fwrite(STDOUT, <<<OUTPUT
Usage: response2schema [inputfile] [outputfile]
Expand All @@ -27,12 +27,25 @@ Example:
OUTPUT
);
exit(0);
exit($exit);
}

function error(string $message) {
fwrite(STDERR, "\e[31m" . $message . "\e[0m" . PHP_EOL);
showHelp(1);
}

function success(string $message) {
fwrite(STDOUT, "\e[32m" . $message . "\e[0m" . PHP_EOL);
}

$inputPath = null;
$outputPath = null;

if (count($argv) < 3) {
error("Missing input and output file arguments!");
}

foreach($argv as $k => $arg) {
if ($k === 0) {
continue;
Expand All @@ -41,30 +54,32 @@ foreach($argv as $k => $arg) {
// Input
if ($k === 1) {
if (!is_string($arg)) {
echo "Input file path invalid.";
exit(1);
error("Please specify an input file.");
}
if ($arg === 'help' || $arg === '--help') {
showHelp();
showHelp(0);
}
$inputPath = $arg;
}

// Output
if ($k === 2) {
if (!is_string($arg)) {
echo 'Output file path invalid.';
exit(1);
error("Please specify an output file.");
}
$outputPath = $arg;
}
}

if ($inputPath === null || $outputPath === null) {
error("Missing input and output file arguments!");
}

try {
\DSuurlant\Response2Schema\Response2Schema::generate($inputPath, $outputPath);
fwrite(STDOUT, "Generation successful." . PHP_EOL);
success("Generation successful!");
} catch (DomainException $exception) {
fwrite(STDERR, $exception->getMessage() . PHP_EOL);
error($exception->getMessage());
}

exit(0);
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
"macfja/phar-builder": "^0.2.8"
},
"scripts": {
"buildphar": "vendor/bin/phar-builder package -e bin/response2schema -z --name response2schema.phar -o build"
"buildphar": "vendor/bin/phar-builder package -e bin/response2schema -z --name response2schema.phar -o build && chmod 755 build/response2schema.phar"
}
}

0 comments on commit 37d33c0

Please sign in to comment.