Skip to content

Commit

Permalink
Add allow-error to exec to allow non-zero exit codes
Browse files Browse the repository at this point in the history
  • Loading branch information
donatj committed Nov 6, 2023
1 parent 620793d commit 66df7ad
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,8 @@ class ExecOutput {
public const OPT_CMD = 'cmd';
/** The format to output the result in - options include "raw" "code" and "code-block" defaults to "raw" */
public const OPT_FORMAT = 'format';
/** Set to 'true' to allow non-zero exit codes to continue */
public const OPT_ALLOW_ERROR = 'allow-error';
public const FORMAT_DEFAULT = 'default';
public const FORMAT_RAW = 'raw';
public const FORMAT_CODE = 'code';
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,5 @@ Execute a command and include the standard output in the documentation
#### Attributes:

- `cmd` **(required)** - The command to execute
- `format` - The format to output the result in - options include "raw" "code" and "code-block" defaults to "raw"
- `format` - The format to output the result in - options include "raw" "code" and "code-block" defaults to "raw"
- `allow-error` - Set to 'true' to allow non-zero exit codes to continue
5 changes: 4 additions & 1 deletion src/Documentation/ExecOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class ExecOutput extends AbstractDocPart {
public const OPT_CMD = 'cmd';
/** The format to output the result in - options include "raw" "code" and "code-block" defaults to "raw" */
public const OPT_FORMAT = 'format';
/** Set to 'true' to allow non-zero exit codes to continue */
public const OPT_ALLOW_ERROR = 'allow-error';

public const FORMAT_DEFAULT = 'default';
public const FORMAT_RAW = 'raw';
Expand All @@ -42,14 +44,15 @@ class ExecOutput extends AbstractDocPart {
public function output( int $depth ) : AbstractElement {
$cmd = $this->getOption(self::OPT_CMD);
$format = $this->getOption(self::OPT_FORMAT);
$allow = $this->getOption(self::OPT_ALLOW_ERROR);

if( !in_array($format, self::FORMATS, true) ) {
throw new ConfigException("Invalid exec format '{$format}', expected to be in: " . implode(', ', self::FORMATS));
}

exec($cmd, $output, $return);

if( $return !== 0 ) {
if( !$allow && $return !== 0 ) {
throw new ExecutionException("Command `{$cmd}` returned exit code: {$return}", $return);
}

Expand Down

0 comments on commit 66df7ad

Please sign in to comment.