Skip to content

Commit

Permalink
Allow arguments and options to be passed through to workflow scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
defunctl committed Sep 9, 2024
1 parent 78e37e6 commit ffea029
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
52 changes: 52 additions & 0 deletions docs/workflows.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,58 @@ pup workflow my-workflow
pup do my-workflow
```

### Pass additional arguments or options to a workflow

You can pass through additional arguments and options to your workflow script.

Example `test-script.sh`:
```bash
#!/usr/bin/env bash

# Loop through all the arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--*=*) # Option in --option=value format
option="${1%%=*}" # Extract the option
value="${1#*=}" # Extract the value
echo "Option: $option, Value: $value"
shift
;;
--*) # Option in --option format (expecting a separate value)
option=$1
shift
if [[ "$1" && ! "$1" =~ ^-- ]]; then
value=$1
echo "Option: $option, Value: $value"
shift
else
echo "Option: $option, No value provided"
fi
;;
*) # Regular argument
echo "Argument: $1"
shift
;;
esac
done
```

Example in `.puprc`:
```json
{
"workflows": {
"my-test-workflow": [
"./test-script.sh"
]
}
}
```

Pass through arguments and options to `test-script.sh`:
```bash
pup workflow my-test-workflow -- arg1 arg2 otherArg --option-one=test1 --option-two=test2
```

## Pseudo-workflows

The `build` and `build_dev` properties within your `.puprc` file are also callable via the `workflow` command.
Expand Down
26 changes: 19 additions & 7 deletions src/Commands/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ class Workflow extends Command {
*/
protected function configure() {
$this->setName( 'workflow' )
->setAliases( [ 'do' ] )
->addArgument( 'workflow', InputArgument::REQUIRED, 'The workflow you would like to run.' )
->addOption( 'root', null, InputOption::VALUE_REQUIRED, 'Set the root directory for running commands.' )
->setDescription( 'Run a command workflow.' )
->setHelp( 'Run a command workflow.' );
->setAliases( [ 'do' ] )
->addArgument( 'workflow', InputArgument::REQUIRED, 'The workflow you would like to run.' )
->addArgument( 'extra_args', InputArgument::IS_ARRAY, 'Additional arguments to pass to the workflow.' )
->addOption( 'root', null, InputOption::VALUE_REQUIRED, 'Set the root directory for running commands.' )
->addOption( 'extra_options', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Additional options to pass to the workflow.' )
->setDescription( 'Run a command workflow.' )
->setHelp( 'Run a command workflow.' );
}

/**
Expand All @@ -34,6 +36,8 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
$config = App::getConfig();
$root = $input->getOption( 'root' );
$workflow_slug = $input->getArgument( 'workflow' );
$extra_args = $input->getArgument( 'extra_args' );
$extra_options = $input->getOption( 'extra_options' );
$io = $this->getIO();
$application = $this->getApplication();
if ( ! $application ) {
Expand Down Expand Up @@ -73,8 +77,16 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
$bail_on_failure = false;
$step = substr( $step, 1 );
}
$io->section( "> <fg=cyan>{$step}</>" );
system( Env::set( $step ), $result );

// Add extra arguments and options to the command.
$extra_args_string = implode( ' ', array_map( 'escapeshellarg', $extra_args ) );
$extra_options_string = implode( ' ', array_map( static function ( $option ) {
return escapeshellarg( $option );
}, $extra_options ) );
$full_command = trim( "{$step} {$extra_args_string} {$extra_options_string}" );

$io->section( "> <fg=cyan>{$full_command}</>" );
system( Env::set( $full_command ), $result );
$io->newLine();

if ( $result ) {
Expand Down

0 comments on commit ffea029

Please sign in to comment.