stepanenko3 / nova-command-runner

This Laravel Nova tool lets you run artisan and bash commands directly from Nova 4 or higher.
MIT License
33 stars 14 forks source link

Support for command options #2

Closed abdulwahid closed 2 years ago

abdulwahid commented 2 years ago

Hi,

Thank you for this awesome package.

I just wanted to ask if you are planning to add support for command options as well via variables soon. The variables work perfectly when used for command arguments but not for options.

We have a scenario in which we are using command options to take start date and end date. Sample command:

command:test-command {--startDate=default} {--endDate=default}

Now in config, if we simply add following:

'Test Command' => [
            'run' => 'command:test-command {--startDate=default} {--endDate=default}',
            'type' => 'primary',
            'group' => 'Test'
        ],

It takes input value for --startDate and --endDate, but we also need to append prefix --startDate= and --endDate= with date values to make it work.

image

Also we can't use field => "date" in this scenario, which is usable if we would have been using arguments.

stepanenko3 commented 2 years ago

@abdulwahid

We support options to configure them in your example, use the following code This will create a log:demo --startDate=2022-04-19 --endDate=2022-04-21 command

'Demo Log' => [
    'run' => 'log:demo --startDate={startDate} --endDate={endDate}',
    'type' => 'primary',
    'group' => 'Text',
    'variables' => [
        [
            'label' =>  'startDate',
            'field' => 'date',
        ],
        [
            'label' =>  'endDate',
            'field' => 'date',
        ]
    ],
],
abdulwahid commented 2 years ago

@stepanenko3 Thanks for your quick response. But this combination doesn't seem to be possible in Laravel command signature. Every option or argument must be enclosed in curly braces, we can't define --startDate= without curly braces in command signature. The example you provided throws error:

The "--startDate" option does not exist. 

because it tries to find {--startDate} in command signature which we don't have, we have it without braces --startDate.

abdulwahid commented 2 years ago

@stepanenko3 Can I create a PR to for this support?

stepanenko3 commented 2 years ago

@abdulwahid,

Yes, it will be great if you open a PR

abdulwahid commented 2 years ago

@stepanenko3

I am unable to push my branch because of permissions.

ERROR: Permission to stepanenko3/nova-command-runner.git denied to abdulwahid.
fatal: Could not read from remote repository.
abdulwahid commented 2 years ago

In src/Dto/CommandDto.php:

I changed from:

$command->setParsedCommand(
                            str_replace('{'.$variable['label'].'}', $variable['value'], $command->getParsedCommand() )
                        );

to:

if (strpos($variable['label'], '--') !== false) {
                        $variableName = explode('=', $variable['label'])[0];
                        $command->setParsedCommand(
                            str_replace('{'.$variable['label'].'}',  $variableName . '=' .$variable['value'], $command->getParsedCommand() )
                        );

                    } else {
                        $command->setParsedCommand(
                            str_replace('{' . $variable['label'] . '}', $variable['value'], $command->getParsedCommand())
                        );
                    }

@stepanenko3

nathan-io commented 2 years ago

Hi @stepanenko3 ! Any plans to add this or adjust the repo permissions so a PR can be made?

stepanenko3 commented 2 years ago

@nathan-io, @abdulwahid I released new version 4.0.11, which implements this

nathan-io commented 2 years ago

Thank you! 🙌